2013-05-30 1 views
2

내 Gruntfile.js에 다음과 같은 구성이 있습니다. 문제는 일부 파일이 변경되면 'uglify'작업이 모든 파일에 대해 평소대로 수행된다는 것입니다. 내가 뭘 잘못하고 있니?Uglify의 동적 확장 기능이 활성화 된 경우 Grunt.js 감시 작업으로 변경된 파일 만 uglify하는 방법은 무엇입니까?

module.exports = function(grunt) { 
    grunt.initConfig({ 
     pkg  : grunt.file.readJSON('package.json'), 
     watch: { 
      scripts: { 
       files: ['js/**/*.js'], 
       tasks: ['uglify'] 
      } 
     }, 
     uglify : { 
      options: { 
       banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n', 
       sourceMapRoot: 'www/js/sourcemap/' 
      }, 
      build: { 
       files: [ 
        { 
         expand: true, 
         cwd: 'js/', 
         src: ['**/*.js', '!**/unused/**'], 
         dest: 'www/js/', 
         ext: '.js' 
        } 
       ] 
      } 
     } 
    }); 

    grunt.loadNpmTasks('grunt-contrib-watch'); 
    grunt.loadNpmTasks('grunt-contrib-uglify'); 

    grunt.event.on('watch', function(action, filepath) { 
     grunt.config(['uglify', 'build', 'src'], filepath); 
    }); 

    grunt.registerTask('default', ['uglify', 'watch']); 
}; 

답변

3

기본적으로 감시 작업은 작업 실행을 생성합니다. 따라서 그들은 서로 다른 프로세스 컨텍스트에 있으므로 watch 이벤트에서 config를 설정하는 것은 효과가 없습니다. 동일한 맥락에서 작업이 실행를 생성하고 유지하지 nospawn를 사용하도록 설정해야합니다 :

watch: { 
    options: { nospawn: true }, 
    scripts: { 
    files: ['js/**/*.js'], 
    tasks: ['uglify'] 
    } 
}, 
1

당신은 거의 있었다. 귀하의 "onWatch"기능을 이런 식으로 뭔가 보일 것입니다 : 당신의 wacth 작업 옵션에서

grunt.event.on('watch', function(action, filepath, target) { 

    grunt.config('uglify.build.files.src', new Array(filepath)); 

    // eventually you might want to change your destination file name 
    var destFilePath = filepath.replace(/(.+)\.js$/, 'js-min/$1.js'); 
    grunt.config('uglify.build.files.dest', destFilePath); 
}); 

nospawn:true 너무 필수입니다.

관련 문제