【发布时间】:2014-06-17 02:30:01
【问题描述】:
我有一个使用“grunt-contrib-watch”和“grunt-exec”的 Grunt.js 文件,原因是我想以独特的方式使用把手预编译器的一些自定义功能。
代码:
module.exports = function(grunt) {
grunt.initConfig({
watch: {
src: {
files: ['**/*.hbs']
}
},
exec: {
preCompileTemplate: {
cmd: function(inputFile) {
grunt.log.writeln('DERP DERP' + inputFile);
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-exec');
grunt.event.on('watch', function(action, filepath, target) {
grunt.task.run('exec:preCompileTemplate:' + filepath);
grunt.log.writeln('-------> ' + filepath);
grunt.log.writeln('-------> ' + target);
});
}
我在运行 grunt watch 然后更改 .hbs 文件时遇到的问题,2 grunt.log.writeln 和 ------> 按预期回显到控制台。
问题是grunt.task.run 似乎永远不会运行,因为带有DERP DERP DERP 的控制台日志永远不会出现。
我不能在 grunt-contrib-watcher 中运行任务吗?我做错了吗?
【问题讨论】:
-
您可以让
grunt.event.on用于监视事件,每次触发时重新配置您的watch任务的src选项并使用spawn: false选项。 here 描述的使用示例。 -
miqid - 我使用了您的解决方案,生成选项很好。想要将其作为答案,以便我可以将其作为找到此页面的人的解决方案?
标签: javascript gruntjs grunt-contrib-watch