[英]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. 基本上我想使用RequireJS模块配置功能。 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. 我的配置中的变量需要使用requireJS来评估。
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. 对我来说,在RequireJS配置(加载模块所需的配置)和用户模块配置之间存在逻辑分离是有意义的。 But I am currently struggling to find this :( 但我目前正在努力找到这个:(
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. 对于这种解决方案,我希望模块依赖于“config”模块,您可以使用路径配置交换另一个模块。 So if "bar" needed some config, "bar.js" would look like: 因此,如果“bar”需要一些配置,“bar.js”将如下所示:
define(['barConfig'], function (config) {
});
Then barConfig.js could have your other dependencies: 然后barConfig.js可能有你的其他依赖项:
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: 然后,如果您需要不同的配置,例如,生产与开发,请使用paths config将barConfig映射到其他值:
requirejs.config({
paths: {
barConfig: 'barConfig-prod'
}
});
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;
});
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.. 我只需要两次requireJS(...),首先创建配置,然后再使用配置加载应用程序模块。
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中
# bar.coffee
define ['module'], (module) ->
# config is now available in this module
console.log(module.config().key)
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()
. 关于@jrburke所说的内容,我发现以下模式非常有用:在调用require.config()
之前,在main.js
定义一个配置模块及其依赖项。
main.js main.js
define('config', ['crypto'], function (crypto) {
return {
'bar': {
key: crypto.randomBytes(64)
},
};
});
requirejs.config({
deps: ['app'],
});
app.js app.js
require(['config'], function (config){
// outputs value of: crypto.bar.key
console.log(config.bar.key);
});
Plnkr Demo: http://plnkr.co/edit/I35bEgaazEAMD0u4cNuj Plnkr演示: http ://plnkr.co/edit/I35bEgaazEAMD0u4cNuj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.