如果我在文档中遗漏了这一点,我深表歉意。基本上我想使用 RequireJS 模块配置功能。我想集中管理包中模块的配置值。
这是文档中的示例:
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;
});
我的问题是我的配置信息会稍微复杂一些,并且本身会有依赖性。我想这样做:
requirejs.config({
config: {
'bar': {
path: path.dirname(module.uri)
key: crypto.randomBytes(64)
},
}
});
我的配置中的变量需要使用 requireJS 来评估。
对我来说,在 RequireJS 配置(加载模块所需的配置)和用户的模块配置之间进行逻辑分离是有意义的。但我目前正在努力寻找这个:(
最佳答案
对于这种解决方案,我会让模块依赖于一个“配置”模块,您可以使用路径配置将其交换为不同的模块。因此,如果“bar”需要一些配置,“bar.js”将如下所示:
define(['barConfig'], function (config) {
});
然后 barConfig.js 可能有你的其他依赖:
define(['crypto'], function (crypto) {
return {
key: crypto.randomBytes(64)
}
});
然后,如果您需要不同的配置,比如生产与开发,请使用路径配置将 barConfig 映射到其他值:
requirejs.config({
paths: {
barConfig: 'barConfig-prod'
}
});
关于javascript - 当配置依赖于 RequireJS 时使用 RequireJS 配置模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14518569/