Require.js路径中的优化器和变量

时间:2013-08-27 09:56:37

标签: javascript variables dynamic requirejs

我在让r.js以我们需要的方式工作时遇到了问题。

我有以下问题:我们有2个域(例如foo.de和bar.de)和不同的环境。根据他们运行的环境和域,他们需要从其原始服务器加载不同的文件。我最初的解决方案是:

// channelDomain and environmentPath get defined above this script
require.config({
  paths: {
    'fooscript': channelDomain+environmentPath
  }
}

在未经优化的浏览器中对此进行测试完全符合,但夜间构建主张:

[Error: Error: The config in mainConfigFile /absolute/path/app/public/js/main.js 
cannot be used because it cannot be evaluated correctly while running in the 
optimizer. Try only using a config that is also valid JSON, or do not use 
mainConfigFile and instead copy the config values needed into a build file or 
command line arguments given to the optimizer.
Source error from parsing: /absolute/path/app/public/js/main.js: ReferenceError:
channelDomain is not defined

我尝试过很多事情,但我的想法已经不多了。我尝试在构建文件中执行empty:thing,但这也不起作用。如果有人能指出我正确的方向,我会很高兴。

1 个答案:

答案 0 :(得分:2)

在同一文件中使用两个require.config。优化器只会读取第一个,正如James在此处所说https://github.com/jrburke/r.js/issues/270#issuecomment-13112859,优化后它将在浏览器中运行。

所以最后你会在main.js中有这样的东西:

require.config({
    //only configurations needed for the transpiler's optimization
});

require.config({
  paths: {
    'fooscript': channelDomain+environmentPath
  }
});
相关问题