我在让 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 但这也不起作用。如果有人能指出我正确的方向,我会很高兴。
最佳答案
在同一个文件中使用两个 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
}
});
关于javascript - Require.js 优化器和路径中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462325/