【问题标题】:Require.js optimizer and variables in pathsRequire.js 优化器和路径中的变量
【发布时间】:2023-03-18 16:19:01
【问题描述】:

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

【问题讨论】:

    标签: javascript variables dynamic requirejs


    【解决方案1】:

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

    【讨论】:

    • 不起作用,因为其他一些文件具有依赖关系 fooscript 并且优化器崩溃,没有给出这样的文件或目录,因为它不知道它存在的路径,因为优化器只读第一个require.config
    • 您可以在第一个配置中添加优化器路径、垫片或使其工作所需的任何内容。
    • 谢谢,这就是我最终得到的结果,尽管这似乎是一种 hacky 方式。
    • 当然,但是 James 是 requirejs 最重要的贡献者,所以我认为它是当今最好的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多