0

我正在为基于多页 requirejs 的应用程序使用以下架构。: https ://github.com/requirejs/example-multipage-shim

该存储库解释了如何通过运行 r.js(用于此类任务的命令行工具)来优化应用程序。

一切都应该可以正常工作,但我的项目作为一些具有依赖项的模块执行 HTTP GET 请求以从服务器获取数据(可以是文本或 json)

这是因为我的某些页面使用的一些 json 和模板需要在服务器端处理以进行本地化。

这是一个基本示例来说明我在说什么:

 define( function( require ){

    var appLang = require('json!/pagelang/login'),           //(a)
        loginTemplate = require('text!/template/login'),     //(b)
        module = require('app/model/user'),                  //(c)
        ....

向我的服务器 localhost/pagelang/login 发出 HTTP 获取请求,该请求返回 json

{
   "hash": "translated_value",
    ...
}     

这同样适用于template/template_name从服务器返回将其 UI 翻译成用户语言的 html。

通过运行 r.js,它会尝试为服务器上现有的目录目录加载这些位置,而这显然不存在。

Tracing dependencies for: app/main/login
Error: Error: Loader plugin did not call the load callback in the build:
json:
  json!/pagelang/login: Error: ENOENT, no such file or directory '/pagelang/login'
Module loading did not complete for: app/main/login

所以,我想阻止命令行工具优化文本!和json!模块。可能吗?

我检查了 requirejs 构建设置,但没有找到解决问题的方法。有什么帮助吗? https://github.com/jrburke/r.js/blob/master/build/example.build.js

4

1 回答 1

8

json 插件if (( config.isBuild && (config.inlineJSON === false || name.indexOf(CACHE_BUST_QUERY_PARAM +'=') !== -1)) || (url.indexOf('empty:') === 0)) {在优化器运行时使用,因此您有几个选择。

  • 添加构建配置选项inlineJSON: false
  • 添加!bust到 json 要求的末尾。require('json!/pagelang/login!bust'), 或者
  • 将路径添加到构建配置选项paths: { "/pagelang/login": "empty:" }

文本插件使用if (config.isBuild && !config.inlineText) {if (url.indexOf('empty:') === 0) {

  • 设置构建配置选项inlineText: false,或
  • 将路径添加到构建配置选项paths: { "/template/login": "empty:" }

==================================================== ================

更新:如果您无法选择inlineJSON工作,请尝试使用inlineText,这似乎也涵盖了 JSON。参考:https ://github.com/requirejs/r.js/blob/master/build/example.build.js

于 2014-05-06T02:47:09.183 回答