Grunt如何设置一个不止一次调用插件的任务

时间:2014-08-06 11:58:21

标签: javascript requirejs gruntjs grunt-contrib-requirejs

我使用requirejs将代码分发到一个文件中。

在grunt.initConfig中,我有:

grunt.initConfig({

   requirejs: {
        compile: {
            options: {
                paths: requirejs_path,
                shim:require_shim,

                baseUrl : "./mobile",
                name: "mobilemain",
                out: "./mobile/dist/main.js",
                removeCombined: true,
                findNestedDependencies: true,
                optimize: "uglify2",
                wrap: true,
                uglify2: requirejs_uglify2
            }
        }
    }
}

这部分代码设置了requirejs。我用

grunt.registerTask("distribute", ["typescript", "requirejs"]);

将其注册到任务distribute,一切正常。

现在我想在distribute任务中调用两次requirejs,一次用于移动,一次用于桌面。基本上,我需要在上面的代码中更改baseUrlnameout配置。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

grunt-contrib-requirejs是一个多任务,您可以根据需要配置尽可能多的子任务(使用options属性)

grunt.initConfig({

   requirejs: {

        // define your base options which are the same for both tasks
        options: {
            paths: requirejs_path,
            shim:require_shim,
            removeCombined: true,
            findNestedDependencies: true,
            optimize: "uglify2",
            wrap: true,
            uglify2: requirejs_uglify2
        },

        // your first task which contains the 3 options which should be used 
        compile: {
            options: {
                baseUrl : "./mobile",
                name: "mobilemain",
                out: "./mobile/dist/main.js"
            }
        },

        // your second task which contains the 3 options which should be used 
        compile2: {
            options: {
                baseUrl : "...",
                name: "...",
                out: "..."
            }
        }
    }
}

然后您可以保留别名任务distribute,因为如果没有指定,grunt将运行所有子任务

grunt.registerTask("distribute", ["typescript", "requirejs"]);

或者您只需使用冒号表示运行单个子任务:

grunt.registerTask("distribute", ["typescript", "requirejs:compile", "requirejs:compile2"]);
如果绝对必须保证,compile需要在compile2之前运行,那么第二个将是有用的(请记住:javascript does not guarantee property order in objects