[英]Grunt.js grunt-contrib-watch event not firing grunt.task.run
[英]How do you watch multiple files, but only run task on changed file, in Grunt.js?
在學習如何使用grunt 時,我正在嘗試制作一個簡單的咖啡腳本觀察器/編譯器。 問題是,如果我告訴watch
任務監視多個文件,並且其中一個發生更改,它會將所有文件傳遞給coffee
命令。 這意味着當您更改 1 個文件時,它將重新編譯與src
模式匹配的所有文件。 相反,我只想重新編譯與src
模式匹配的更改的單個文件。
這是grunt.js
:
module.exports = function(grunt) {
grunt.initConfig({
coffee: {
app: {
src: ['test/cases/controller/*.coffee'],
dest: 'tmp',
options: {
bare: true,
preserve_dirs: true
}
}
},
watch: {
files: ['<config:coffee.app.src>'],
tasks: ['coffee:app']
}
});
grunt.loadNpmTasks('grunt-coffee');
grunt.registerTask('default', 'coffee');
};
這是使用grunt-coffee ,基本上是這樣的: https : //gist.github.com/2373159 。
當我運行grunt watch
並將文件保存在test/cases/controller/*.coffee
,它會編譯所有匹配的文件(將它們放在tmp/*
)。
你如何只使用 grunt編譯更改過的文件?
即將發布(目前正在開發)的 v0.4.0a grunt 有grunt.file.watchFiles對象,它是專門為此目的而設計的。 grunt-coffee 插件可能已經添加了對這個功能的支持,我不確定。
無論哪種方式,如果您有興趣在您的項目中嘗試開發中的 grunt 版本,請查看我何時能夠使用開發中的功能“X”? 常見問題入口。
我在編譯我的 less 文件時得到了這個工作。 你應該能夠稍微弄亂這個配置,讓它與 coffeescript 插件一起工作。 感興趣的部分是grunt.event.on('watch', ...)
。 在此事件處理程序中,我正在更新 less 命令中的files
屬性以僅包含更改的文件。
path = require('path');
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
less: {
development: {
options: {
paths: ["./library/less"],
},
files: [
{ src: "./library/less/bootstrap.less", dest: "./library/css/bootstrap.css"},
{ src: "./library/less/app.less", dest: "./library/css/app.css"}
]
}
},
watch: {
styles: {
files: "./library/less/*",
tasks: ["less"],
options: {
nospawn: true,
},
},
},
});
// Event handling
grunt.event.on('watch', function(action, filepath){
// Update the config to only build the changed less file.
grunt.config(['less', 'development', 'files'], [
{src: filepath, dest: './library/css/' + path.basename(filepath, '.less') + '.css'}
]);
});
// Load the plugins
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
// Tasks
grunt.registerTask('default', ['watch']);
};
這些答案都不適合我。 如果有人感興趣,這是我的解決方案(我知道我回答這個問題有點晚了)。
在本期中,Kyle Robinson 建議使用watch
事件。 將 watch task nospawn
屬性設置為true
以使其工作非常重要。 我修改了他的解決方案以有選擇地運行任務:
grunt.event.on('watch', function(action, filepath) {
if (minimatch(filepath, grunt.config('watch.stylesheets.files'))) {
grunt.config('compass.dist.options.specify', [filepath]);
}
if (minimatch(filepath, grunt.config('watch.scripts.files'))) {
var uglifySrc = filepath.replace(grunt.config('uglify.dist.cwd'), '');
grunt.config('jshint.dist.src', [filepath]);
grunt.config('uglify.dist.src', [uglifySrc]);
}
});
這是完整的解決方案: https : //gist.github.com/luissquall/5408257
https://github.com/tschaub/grunt-newer看起來與類似任務完全一樣:
將 Grunt 任務配置為僅使用較新的文件運行。
概要:較新的任務將配置另一個任務以使用 a) 比 dest 文件新或 b) 比上次成功運行(如果沒有 dest 文件)新的 src 文件運行。 有關示例和更多詳細信息,請參見下文。
您可以輕松地添加到任何任務。 在你的情況下:
grunt.loadNpmTasks('grunt-newer');
grunt.registerTask('default', 'newer:coffee');
所以 Grunt 0.4 的新功能是更多命名任務
讓我們給你舉個例子!
watch: {
package1: {
files: [
'./modules/package1/**/*.coffee'
],
tasks: ['coffee:package3']
},
package2: {
files: [
'./test_packages/package2/**/*.coffee'
],
tasks: ['coffee:package3']
},
package3: {
files: [
'./test_packages/package3/**/*.coffee'
],
tasks: ['coffee:package3']
},
}
要運行所有這些監視任務,只需執行 grunt.registerTask('default', ['myInitialBuild', 'watch']);
其中myInitialBuild
是任何初始構建(所有文件),然后在每個包上觀察它。 實際上,您可以對每個文件都執行此操作,但這聽起來很糟糕。
任務 grunt-contrib-watch 現在支持這一點。
https://npmjs.org/package/grunt-contrib-watch -> 尋找“根據需要編譯文件”
grunt.initConfig({
watch: {
scripts: {
files: ['lib/*.js'],
tasks: ['jshint'],
options: {
nospawn: true,
},
},
},
jshint: {
all: ['lib/*.js'],
},
});
// on watch events configure jshint:all to only run on changed file
grunt.event.on('watch', function(action, filepath) {
grunt.config(['jshint', 'all'], filepath);
});
這應該可以防止任務每次發生變化時編譯所有文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.