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

标签 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 但这也不起作用。如果有人能指出我正确的方向,我会很高兴。

最佳答案

在同一个文件中使用两个 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/

相关文章:

ios - Swift 如何在标签中的文本周围添加背景颜色?

MySQL 用户定义变量与 COUNT 一起使用时返回错误结果

Excel:如何根据数据表创建动态数据验证列表

c# - 当我只有泛型类型和实例值时,如何手动构建表达式树?

javascript - 如何制作分类整理器?

javascript - 如何将数据传递给 Vue JS 组件?

python - 在同一行打印字符串和变量

javascript - 我可以假设 DOM 在动态加载的 JS 脚本上还没有准备好吗?

javascript - jasmine.matchersUtil.equals 与 ===

php - 带有 PHP 文件的 JQuery AJAX 缓存