configuration依赖于RequireJS时,使用RequireJSconfiguration模块

道歉,如果我错过了这个文件。 基本上我想使用RequireJS模块configurationfunction。 我想集中pipe理给包中模块的configuration值。

这是文档中的一个例子:

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; }); 

我的问题是,我的configuration信息会更复杂一点,并将本身具有依赖性。 我想要做:

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

在我的configuration中的variables需要使用requireJS来评估。

对我来说,在RequireJSconfiguration(加载模块所需的configuration)和用户的模块configuration之间存在逻辑上的分离是有意义的。 但我目前正在努力寻找这:(

对于这种解决scheme,我会让模块依赖于一个“configuration”模块,您可以使用pathconfiguration交换不同的模块。 所以如果“酒吧”需要一些configuration,“bar.js”看起来像:

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

然后barConfig.js可以有你的其他依赖:

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

然后,如果你需要不同的configuration,生产与开发,使用pathconfiguration来将barConfig映射到其他值:

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

我认为正确的方法是做一个configuration模块…

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

然后在其他模块中使用它…

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

然后你可以使它变得复杂,只要你喜欢!

编辑:你可以污染这个特殊类的全球命名空间…

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

将其添加到顶层需要调用:

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

那么你可以使用它,而不必将其添加到你的定义:

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

想到这一点,我想出了一个解决方法。 这不是特别漂亮,但它似乎工作。

我只需要两次requireJS(…),第一次创buildconfiguration,第二次使用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() 

在bar.coffee

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

对于@jrburke说的话,我发现以下模式非常有用:在调用require.config()之前,定义一个configuration模块,并且它是main.js的依赖关系。

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演示: http ://plnkr.co/edit/I35bEgaazEAMD0u4cNuj