Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

requireJs build script

requireJs build script

Problem

I have code like the following

define("ModuleA", ["InitialDependency"], function (initDep){
   return {};
});


define("ModuleB", ["ModuleA", "OtherDependency"], function (moduleA, otherDep){
   return {};
});

Each of these Modules is defined in separate files "ModuleA.js", "Moduleb.js", "InitialDependency.js" and "OtherDependency.js".

These modules are loaded sequentially in my application. ModuleB is always loaded after ModuleA. this means that in the optimization stage I do not want ModuleA's Script combined in the built script for ModuleB. I want the following

ModuleA.built.js includes

  • InitialDependency
  • ModuleA

ModuleB.built.js includes

  • OtherDependency
  • ModuleB

I don't want them all in the same file however as ModuleB may never be loaded.

I can do a build script for both modules but this will be time consuming as I have quite a few modules in my project and would like a build script that will build the lot of them at once.

What do I need to know to create a build script for building both of these modules (and more that follow the same dependency pattern)?

Problem courtesy of: Aran Mulholland

Solution

To achieve this, you'd have to play with the modules configuration option.

It could look like this:

{
    modules: [
        {
            name: "ModuleA",
            include: [],
            exclude: []
        },
        {
            name: "ModuleB",
            exclude: [
                "moduleA"
            ]
        }
    ]
}

There's a similar example setup by James here: https://github.com/requirejs/example-multipage

Of course, by building these modules separately, you may end up needing to update paths. If so, the best way then would be to create a file containing a require.config call with special setting for your builded app and including this configuration instead of your usual one. But if you set dependencies in a good separated way, then you'll probably be fine. By "good separated" way, I mean that if moduleA is the base script, then it shouldn't have dependencies packed with moduleB - but I guess this is common sense!

Note about shimmed modules: As shimmed config only work whe files are loaded and by r.js to order plugins, be sure you don't include a shim module without it's dependency if you're not 100% sure these will be loaded before. More info here: https://github.com/requirejs/example-multipage-shim

Hope this help!

Solution courtesy of: Simon Boudrias

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

requireJs build script

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×