Grunt watch:只编译一个文件不是全部

我有咕噜安装程序将我所有的咖啡文件编译成JavaScript,并使用dynamic_mappings维护所有文件夹结构,这很好。

coffee: { dynamic_mappings: { files: [{ expand: true, cwd: 'assets/scripts/src/', src: '**/*.coffee', dest: 'assets/scripts/dest/', ext: '.js' }] } } 

我想要做的是使用手表编译任何改变的咖啡文件,仍然保持文件夹结构。 这个工作使用上面的任务,这个任务:

 watch: { coffeescript: { files: 'assets/scripts/src/**/*.coffee', tasks: ['coffee:dynamic_mappings'] } } 

问题是,当一个文件改变时,它将咖啡的整个目录再次编译成Javascript,如果只编译改变为Javascript的单个咖啡文件,那将是非常好的。 这在Grunt中是自然的可能还是这是一个自定义function。 这里的关键是它必须保持文件夹结构,否则它会很容易。

我们有自定义的观察脚本在工作,我试图卖给他们在Grunt,但需要这个function来做到这一点。

你可以使用类似下面的Gruntfile。 每当CoffeeScript文件发生更改时,它都会更新coffee:dynamic_mappings的configuration,以仅将修改后的文件用作src

这个例子是在grunt-contrib-watch自述文件中稍微修改过的例子。

希望能帮助到你!

 var path = require("path"); var srcDir = 'assets/scripts/src/'; var destDir = 'assets/scripts/dest/'; module.exports = function( grunt ) { grunt.initConfig( { coffee: { dynamic_mappings: { files: [{ expand: true, cwd: srcDir, src: '**/*.coffee', dest: destDir, ext: '.js' }] } }, watch : { coffeescript : { files: 'assets/scripts/src/**/*.coffee', tasks: "coffee:dynamic_mappings", options: { spawn: false, //important so that the task runs in the same context } } } } ); grunt.event.on('watch', function(action, filepath, target) { var coffeeConfig = grunt.config( "coffee" ); // Update the files.src to be the path to the modified file (relative to srcDir). coffeeConfig.dynamic_mappings.files[0].src = path.relative(srcDir, filepath); grunt.config("coffee", coffeeConfig); } ); grunt.loadNpmTasks("grunt-contrib-coffee"); grunt.loadNpmTasks("grunt-contrib-watch"); grunt.registerTask("default", [ "coffee:dynamic_mappings", "watch:coffeescript"]); }; 

从类似的问题的答案find解决schemehttps://stackoverflow.com/a/19722900/1351350

简短的回答:尝试https://github.com/tschaub/grunt-newer