首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从另一个grunt文件加载grunt任务

从另一个grunt文件加载grunt任务
EN

Stack Overflow用户
提问于 2015-03-18 10:20:31
回答 1查看 1.6K关注 0票数 3

我想避免重复的代码,所以我尝试从grunt文件"a“加载Grunt任务,并在gruntfile "b”中使用它们。

这意味着:我希望看到文件"b“中"a”的所有任务(但没有代码),只需要像引用或模板一样设置另一个gruntfile。

这是“b”号文件:

代码语言:javascript
复制
module.exports = function (grunt) {
'use strict';

var karmaGrunt = './../../grunt',
    abortHandler = function () {
        var errors = grunt.fail.errorcount,
            warnings = grunt.fail.warncount;

        if (errors > 0 || warnings > 0) {
            //run rocketlauncher python script and then stop the grunt runner.
            grunt.task.run(["shell:rocketlauncher", "fatal"]);
        }
    },
    fatal = function () {
        // this function stops grunt and make the jenkins build red.
        grunt.fail.fatal('failed');
    };

require("grunt-load-gruntfile")(grunt);

// load grunt task from another file and add it.
grunt.loadGruntfile(karmaGrunt);

//grunt needs to continue on error or warnings, that's why we have to set the force property true
grunt.option('force', true);

grunt.initConfig({
    shell: {
        options: {
            execOptions: {
                cwd: '../scripts'
            }
        },
        'rocketlauncher': {
            command: './runRocketLauncher.sh'
        }
    }
});

grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-shell');

grunt.registerTask('build-process', ['karma', 'abortHandler']);
grunt.registerTask('abortHandler', abortHandler);
grunt.registerTask('fatal', fatal);
}

这是文件"a":

代码语言:javascript
复制
module.exports = function (grunt) {
"use strict";
var eConfig = '../e-specs/karma.config.js',
    dConfig = '../d-specs/karma.config.js',
    cConfig = '../c-specs/karma.config.js';
grunt.initConfig({
    karma: {
        options: {
            reporters: ['progress', 'coverage', 'threshold']
        },
        c: {
            configFile: cConfig
        },
        d: {
            configFile: dConfig
        },
        e: {
            configFile: eConfig
        }
    }
});

grunt.loadNpmTasks('grunt-karma');
};

我的文件b加载任务"Karma“,但是如果我只运行a的grunt文件,我有3个嵌套的任务("e”、"c“、"d"),但是如果我从另一个文件加载它们,我唯一能看到的任务就是”业力“。

错误是:

找不到“因果报应”目标。警告:任务“业力”失败。使用-武力,继续。

完成了,但有警告。

如果我在文件"a“中直接运行相同的任务,那么任务就像一种魅力一样工作。

EN

回答 1

Stack Overflow用户

发布于 2015-10-30 06:59:51

有一个Gruntfile来加载另一个Gruntfile:咕噜-负载-格伦特文件

有了这个,您可以合并两个Grunt配置,包括定义的任务。

下面是一个示例:

./Gruntfile.js

代码语言:javascript
复制
module.exports = function (grunt) {

    require("grunt-load-gruntfile")(grunt);

    grunt.loadGruntfile("web"); //loads the Gruntfile from the folder web/

    grunt.registerTask('showConfig', "shows the current config", function(){
        console.log(JSON.stringify(grunt.config(), null, 2));
    });
};

以及./web/Gruntfile.js中的第二个Gruntfile。

代码语言:javascript
复制
module.exports = function (grunt) {

    grunt.config("WebConfig", "Configuration from the Gruntfile in web/Gruntfile.js");

    grunt.registerTask('server', "runs the server",function(){
        console.log("just shows this message");
    });
};

运行grunt showConfig将从第一个Gruntfile执行任务并显示配置,包括在./web/Gruntfile.js中定义的参数。

运行grunt server将从./web/Gruntfile.js执行任务。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29119401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档