Configuring modules with RequireJS when config is dependent on RequireJS

Sorry if I missed this in the docs. Basically I want to use the configuration function of the RequireJS module. I would like to centrally manage the configuration values ​​set by the modules in the 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 information will be a little more complicated and it will have dependencies. I'd like to do:

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

If the variables in my configuration should use requireJS for evaluation.

It would be logical for me to distinguish between the RequireJS configuration - the configuration necessary to load the modules and the configuration of the user module. But I'm currently struggling to find this :(

+7
source share
4 answers

Thinking about this a bit more, I came up with a workaround. It is not particularly beautiful, but it seems to work.

I just require JS (...) twice, first I create the configuration, and the second I load the application modules with the configuration.

 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) 
0
source

For such a solution, I would like the module to depend on the "config" module, which you can change for another using the path configuration. Therefore, if "bar" needs some configuration, "bar.js" will look like this:

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

Then barConfig.js may have your other dependencies:

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

Then, if you need different configurations, for example, production vs. dev, use config config to map barConfig to other values:

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

I think the right way to do this is to create a configuration 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 do it as hard as you like!

EDIT: you can 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 to call:

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

Then you can use it without adding it to your definition:

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

Riffing on what @jrburke says, I found the following template quite useful: define the configuration module and its dependencies in main.js immediately before calling 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

0
source

All Articles