基于模板/选项值的Grunt条件声明

时间:2014-06-09 17:37:53

标签: jquery node.js jquery-mobile gruntjs

我在Grunt配置中有这些模板设置:

grunt.config.init({
    targetSuffix:   grunt.option("target") || "dev",

    mainTarget:     grunt.option("installTo") || _WEB_ROOT + "client",
    devTarget:      "<%= mainTarget %>-<%= targetSuffix %>",
    coverageTarget: _WEB_ROOT + "client-coverage" + ("<%= targetSuffix %>" ? "-<%= targetSuffix %>" : ""),

    // ...
});

前两个工作没有错误,无论是在命令行(grunt install --target=user1)还是在&#34;快捷方式中指定的#34;任务:

grunt.registerTask("user1", "same as 'grunt install --target=user1'", function() {
    grunt.config.set("targetSuffix", "user1");
    // or grunt.option("target", "user1");
    grunt.task.run("install");
});

(文档中不清楚,但我现在假设grunt.config处理grunt.option之后发生{。}}。

coverageTarget定义给我带来了问题。如果未指定target,我需要_WEB_ROOT + "client-coverage"。如果有target,请添加连字符,然后添加目标值。与devTarget的区别在于它对于未指定的状态有mainTarget,因为这两个在我的任务中分开使用。

我不需要或者想要对单独的任务/目标进行正确处理以支持coverageMainTargetcoverageDevTarget。但是,我似乎无法在声明中使用任何条件(三元运算符)。

以下是四个测试用例的结果:默认行为,命令行选项,grunt.option()grunt.config.set()

/*
 * "client-coverage-dev" for default and grunt.option().
 * Should be "client-coverage". Implies "<%= targetSuffix %>"
 * isn't "dev", but that's what gets appended.
 * "client-coverage-user1" for CL and grunt.config.set()
 */
coverageTarget: _WEB_ROOT + "client-coverage" + ("<%= targetSuffix %>" != "dev" ? "-<%= targetSuffix %>" : "")

/*
 * Only appends a target for CL option. All others yield
 * "client-coverage".
 */
coverageTarget: _WEB_ROOT + "client-coverage" + (grunt.option("target") ? "-<%= targetSuffix %>" : "")
// or
coverageTarget: _WEB_ROOT + "client-coverage" + (grunt.option("target") ? "-" + grunt.option("target") : "")

/*
 * All output "client-coverage".
 */
coverageTarget: _WEB_ROOT + "client-coverage" + (grunt.config.get("targetSuffix") ? "-<%= targetSuffix %>" : "")
// or
coverageTarget: _WEB_ROOT + "client-coverage" + (grunt.config.get("targetSuffix") ? "-" + grunt.config.get("targetSuffix") : "")

我还尝试过在grunt.config.init之前/之前进行处理,但在这种情况下grunt.option()只返回target选项,如果它在命令行中指定的话,如果设置为grunt.option(),则

如何告诉Grunt根据另一个声明或模板字符串的存在或值构造声明?这里有一些Grunt依赖循环吗?

1 个答案:

答案 0 :(得分:0)

“Grunt选项API用于跨多个任务共享参数并访问在命令行上设置的参数。”但是,为了在Grunt模板*中轻松访问这些值,我决定坚持使用grunt.config代替grunt.option

然后我意识到我可以在之外和之后调用grunt.config.init()(a.k.a。,grunt.initConfig())进行我需要的处理。

grunt.config.init({
    // for example...
    clean: {
        options: {
            force: true
        }
        ,build:    ["client-build/", "client/extlibs/", "coverage/"]
        ,prod:     "<%= installTarget %>/**"
        ,dev:      "<%= installTarget %>/**"
        ,coverage: "<%= cvgTarget %>/**"
    }
});

// store the function so you can re-process, if needed
var processOpts = function () {
    var _WEB_ROOT = "/my/webroot/";
    var _CONTEXTS = ["prod", "dev"];
    var dev = grunt.option("dev");

    if (dev === true) { // if given just "--dev"
        grunt.config.set("installTarget", _WEB_ROOT + "client-dev");
        grunt.config.set("cvgTarget",     _WEB_ROOT + "client-coverage-dev");
        grunt.config.set("context",       "dev");
    } else if (dev !== undefined && dev !== "") { // "--dev=name"
        grunt.config.set("installTarget", _WEB_ROOT + "client-" + dev);
        grunt.config.set("cvgTarget",     _WEB_ROOT + "client-coverage-" + dev);
        grunt.config.set("context",       "dev");
    } else {
        var target = grunt.option("installTo");
        if (target === undefined || target === "") {
            grunt.config.set("installTarget", _WEB_ROOT + "client");
            grunt.config.set("cvgTarget",     _WEB_ROOT + "client-coverage");
        } else {
            grunt.config.set("installTarget", target);
        }

        var context = grunt.option("context");
        if (_CONTEXTS.indexOf(context) === -1) {
            // invalid context, default to first value ("prod")
            grunt.config.set("context", _CONTEXTS[0]);
        } else {
            grunt.config.set("context", context);
        }
    }

    grunt.verbose.writeln("installTarget: " + grunt.config.get("installTarget"));
    grunt.verbose.writeln("cvgTarget: "     + grunt.config.get("cvgTarget"));
    grunt.verbose.writeln("context: "       + grunt.config.get("context"));

    // build context-specific targets dynamically
    var i;
    var commonTasks = ["compass", "requirejs", "clean", "template", "copy", "setmymode"];
    for (i=0; i<commonTasks.length; i++) {
        commonTasks[i] += ":" + grunt.config.get("context");
    }

    grunt.registerTask("prod", ["clean:build", "bower:install", "jshint:prod", "karma:unit"].concat(commonTasks));
    grunt.registerTask("dev",  [               "bower:install"                             ].concat(commonTasks));
};
processOpts();

// main tasks for command-line
grunt.registerTask("default",  ["prod", "coverage"]);

// dev task aliases
grunt.registerTask("n13", "same as 'grunt dev --dev=n13'", function() {
    grunt.option("dev", "n13");
    processOpts();
    grunt.task.run("dev");
});