Javascript 定义自定义Grunt任务并将其与其他任务链接
我有以下Javascript 定义自定义Grunt任务并将其与其他任务链接,javascript,gruntjs,Javascript,Gruntjs,我有以下grunfile.js: module.exports = function(grunt) { var config = { shell: { ... }, copy: { ... } }; grunt.initConfig(config); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-shell'); grunt
grunfile.js
:
module.exports = function(grunt) {
var config = {
shell: {
...
},
copy: {
...
}
};
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['shell:compile', 'copy:jsfiles']);
};
我正在使用grunt-contrib-x
组件,这些组件可以配置,然后作为链的一部分注册到任务中
自定义任务怎么样?
我需要添加另一个任务,其工作由函数执行:
var customTask = function() {
// This will do something...
};
我需要在shell:compile
和copy:jsfiles
之后运行它,作为另一个任务的一部分,也可以在其他链中运行。我希望有相同的模式,并且能够做到:
module.exports = function(grunt) {
var config = {
shell: { ... }, copy: { ... },
customTask: function() {
// Doing stuff
}
};
// ... some code ...
grunt.registerTask('default', ['shell:compile', 'copy:jsfiles']);
grunt.registerTask('advanced', ['shell:compile', 'copy:jsfiles', 'customTask']);
grunt.registerTask('advanced2', ['shell:compileComponent', 'copy:jsfilesComponent', 'customTask']);
};
目标是能够创建任务链,并将自定义任务作为要执行的顺序任务列表的一部分
如何实现这一点?调用
grunt.registerTask
并传入名称作为第一个参数,传入要运行的函数作为最后一个参数
grunt.registerTask('myTask', function () {
//do some stuff
});
然后你可以把它锁起来
grunt.registerTask('advanced', ['shell:compile', 'copy:jsfiles', 'myTask']);
基本上,它与示例中的相同,只是将自定义任务定义为grunt.registerTask
的参数,而不是配置中的属性
。此外,你似乎有正确的想法---你现在做的有什么问题吗?这不起作用:)