如何创build一个引用其他咕噜任务的咕task任务

我有一个grunt文件,我正在使用它来构build我的web应用程序。

这个咕噜文件使用几个clean contrib插件,如cleancopycompasscssmin等来正确构buildWeb应用程序。

这个咕噜文件也应该处理生成CSS和复制文件来创build主题CSS文件。 目前,我正在为每个主题的cleancopycompass (等)任务添加目标。

这在grunt文件中变得笨拙,并且在添加新主题时会使其变得困难和容易出错。

为了使事情变得更容易,我真的很想创build自己的自定义“主题” grunt任务,在内部使用其他grunt contrib任务( cleancopycompass等)来执行指定主题的所有必要任务。

只有less量configuration数据的主题(主要是它的源文件夹),我会有足够的信息来调用其他任务(因为主题源和目标文件是非常约定驱动)。

我似乎无法find一种方法来调用我的自定义任务中的任务,我可以做到这一点,并以编程方式指定所有的configuration选项,文件等。

有没有人有任何想法我可以做到这一点?

谢谢,埃德

 grunt.initConfig({ clean: {/!* clean task configuration *!/}, copy: {/!* copy task configuration *!/}, compass: {/!* compass task configuration *!/}, cssmin: {/!* cssmin task configuration *!/} }); grunt.registerTask('theme', function () { var tasks = [ 'clean', 'copy', 'compass', 'cssmin' ]; tasks.forEach(function (task) { grunt.task.run(task); }); }); 

我似乎无法find一种方法来调用我的自定义任务中的任务,我可以做到这一点,并以编程方式指定所有的configuration选项,文件等。

我不认为你可以运行具有特定configuration的多任务,因为多任务configuration是从任务configuration中指定的目标中读取的。

所以一种方法是在运行之前修改任务configuration。

这是一个非常基本的例子:

 grunt.registerMultiTask('theme', function() { var themeName = this.options().name; grunt.config.set('yourOtherTask.dist.options.name', themeName); grunt.task.run('yourOtherTask'); }); 

像这样简单:

 concat: { web: { src: ['web/**/*.js'], dest: 'dist/main.js' } }, uglify: { server: { src: '<%= concat.web.dest %>', dest: '<%= concat.web.dest %>.min.js' } }, 

我有同样的问题,解决了它。

请记住:grunt是所有运行在节点上的JavaScript,所以你可以做任何JavaScript和节点支持的东西。

我的解决scheme像这样工作:

首先,把你的核心应用程序的所有东西放在一个单独的JavaScript文件中,例如“grunt-my-app-core.js”。 在那个导出两个函数中, init(grunt)getConfig(grunt, options)

 (function() { "use strict"; //enable ECMAScript 5 Strict Mode function init(grunt) { } function getConfig(grunt, options) { return {}; } /////////////////////////// // Exports exports = module.exports = { init: init, getConfig: getConfig }; })(); 

init(grunt)是加载和注册任务。 可能是这样的:

 /** * public API * * add all required settings to grunt * * register task "dist" and task "doc" * * @param {object} grunt the grunt instance * @return {void} */ function init(grunt) { // overwrite platform specific setting get always unix like line feed char grunt.util.linefeed = '\n'; // Load plugins provide necessary task. grunt.loadNpmTasks('grunt-contrib-clean'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-less'); grunt.registerTask('dist', ['clean:build', 'copy:build', 'copy:dist', 'uglify', 'less']); grunt.registerTask('doc', ['clean:doc', 'copy:doc']); } 

getConfig(grunt, options)将构build并返回configuration对象:

 /** * public API * * will return a config object that have to be given as argument to "grunt.initConfig" * * @param {object} grunt the grunt instance * @param {object|undefined} options to change some pathes * @return {object} the configuration object for initConfig */ function getConfig(grunt, options) { options = options || {}; var buildDir = options.buildDir || "build/"; var distDir = options.distDir || "dist/"; var docDir = options.docDir || "doc/"; // ... doing some stuff to collect files or what ever ... return { clean: { build: { src: [buildDir] }, doc: { src: [docDir] } }, copy: { // ... } // ... add here all the stuff you like to config ... }; } 

而且,在你的主题项目中的Gruntfile.js可以很短,不需要你的核心应用程序的所有东西。 这可能是这样的:

 /** * Module dependencies. */ var gruntMyApp = require("../my-app/grunt-my-app-core.js"); /*global module:false*/ module.exports = function(grunt) { var config = gruntMyApp.getConfig(grunt); // ... extend config, if you need it grunt.initConfig(config); // will register Task "dist" and "doc" gruntMyApp.init(grunt); // Default task. grunt.registerTask('default', ['dist', 'doc']); }; 

现在,如果你改变了你的核心应用程序的东西,所有的主题将得到这个。 你只需要手动更新package.json文件中的devDependencies