繁体   English   中英

Grunt.js grunt-contrib-watch事件未触发grunt.task.run

[英]Grunt.js grunt-contrib-watch event not firing grunt.task.run

我有一个Grunt.js文件,该文件使用“ grunt-contrib-watch”和“ grunt-exec”,原因是一些自定义功能,我想以一种独特的方式利用把手预编译器。

编码:

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中运行任务吗? 我做错了吗?

我只是想在文件更改时就上传一些文件,但是在您和miqid之间,以及在那里大量阅读shama的评论之间,我确实找到了一种方法。 我认为这也应该是您正在寻找的

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);
  });
};

现在,我是很新,咕噜(比如,学习它今晚), 也许 肯定有一些聪明还是我缺少重要,但是这让我的方式来运行单单是改变了一个文件curl命令grunt watch跑步。

您需要在grunt.initConfig配置任务。 您可以在他们的官方网站上看到更多信息

grunt.initConfig({
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        globals: {
          jQuery: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint', 'yourAwasomeTask']
    }});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM