【问题标题】:Requirejs optimizer and locale setting that is set dynamicallyRequirejs 优化器和动态设置的语言环境设置
【发布时间】:2015-01-30 10:36:28
【问题描述】:

我正在通过 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?

【问题讨论】:

    标签: javascript gruntjs requirejs r.js grunt-contrib-requirejs


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 2013-05-24
      • 2011-09-20
      • 1970-01-01
      • 2016-09-07
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多