Javascript 当配置依赖于RequireJS时,使用RequireJS配置模块

Javascript 当配置依赖于RequireJS时,使用RequireJS配置模块,javascript,node.js,requirejs,Javascript,Node.js,Requirejs,如果我在文档中遗漏了这一点,我深表歉意。基本上我想使用RequireJS模块配置特性。我希望集中管理提供给包中模块的配置值 这是文档中的一个示例: requirejs.config({ config: { 'bar': { size: 'large' }, 'baz': { color: 'blue' } } }); //bar.js, which uses simp

如果我在文档中遗漏了这一点,我深表歉意。基本上我想使用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配置(加载模块所需的配置)和用户的模块配置之间进行逻辑分离是有意义的。但我目前正在努力寻找:(

我认为正确的方法是制作一个配置模块

// 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调用:

require(['config', 'main']);
然后,您可以使用它,而无需始终将其添加到定义中:

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

经过仔细考虑,我想出了一个解决办法。它并不特别漂亮,但似乎确实有效

我只需执行两次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()
在酒吧里喝咖啡

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

对于这种解决方案,我会让模块依赖于一个“config”模块,您可以使用path config将其替换为另一个模块。因此,如果“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'
  }
});

根据@jrburke的说法,我发现以下模式非常有用:在调用
require.config()
之前,在
main.js
中定义配置模块及其依赖项

main.js

define('config',['crypto'],函数(crypto){
返回{
“酒吧”:{
密钥:加密随机字节(64)
},
};
});
requirejs.config({
部门:[“应用程序”],
});
app.js

require(['config'],函数(config){
//输出:crypto.bar.key的值
log(config.bar.key);
});

Plnkr演示:

是的,这是我目前拥有的,但这意味着我需要从每个模块中获取“配置”。这也意味着我不能集中地为不同的模块指定不同的配置。我真的希望利用requireJS配置功能,但可能不可能。请退出我的编辑,全局是应用程序如果你在任何地方都在使用某个东西,那就合适了啊,没错!有时很难说:p你的解决方案只是用“module”替换了“config”,但也用了一种更复杂的方式。在需要配置设置的任何地方,你都必须在定义中使用“module”。你可以使用我的解决方案,但将window.config={…}更改为module.config={…}(模块真的是nodejs中的全局名称空间?)