如何使用 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 文件.
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.
这是我迄今为止最接近的,但frontend-src/coffeescript"文件夹结构正在从 src 目录复制到 dest 目录.
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']
}
}
这就是 Grunt 0.4.0 的全部内容.任何帮助将不胜感激!
This is all with Grunt 0.4.0. Any help would be greatly appreciated!
推荐答案
我也遇到了同样的问题.我使用 regarde:file
事件解决了它.
I've had the same issue. I solved it using the regarde:file
event.
首先,我使用 regarde:file
事件来监听更改的文件.这将为两个任务提供配置:如果源位置中的文件已被删除,则为 clean:coffee
,如果文件已被更改/添加,则为 coffee:refresh
.
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 and coffee:refresh
if files have been changed/added.
然后 regarde
任务将触发其任务,该任务将启动 refresh:coffee
(不要误认为是 coffee:refresh
).此任务将检查是否为 clean:coffee
和/或 coffee:refresh
添加了配置,并在需要时运行这些任务(通过函数 grunt.task.运行
).如果还会重置标志,这将导致下一个接收到的 regarde:file
事件再次清理配置.
Then the regarde
task will trigger its tasks, which will launch refresh:coffee
(not to be mistaken from coffee:refresh
). This task will check if there is configuration added for clean:coffee
and/or for coffee:refresh
and run these tasks if needed (via function grunt.task.run
). If will also reset the flag, which will cause the next received regarde:file
event to cleanup the configuration again.
深入解释:
首先,regarde
配置:
// watch for changed coffeescript files
coffee: {
files: 'path/to/coffee/**/*.coffee',
tasks: ['refresh:coffee', 'livereload']
},
然后我监听 regarde:file
事件,在其中我更新了配置中的 clean:coffee
和 coffee:refresh
文件列表.
Then I listen for the regarde:file
event, where I update the clean:coffee
and coffee:refresh
file lists in their config.
根据 regarde:file
事件提供配置:
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
}
}
}
通过 grunt.config()
函数可以轻松更新配置.下面是提供 coffee:refresh
和 clean:coffee
的代码片段.
It is easy to update configuration via grunt.config()
function. Below the code snippets to feed coffee:refresh
and clean:coffee
.
将配置添加到 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);
将配置添加到 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);
任务 refresh:coffee
被触发:
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屋!