treeno
treeno

Reputation: 2600

How to to use require.js and optimizer to build a library

I'm thinking about using require.js and it's optimzier to combine and minify a library that consists of more then one js-file. Every js-file contains one single module. But there is no main-module that requires (imports) all modules/js-files. I want to combine all files in one file, that could be used as a library. In that way I could ship the library in one single file instead of several files.

Is this possible, or do I have to create some kind of a main-module that requires /imports all other modules?

Thanks for your help!

treeno

Upvotes: 0

Views: 100

Answers (1)

Louis
Louis

Reputation: 151380

I've not tried it myself but you should be able to pass a build configuration to r.js that contains the following:

modules: [{
    name: "my-lib.js",
    create: true,
    include: ["moduleA", "moduleB", ...]
}]

This tells r.js to put into a single file named my-lib.js all of the dependencies listed in include and tells it that there is no corresponding file named my-lib.js in your sources but that r.js should create it in the output. So the key is:

  • List all the modules you want in include.

  • Use create: true.

The documentation on the optimizer does not mention create but you can find it documented here.

Upvotes: 2

Related Questions