Wudong
Wudong

Reputation: 2360

Grunt how to set up a task to invoke a plugin more than once

I use requirejs to distribute the code into a single file.

In grunt.initConfig, I have:

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
            }
        }
    }
}

this this part of code to set up requirejs. and I use

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

to register it to a task distribute, all works fine.

Now I want to call requirejs twice in the distribute task, once for mobile and once for desktop. Basically I will need to change the baseUrl, name, and out config in the above code.

How can I do that?

Upvotes: 1

Views: 76

Answers (1)

hereandnow78
hereandnow78

Reputation: 14434

grunt-contrib-requirejs is a multitask where you can simply configure as much sub-tasks as you want (using the options property)

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: "..."
            }
        }
    }
}

then you can leave your alias-task distribute as it is, because grunt will run all sub-tasks if none is specified

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

or you can simply just run a single sub-task using the colon-notation:

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

the second one would be useful if you absolutly must guarantee, that compile needs to be run before compile2 (remember: javascript does not guarantee property order in objects)

Upvotes: 2

Related Questions