Javascript Grunt.js Grunt contrib监视事件未触发Grunt.task.run
我有一个Grunt.js文件,它使用“Grunt contrib watch”和“Grunt exec”,原因是我想以一种独特的方式使用Handlebar预编译器的一些自定义功能 守则:Javascript Grunt.js Grunt contrib监视事件未触发Grunt.task.run,javascript,gruntjs,grunt-contrib-watch,Javascript,Gruntjs,Grunt Contrib Watch,我有一个Grunt.js文件,它使用“Grunt contrib watch”和“Grunt exec”,原因是我想以一种独特的方式使用Handlebar预编译器的一些自定义功能 守则: module.exports = function(grunt) { grunt.initConfig({ watch: { src: { files: ['**/*.hbs'] } },
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文件时遇到的问题,2grunt.log.writeln
与-->
按预期回显到控制台
问题是grunt.task.run
似乎从未运行过,因为控制台日志中的DERP-DERP-DERP
从未出现
我是否无法从grunt contrib watcher内部运行任务?我做得不对吗?我只是想在文件发生变化时上传一些文件,但在你和米奇德之间以及大量阅读沙玛的评论之后,我确实找到了一种方法。我想它也应该是你想要的
var watched_files = 'src/**/*';
var my_command = 'echo ';
module.exports = function(grunt) {
grunt.initConfig({
watch: {
scripts: {
files: watched_files,
tasks: ['exec:whatever'],
options: {
spawn: false,
},
},
},
exec: {
whatever: {
cmd: function() {
var fn = this.config.get('exec.current_file');
return my_command + '"' + fn + '"';
}
},
},
});
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.event.on('watch', function(action, filepath) {
grunt.config('exec.current_file', filepath);
});
};
现在,我对grunt还很陌生(比如,今晚学习),所以也许我确实缺少了一些聪明或重要的东西,但这让我有了一种方法,可以通过运行
grunt watch
来对刚更改的文件运行curl命令。我只是尝试在文件更改时上载一些文件,但是在你和米奇德之间,以及在那里大量阅读萨玛的评论,我确实找到了一种方法。我想它也应该是你想要的
var watched_files = 'src/**/*';
var my_command = 'echo ';
module.exports = function(grunt) {
grunt.initConfig({
watch: {
scripts: {
files: watched_files,
tasks: ['exec:whatever'],
options: {
spawn: false,
},
},
},
exec: {
whatever: {
cmd: function() {
var fn = this.config.get('exec.current_file');
return my_command + '"' + fn + '"';
}
},
},
});
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.event.on('watch', function(action, filepath) {
grunt.config('exec.current_file', filepath);
});
};
现在,我对grunt还很陌生(比如,今晚学习它),所以也许我确实遗漏了一些聪明或重要的东西,但这让我有了一种方法,可以对一个刚刚通过运行
grunt watch
而改变的文件运行curl命令。您需要在grunt.initConfig
中配置任务。你可以在他们的官方网站上看到
grunt.initConfig({
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
globals: {
jQuery: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'yourAwasomeTask']
}});
grunt.initConfig({
jshint:{
文件:['grunfile.js','src/***/.js','test/***/.js'],
选项:{
全球:{
jQuery:true
}
}
},
观察:{
文件:[''],
任务:['jshint','yourAwasomeTask']
}});
您需要在grunt.initConfig
中配置任务。你可以在他们的官方网站上看到
grunt.initConfig({
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
options: {
globals: {
jQuery: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'yourAwasomeTask']
}});
grunt.initConfig({
jshint:{
文件:['grunfile.js','src/***/.js','test/***/.js'],
选项:{
全球:{
jQuery:true
}
}
},
观察:{
文件:[''],
任务:['jshint','yourAwasomeTask']
}});
请参见,您可以在grunt.event.on
上为监视事件重新配置watch
任务的src
选项,每次它触发时,使用spawn:false
选项。miqid-我使用了你的解决方案,这个spawn选项很好。想把它作为一个答案,这样我就可以把它作为找到此页面的人的解决方案了吗?请看,您可以为观看事件设置grunt.event.on
,在每次触发时重新配置watch
任务的src
选项,并使用spawn:false
选项。miqid-我使用了你的解决方案,这个spawn选项很好。想把它作为一个答案,这样我就可以把它作为一个解决方案的人谁找到这个页面?