我只能找到examples关于在搜索过程中使用grunt-newer
但jshint
而不是requirejs
。所以我在我的grunt配置文件中使用这个配置:
watch: {
options: {
nospawn: true
},
js: {
files: [
'public/**/*.js',
'!public/asset/**/*.js'
],
tasks: ['newer:requirejs']
},
},
requirejs: getRequirejsOptions(),
...
requirejs选项是这样的:
module.exports = function () {
var options = {};
files.forEach(function (file) {
options[file] = {
options: {
baseUrl: path.join(__dirname, '../public'),
include: file,
create: true,
out: path.join(__dirname, '../public/asset/', file + '.js'),
optimize: process.env.NODE_ENV == 'local' ? 'none' : 'uglify',
preserveLicenseComments: !false,
packages: [
...
]
}
}
});
return options;
}
它确实触发grunt watch
仅对已更改的文件运行requirejs
:
$ grunt watch
Running "watch" task Waiting...
>> File "public/stock/js/main.js" changed.
Running "newer:shell:requirejs" (newer) task No newer files to process.
Running "watch" task Completed in 1.834s at Fri Jan 06 2017 12:08:05 GMT+0800 (CST) - Waiting...
但刷新浏览器后,我发现源文件没有改变。