Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Grunt更新的自定义任务_Javascript_Gruntjs_Grunt Contrib Watch - Fatal编程技术网

Javascript 使用Grunt更新的自定义任务

Javascript 使用Grunt更新的自定义任务,javascript,gruntjs,grunt-contrib-watch,Javascript,Gruntjs,Grunt Contrib Watch,我正在尝试使用grunt更新来监视文件夹中的文件,如果有更改,则触发自定义任务 我的Grunfile.js中有类似的内容: grunt.initConfig({ watch: { widgets: { files: "/somepath/*.js", tasks: ['newer:mycustomtask'] } } }); grunt.registerTask("mycustomtask", ["description

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

我的Grunfile.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”中查看哪些文件已更改

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

grunt.initConfig({

  mycustomtask: {
  },

  watch: {
    widgets: {
      files: "/somepath/*.js",
      tasks: ['newer:mycustomtask']
    }
  }
});
如果您引用的任务(如内部监视或并发任务)未安装或未配置,则会得到此错误输出


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

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

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();
    });
};
现在,
grunfile.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']);
};
在这里,它规定:

  • 感兴趣的文件是
    ['src/config.js','src/data.js']
  • 我们的
    customtask
    原则上对这些文件进行操作(以防直接调用)
  • watch
    应该观察这些文件,并在发生变化时启动
    customtask:release
  • grunt.loadTasks('tasks')
    从目录
    tasks
    加载所有“任务定义”,即此处仅加载
    customtask
  • grunt.registerTask('build',['watch:data'])
    watch:data
  • 最后,为了仅为更改的文件调用
    customtask
    ,本例使用了“根据需要编译文件”一节中采用的策略。松散地说,它将所有更改的文件组合到一个对象中,然后使用该对象的键动态修改
    customtask
    src
    属性

    运行
    grunt build
    然后启动“监视”。如果在另一个终端窗口中运行,例如
    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