我通过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 配置部分,我所做的是:
-
只需使用静态信息调用
require.config
(或requirejs.config
)一次。配置不包含任何变量。我r.js
指出这个静态信息。 -
在运行时,我至少有一个额外的调用来设置要计算的值的
require.config
。RequireJS 将多个require.config
调用合并到一个配置中。
r.js
将仅使用它在文件中识别的第一个配置。因此,您可以将单个requirejs.config
调用拆分为静态和动态部分,并将它们保留在同一文件中。