Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 753
Vlad Tsepelev Avatar asked Apr 04 '14 14:04

Vlad Tsepelev


1 Answers

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.

like image 117
Louis Avatar answered Sep 22 '22 12:09

Louis