如何创建引用其他grunt任务的grunt任务

时间:2013-08-27 03:07:26

标签: javascript node.js gruntjs

我有一个 grunt 文件,我用它来构建我的网络应用程序。

grunt 文件使用多个 grunt 个约翰插件,如cleancopycompasscssmin,等,以正确构建网络应用程序。

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

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

为了简化操作,我真的想创建自己的自定义“主题” grunt 任务,该任务将在内部使用其他 grunt contrib任务({{1 }},cleancopy等)执行指定主题的所有必要任务。

对于主题(主要是其源文件夹)只有少量的配置数据,我将有足够的信息来调用其他任务(因为主题源和目标文件是非常常规驱动的)。

我似乎找不到从我的自定义任务中调用任务的方法,我可以在其中以编程方式指定所有配置选项,文件等。

有谁知道我该怎么做?

谢谢,Ed

4 个答案:

答案 0 :(得分:8)

  

我似乎找不到从自定义任务中调用任务的方法   在哪里我可以做到并指定所有配置选项,文件等。   编程。

我不认为您可以使用特定配置运行grunt多任务,因为从任务配置中指定的目标读取多任务配置。

所以这样做的一种方法是在运行之前修改任务配置。

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

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

答案 1 :(得分:5)

这很简单:

concat: {
  web: {
    src: ['web/**/*.js'],
    dest: 'dist/main.js'
  }
},

uglify: {
  server: {
    src: '<%= concat.web.dest %>',
    dest: '<%= concat.web.dest %>.min.js'
  }
},

答案 2 :(得分:1)

我遇到了同样的问题并解决了它。

请记住:grunt是在节点中运行的所有JavaScript,因此您可以执行JavaScript和节点支持的任何事情。

我的解决方案是这样的:

首先,将所有内容放在核心应用程序的grunt中,例如单独的JavaScript文件&#34; grunt-my-app-core.js&#34;。 在该导出中有两个函数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)将构建并返回配置对象:

/**
 * 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']);
};

现在,如果您在核心应用上更改了某些内容,那么所有主题都将获得此功能。您需要更新手册的唯一方法是devDependencies文件中的package.json

答案 3 :(得分:1)

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);
    });
});
相关问题