使用Grunt更新自定义任务
Use Grunt newer with custom task
我正在尝试使用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']);
};
在这里,它指定:
- 感兴趣的文件是
['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
启动"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']
}
}
});
相关文章:
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 自定义咕噜声任务命名约定
- 使用 gruntjs 自定义初始化任务重命名模板文件夹
- 正在新文件中创建自定义任务
- 在拉力赛中:我是否可以将自定义任务状态映射到拉力赛任务板中的现有任务状态(类似于用户情景的看板)
- 带有 Web 浏览器控件的 Excel 自定义任务窗格 - 使用功能区按钮显示/隐藏
- 自定义咕噜声任务上的错误“对象没有方法'endsWith'”
- 创建自定义的 Grunt 任务来处理文件
- Grunt:自定义任务-否;任务名称“;找到个目标
- 在任务栏浏览器选项卡上显示自定义文本
- firefox中用于特定任务的自定义按钮
- 与另一个grunt任务共享来自自定义grunt任务的数据
- Grunt:自定义任务开发指南
- 使用Grunt更新自定义任务
- 用自定义任务扩展meteor的构建
- 定义自定义Grunt任务并将其与其他任务链接
- 通过自定义Grunt任务处理文件
- 在我的任务代码运行之前,如何从我的自定义任务中调用其他任务?
- 带有自定义json配置的grunt任务