문제

동시를 사용하여 Nodemon과 Watch/Livereload를 모두 실행하는 Grunt 작업을 실행하고 있습니다. 기본 부하에서는 동시에 보풀 및 실행합니다. 또한 변경시 개별 파일을 보라는 시계를 설정하고 싶습니다. 현재 하나의 파일이 변경되면 모든 파일이 보풀됩니다.

나는 stackoverflow에서 비슷한 질문을 조사했으며 Grunt-Newer와 잠재적 인 솔루션으로 가기로 결정했습니다. 그러나 아래 구현에서 '최신'접두사는 아무것도하지 않는 것 같습니다. 변경된 파일 만 보풀되도록 어떻게 수정하려면 어떻게해야합니까?

module.exports = function(grunt) {
  //load all dependencies
  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concurrent: {
      dev: {
        options: {
          logConcurrentOutput: true
        },
        tasks: ['watch', 'nodemon']
      }
    },
    jshint: {
      files: ['Gruntfile.js', 'client/src/*.js', 'server/**/*.js'],
      options: {
        '-W030': true,
        '-W083': true,
        globals: {
          console: true,
          module: true,
          document: true
        }
      }
    },
    watch: {
      all: {
        files: ['<%= jshint.files %>'],
        tasks: ['newer:jshint']
      },
      frontend: {
        files: ['client/**/*.{css,js,html}'],
        options: {
          livereload: true
        }
      }
    },
    nodemon: {
      dev: {
        options: {
          file: 'server/server.js',
          watchedFolders: ['server']
        }
      }
    }
  });

  grunt.registerTask('test', ['jshint']);
  grunt.registerTask('default', ['jshint', 'concurrent']);

};
도움이 되었습니까?

해결책

나는 같은 문제가 있었고 마침내 그것을 알아 냈습니다. 해결책은 문서에 깊이 숨겨져 있으며 spawn 코드 샘플의 옵션 : https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed

구성 파일은 질문에있는 것과 동일하게 유지되지만 시계 이벤트에 리스너를 추가해야합니다. 그들이 제공하는 '견고한'옵션 (특정 작업 구성을 위해 수정)을 권장합니다. 이 코드를 통화 바로 위에 놓습니다 grunt.initConfig 그리고 당신 이후 require 전화.

var changedFiles = Object.create(null);
var onChange = grunt.util._.debounce(function() {
  // Modified to point to jshint.files as per the task example in the question.
  grunt.config('jshint.files', Object.keys(changedFiles));
  changedFiles = Object.create(null);
}, 200);

grunt.event.on('watch', function(action, filepath) {
  changedFiles[filepath] = action;
  onChange();
});

추가 nospawn 옵션 all 작업을 시청하십시오. 이것은 문서에서 오해의 소지가있는 것입니다. 구성을 동적으로 수정하려면 비활성화해야하지만 기본적으로 설정이 설정되지 않는 한 기본적으로 최신 작업을 방지해야합니다. true:

 watch: {
   all: {
     files: ['<%= jshint.files %>'],
     tasks: ['newer:jshint'],
     options: {
       nospawn: true,
     }
   },
   ...

참고 : 실행 중에 Grunt 파일을 수정하면 모든 파일에 보풀을 뿌려야하는 이유는 확실하지 않지만 그 일이 발생하는 이유는 확실하지 않지만 모든 변경 사항에 대해 모든 것을 계속 줄입니다. 방금 피하기 위해 보풀을 해야하는 파일 목록에서 'gruntfile.js'를 꺼 냈습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top