Search code examples
javascriptconfigurationrequirejsamd

requireJS pass configuration info to all modules with "*" wildcard


I have a need to pass a configuration value into all my AMD modules using requireJS.

I can happily pass a configuration value to a specific module by using the following example; as given in the requireJS API config modules documentation

requirejs.config({
    config: {
        'bar': {
            raw: true
        },
        'baz': {
            raw: true
        },
        ...
    }
});

The above works fine, but I have some 50 modules that I want to pass the same configuration value to, and these could increase or change. I could define 50 module names, as above, and pass the value, but I don't really want to have to define each module by name and maintain that list, instead I'd like to do something like this.

requirejs.config({
    config: {
        '*': {
            raw: true
        }
    }
});

I have tried the above but it did not work.

requireJS config map appears to support the "*" wildcard, but I don't see any mention of similar for "config". I have searched, but I guess I'm having a "bad search day".

So, the question is: Is there a "*" wildcard support for "config" and I am just having a problem with it? Or is there some other way to accomplish this?


Solution

  • I don't know a way to use the wildcard syntax directly, but you can accomplish the same thing through the simplest module definition at the bottom of your data-main:

    requirejs.config({
        // normal stuff
    });
    
    define('moduleconfig', {
        color: "black",
        size: "unisize"
    });
    

    And then instead of requiring the special 'module' module, just require your 'moduleconfig' module:

    define( ['underscore', 'jquery', 'moduleconfig'], function( _, $, moduleconfig) {
    
        console.log('Color', moduleconfig.color)
    
     });