0

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

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

问题是 grunt r.js 插件 ( https://github.com/gruntjs/grunt-contrib-requirejs ) 每次我尝试在我的 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?

4

1 回答 1

1

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

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

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

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

于 2015-01-30T11:31:38.963 回答