【问题标题】:running a dynamically named grunt task with grunt-contrib-watch使用 grunt-contrib-watch 运行动态命名的 grunt 任务
【发布时间】:2014-04-24 16:13:10
【问题描述】:

有没有办法根据更改的文件动态指定要运行的任务?

换句话说:

watch: {
  exec: {
    files: [html/*.html],
    tasks: ['exec:my_exec_task:THE_FILE_THAT_CHANGED']
  }
}

我可以捕获监视事件,但我无法从回调中运行任务,因为它“做错了”。

grunt.event.on('watch', function(action, filepath, target) {
  if (target === 'exec') {
    grunt.task.run('exec:my_exec_task:' + filepath); /* this doesn't work */
    grunt.config('filepath', filepath); /* and neither does this, it's undefined in my exec task */
  }
});

至少文档是这么说的:https://github.com/gruntjs/grunt-contrib-watch#using-the-watch-event

有什么想法吗?

【问题讨论】:

    标签: gruntjs grunt-contrib-watch


    【解决方案1】:

    根据文档

    监视事件并非旨在替换用于配置和运行任务的标准 Grunt API。如果您尝试从监视事件中运行任务,那么您很可能做错了。请阅读配置任务。

    您不能从事件运行任务,但您可以在任务开始之前更改配置。添加到观看配置选项部分spawn: false

    grunt.initConfig({
      watch: {
        scripts: {
          files: ['app/*.js'],
          tasks: ['jshint:one'],
          options: {
            spawn: false,
          },
        },
      },
      jshint: {
        one: {src: ""},
      },
    });
    

    并在监视事件更改配置“即时”

    grunt.event.on('watch', function(action, filepath) {
      grunt.config('jshint.one.src', filepath);
    });
    

    应用配置后,watch 部分将运行任务jahint:one

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2015-06-04
      • 2013-05-25
      相关资源
      最近更新 更多