需要动态设置的优化器和区域设置

Requirejs optimizer and locale setting that is set dynamically

本文关键字:设置 区域 优化 动态      更新时间:2023-09-26

我通过grunt使用requirejs优化器(r.js),这是我的requirejs配置:

requirejs.config
  baseUrl: '/scripts'
  locale: window.localStorage.getItem('locale') || null
  ...

问题是每次我尝试在我的requirejs配置中使用变量时,grunt r.js插件(https://github.com/gruntjs/grunt-contrib-requirejs)都会抛出错误。

The main config file 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.

您是否设法同时使用变量作为语言环境和 r.js?

您的locale设置仅在运行时获取实际值。对于只能在运行时给出值的 RequireJS 配置部分,我所做的是:

  1. 只需使用静态信息调用require.config(或requirejs.config)一次。配置不包含任何变量。我r.js指出这个静态信息。

  2. 在运行时,我至少有一个额外的调用来设置要计算的值的require.config。RequireJS 将多个require.config调用合并到一个配置中。

r.js将仅使用它在文件中识别的第一个配置。因此,您可以将单个requirejs.config调用拆分为静态和动态部分,并将它们保留在同一文件中。