使用Grunt更新自定义任务

Use Grunt newer with custom task

本文关键字:任务 自定义 更新 Grunt 使用      更新时间:2023-09-26

我正在尝试使用grunt-newer来监视文件夹中的文件,如果有任何更改,则触发自定义任务。

我有这样的东西在我的Gruntfile.js:

grunt.initConfig({
   watch: {
      widgets: {
         files: "/somepath/*.js",
         tasks: ['newer:mycustomtask']
      }
   }
});
grunt.registerTask("mycustomtask", ["description of my task"], function() {
  console.log("me has been triggered");
});

每当我运行"grunt watch"时,我有这样的输出:

Running "watch" task
Waiting...
File "/somepath/WidgetA.js" changed.
Running "newer:mycustomtask" (newer) task
Fatal error: The "newer" prefix is not supported for aliases

我谷歌了一下,但没有找到任何关于这个的东西。有人知道我怎么实现这个吗?我现在需要在我的"customtask"中,哪些文件已经更改

如果你引用一个任务(内部手表或并发),要么是未安装未配置你得到这个错误输出

当您从不同的项目复制粘贴一个watch配置时,这种情况经常发生。

我遇到了类似的需求,我最终得到的解决方案大致如下。让我们假设项目结构是:

Gruntfile.js
package.json
src/
    config.js
    data.js
tasks/
    customtask.js

这里,src目录包含将由watch监视的数据,而自定义任务的定义存储在tasks/customtask.js中。对于本例,该任务将只打印已更改文件的文件名:

var fs = require('fs');
var path = require('path');
module.exports = function(grunt) {
    grunt.registerMultiTask('customtask', function() {
        var done = this.async();
        if(!this.files){ done(); return; }
        this.files[0].src.forEach(file_name => {
            console.log(file_name);
        });
        done();
    });
};

现在,Gruntfile.js看起来像:

module.exports = function(grunt) {
    const files = ['src/config.js', 'src/data.js'];
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        customtask: {
            release: {
                src: files
            }
        },
        watch: {
            data: {
                files: files,
                tasks: ['customtask:release']
            },
            options: {
                spawn: false
            }
        }
    });
    grunt.loadTasks('tasks');
    grunt.loadNpmTasks('grunt-contrib-watch');
    var changedFiles = Object.create(null);
    var onChange = grunt.util._.debounce(function() {
        grunt.config('customtask.release.src', Object.keys(changedFiles));
        changedFiles = Object.create(null);
    }, 200);
    grunt.event.on('watch', function(action, filepath) {
        changedFiles[filepath] = action;
        onChange();
    });
    grunt.registerTask('build', ['watch:data']);
};

在这里,它指定:

  1. 感兴趣的文件是['src/config.js', 'src/data.js']
  2. 我们的customtask原则上对这些文件进行操作(以防直接调用)
  3. watch应该观察这些文件,并启动customtask:release每当一些变化
  4. grunt.loadTasks('tasks')从目录tasks加载所有的"任务定义",也就是说,这里只有customtask
  5. grunt.registerTask('build', ['watch:data'])watch:data定义了一个快捷方式

最后,为了只对已更改的文件调用customtask,本例使用了"按需编译文件"一节中文档中使用的策略。简而言之,它将所有更改的文件组装在一个对象中,然后使用该对象的键来实时修改customtasksrc属性。

运行grunt build启动"watch"。如果在另一个终端窗口(例如touch src/*.js)中运行,输出为:

Running "watch:data" (watch) task
Waiting...
>> File "src/config.js" changed.
>> File "src/data.js" changed.
Running "customtask:release" (customtask) task
src/config.js
src/data.js

最后两行来自customtask

你只需要为你的任务有一个配置条目(即使是空的):

grunt.initConfig({
  mycustomtask: {
  },
  watch: {
    widgets: {
      files: "/somepath/*.js",
      tasks: ['newer:mycustomtask']
    }
  }
});