Vlad Tsepelev
Vlad Tsepelev

Reputation: 2046

How to extend configs with RequireJS

Im using RequireJS for my coding. I have one source base, but have few different frontends. These frontends should use different sets of modules, so I have to support few RequireJS configs, each for different product. Adding module to one config, cause adding it to few additional configs.

Is there way to inherit/extend RequireJS configs?

Upvotes: 6

Views: 2742

Answers (2)

Joe T
Joe T

Reputation: 794

Louis' answer is right, but just a note, it is possible to read from multiple configs when using the r.js optimizer. According to the example build file you can pass an array of values to the mainConfigFile, with the latter taking precedence over the former.

Upvotes: 1

Louis
Louis

Reputation: 151380

If you give multiple configurations to RequireJS, it will combine them. For instance if you first make this call:

require.config({
    paths: {
        foo: "path/to/foo",
        bar: "path/to/bar"
    }
});

And then this second call:

require.config({
    paths: {
        foo: "some/other/path/to/foo",
        baz: "path/to/baz"
    }
});

These two will be combined so that bar still has the path defined in the first require.config call. However, foo will be overridden by the second config. And baz won't be resolved as path/to/baz until the 2nd config is read.

There are limitations however. For instance, you can give an array as a paths setting for a specific module. For instance, foo: ["/some/path", "another/path"]. RequireJS will try the paths in order when you require foo. However, there is no method to tell RequireJS in a subsequent call to require.config to take whatever array was set earlier and just add another value to the array. You'd have to give require.config the whole array in a subsequent call.

Upvotes: 7

Related Questions