2013-01-25 69 views
7

如果我在文档中遗漏了这些,请致歉。基本上我想使用RequireJS模块配置功能。我想集中管理给包中模块的配置值。配置依赖于RequireJS配置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配置(加载模块所需的配置)和用户的模块配置之间存在逻辑上的分离是有意义的。但我目前正在努力找到这个:(

回答

0

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

我只是做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

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

我认为这样做的正确方法是使配置模块...

// 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; 
}); 
+0

没错这就是我目前有,但它意味着我需要需要'配置'从每个模块。这也意味着我无法以集中方式为不同的模块指定不同的配置。我真的希望能够使用requireJS配置功能,但也许这是不可能的 – greTech

+0

检查我的编辑,全局变量是适当的,如果你使用的东西无处不在 – Felix

+0

不要感谢污染全局,即时通讯也使用Node.js :) – greTech

6

对于这种解决方案,我希望模块依赖于一个“配置”模块,您可以使用路径配置交换不同的模块。所以,如果“栏”需要一些配置,“bar.js”看起来像:

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

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

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

然后,如果你需要不同的CONFIGS为说,生产与开发,利用路径配置到barConfig映射到其他值:

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

Riffing什么@jrburke是说,我发现下面的模式是非常有用的:定义配置模块,并将其之前刚刚是在main.js依赖调用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演示:http://plnkr.co/edit/I35bEgaazEAMD0u4cNuj