如何使用grunt-regarde与grunt-contrib-coffee只编译更改的.coffee文件? [英] How can I use grunt-regarde with grunt-contrib-coffee to only compile changed .coffee files?
问题描述
我的项目有超过300个CoffeeScript文件,因此需要几秒钟时间才能重新编译所有内容。我想只重新编译更改的CoffeeScript文件。
这是我到目前为止最接近的,但是frontend-src / coffeescript文件夹结构
咖啡:{
changed:{
expand: true,
cwd:'./',
src:['<%= grunt.regarde.changed%>'],
dest:'public / js /',
ext:'.js'
}
},
当前:{
咖啡:{
文件:'frontend-src / coffeescript / * .coffee',
tasks:['coffee:changed','livereload']
}
}
这是Grunt 0.4.0所有。任何帮助将非常感谢!
解决方案我有同样的问题。我使用
regarde:file
事件解决它。
首先,我使用
regarde:file
事件侦听更改的文件。这将为配置执行两个任务:clean:coffee
如果源位置中的文件已删除,并且coffee:refresh
然后
respecte
任务将触发其任务, code> refresh:coffee (不要从咖啡:refresh
中误认为)。此任务将检查是否添加了clean:coffee
和/或coffee:refresh
的配置并运行这些任务如果需要(通过函数grunt.task.run
)。如果也将重置标志,这将导致下一个接收的respecte:file
事件再次清除配置。
详细说明:
首先,
respecte
config://观察更改的coffeescript文件
coffee:{
files:'path / to / coffee / ** /*.coffee',
tasks:['refresh:coffee','livereload']
},
然后我监听
regarde:file
事件,其中更新clean:coffee
和咖啡:刷新
文件列表。
根据
regarde:file
事件进行配置:grunt.event.on('respecte:file',function(status,target,filepath){
if(resetFlag){
//清除上一个周期的文件列表,清除干净:咖啡和咖啡:refresh
//文件列表
...
resetFlag = false;
}
if(status ==='deleted'){
if(filepath){
//计算文件路径的目的地和
//将其添加到clean:coffee filelist
}
} else {
if(!grunt.file.isDir(filepath)){
//将文件路径添加到coffee:refresh filelist
}
}
}
很容易通过
grunt.config()
下面的代码片段咖啡:refresh
和clean:coffee
。
将配置添加到
咖啡:刷新
:
var config = grunt.config('coffee')|| {};
var value = config.refresh || {};
value.files = value.files || [];
...
var cwd = path.dirname(filepath),
src = path.basename(filepath),
dest = cwd.replace('path / to / source ','path / to / dest');
value.files.push({
expand:true,
src:src,
dest:dest,
cwd:cwd,
ext: .js'
});
grunt.config('coffee',config);
将配置添加到
clean:coffee
:var cwd = path.dirname(filepath),
src = path.basename filepath),
dest = cwd.replace('path / to / source','path / to / dest');
value.src.push(path.join(dest,src.replace('coffee','js')));
//只清除已删除的内容
config = grunt.config('clean')|| {};
config.coffee = value;
grunt.config('clean',config);
任务
refresh:coffee
触发:grunt.registerMultiTask('刷新','刷新更改的文件' ){
this.requires('regarde');
var tasks = [];
var clean = grunt.config('clean');
//检查是否有clean:refresh config available
if(clean&& clean [this.target]){
tasks.push('clean:'+ this.target) ;
}
var config = grunt.config(this.target);
//检查是否有咖啡:refresh config available
if(config& & config.refresh){
tasks.push(this.target +':refresh');
}
//运行任务
grunt.task.run );
//将resetFlag设置为true
resetFlag = true;
});
My project has over 300 CoffeeScript files, so it takes several seconds to recompile everything. I'd like to only recompile the changed CoffeeScript files.
Here's the closest I've come so far, but the "frontend-src/coffeescript" folder structure is being copied from the src directory to the dest directory.
coffee: { changed: { expand: true, cwd: './', src: ['<%= grunt.regarde.changed %>'], dest: 'public/js/', ext: '.js' } }, regarde: { coffee: { files: 'frontend-src/coffeescript/**/*.coffee', tasks: ['coffee:changed', 'livereload'] } }
This is all with Grunt 0.4.0. Any help would be greatly appreciated!
解决方案I've had the same issue. I solved it using the
regarde:file
event.First I listen for changed files by using the
regarde:file
event. This will feed the configuration for two tasks:clean:coffee
if files in the source location has been deleted andcoffee:refresh
if files have been changed/added.Then the
regarde
task will trigger its tasks, which will launchrefresh:coffee
(not to be mistaken fromcoffee:refresh
). This task will check if there is configuration added forclean:coffee
and/or forcoffee:refresh
and run these tasks if needed (via functiongrunt.task.run
). If will also reset the flag, which will cause the next receivedregarde:file
event to cleanup the configuration again.In depth explanation:
First of all,
regarde
config:// watch for changed coffeescript files coffee: { files: 'path/to/coffee/**/*.coffee', tasks: ['refresh:coffee', 'livereload'] },
Then I listen for the
regarde:file
event, where I update theclean:coffee
andcoffee:refresh
file lists in their config.Feed the configuration based on the
regarde:file
event:grunt.event.on('regarde:file', function (status, target, filepath) { if (resetFlag) { // clean file list from previous cycle, so clean clean:coffee and coffee:refresh // file lists ... resetFlag = false; } if (status === 'deleted') { if (filepath) { // calculate filepath's destination and // add it to clean:coffee filelist } } else { if (!grunt.file.isDir(filepath)) { // add filepath to coffee:refresh filelist } } }
It is easy to update configuration via
grunt.config()
function. Below the code snippets to feedcoffee:refresh
andclean:coffee
.Adding config to
coffee:refresh
:var config = grunt.config('coffee') || {}; var value = config.refresh || {}; value.files = value.files || []; ... var cwd = path.dirname(filepath), src = path.basename(filepath), dest = cwd.replace('path/to/source', 'path/to/dest'); value.files.push({ expand:true, src:src, dest:dest, cwd:cwd, ext:'.js' }); grunt.config('coffee', config);
Adding config to
clean:coffee
:var cwd = path.dirname(filepath), src = path.basename(filepath), dest = cwd.replace('path/to/source', 'path/to/dest'); value.src.push(path.join(dest, src.replace('coffee', 'js'))); // clean only what has been removed config = grunt.config('clean') || {}; config.coffee = value; grunt.config('clean', config);
Task
refresh:coffee
gets triggered:grunt.registerMultiTask('refresh', 'refreshing the changed file(s)', function () { this.requires('regarde'); var tasks = []; var clean = grunt.config('clean'); // check if there is clean:refresh config available if (clean && clean[this.target]) { tasks.push('clean:' + this.target); } var config = grunt.config(this.target); // check if there is coffee:refresh config available if (config && config.refresh) { tasks.push(this.target + ':refresh'); } // run the tasks grunt.task.run(tasks); // set the resetFlag back to true resetFlag = true; });
这篇关于如何使用grunt-regarde与grunt-contrib-coffee只编译更改的.coffee文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!