willcode.co
willcode.co

Reputation: 674

Configuring modules with RequireJS when config depends on RequireJS

Apologies if I have missed this in the docs. Basically I want to use the RequireJS module configuration feature. I would like to centrally manage the config values given to modules in a package.

This is an example from the docs:

requirejs.config({
    config: {
        'bar': {
            size: 'large'
        },
        'baz': {
            color: 'blue'
        }
    }
});

//bar.js, which uses simplified CJS wrapping:
define(function (require, exports, module) {
    //Will be the value 'large'
    var size = module.config().size;
});

//baz.js which uses a dependency array,
define(['module'], function (module) {
    //Will be the value 'blue'
    var color = module.config().color;
});

My problem is that my configuration info will be a little more complex, and will itself have dependencies. I would like to do:

requirejs.config({
    config: {
        'bar': {
            path: path.dirname(module.uri)
            key: crypto.randomBytes(64)
        },
    }
});

Where variables in my config need to use requireJS to evaluate.

To me it would make sense for there to be a logical separation between the RequireJS configuration - the config necessary to load modules - and the user's module configuration. But I am currently struggling to find this :(

Upvotes: 7

Views: 11359

Answers (4)

f1lt3r
f1lt3r

Reputation: 2223

Riffing on what @jrburke is saying, I found the following pattern to be quite useful: define a config module and it's dependencies in the main.js just before the invocation of require.config().

main.js

define('config', ['crypto'], function (crypto) {
  return {
    'bar': {
      key: crypto.randomBytes(64)
    },
  };
});

requirejs.config({
  deps: ['app'],
});

app.js

require(['config'], function (config){

  // outputs value of: crypto.bar.key
  console.log(config.bar.key);
});

Plnkr Demo: http://plnkr.co/edit/I35bEgaazEAMD0u4cNuj

Upvotes: 0

jrburke
jrburke

Reputation: 6766

For this sort of solution, I would have the module depend on a "config" module that you can swap for a different one using paths config. So if "bar" needed some config, "bar.js" would look like:

define(['barConfig'], function (config) {
});

Then barConfig.js could have your other dependencies:

define(['crypto'], function (crypto) {
    return {
      key: crypto.randomBytes(64)
    }
});

Then, if you needed different configs for say, production vs. dev, use paths config to map barConfig to other values:

requirejs.config({
  paths: {
    barConfig: 'barConfig-prod'
  }
});

Upvotes: 6

willcode.co
willcode.co

Reputation: 674

Having thought about this a little more I have come up with a workaround. It is not particularly pretty but it does seem to work.

I simply do requireJS(...) twice, first to create the config, and second to load the application modules with the config..

requireJSConfig = 
    baseUrl: __dirname
    nodeRequire: require

# Create the require function with basic config
requireJS = require('requirejs').config(requireJSConfig)  
requireJS ['module', 'node.extend', 'crypto', 'path'], (module, extend, crypto, path) ->
    # Application configuration 
    appConfig =
        'bar':
            path:   path.dirname(module.uri)
            key:    crypto.randomBytes(64) # for doing cookie encryption

    # get a new requireJS function with CONFIG data
    requireJS = require('requirejs').config(extend(requireJSConfig, config: appConfig))
    requireJS ['bar'], (app) ->
        ###
            Load and start the server
        ###
        appServer = new app()

        #  And start the app on that interface (and port).
        appServer.start()

And in bar.coffee

# bar.coffee
define ['module'], (module) ->
    # config is now available in this module
    console.log(module.config().key)

Upvotes: 0

Felix
Felix

Reputation: 1205

I think the proper way to do this is to make a config module...

// config.js
define(['module', 'path', 'crypto'], function(module, path, crypto) {
    return {
        path: path.dirname(module.uri)
        key: crypto.randomBytes(64)
    };
}); 

Then use it in other modules...

// bar.js
define(['config'], function (config) {
    var key = config.key;
});

You can then make it as complicated as you like!

EDIT: You could pollute the global namespace for this special class...

define(['module', 'path', 'crypto'], function(module, path, crypto) {
    window.config = {
        path: path.dirname(module.uri)
        key: crypto.randomBytes(64)
    };
}); 

Add it to the top level require call:

require(['config', 'main']);

Then you can use it without always adding it to your define:

// bar.js
define([], function() {
    var key = config.key;
});

Upvotes: 2

Related Questions