javascript - 尽管 'text' 参数,r.js 仍评估 'stubModules' 插件

标签 javascript node.js requirejs requirejs-optimizer

首先是一些代码:

我的 r.js 启动文件,由 r.js.cmd -o static/js/boot.js 运行

({
    baseUrl: './',
    preserveLicenseComments: false,
    name: 'boot',
    stubModules: ['text'],
    mainConfigFile: './requirejs/config.js',
    out: 'build.min.js',
    //paths: {
    //    'text': 'plugins/requirejs.text'
    //},
})

然后插件在控制台中抛出异常:

Error: Error: Loader plugin did not call the load callback in the build:
text:
    text!langJSON: Error: ENOENT, no such file or directory 'C:\web\lang\main'
    text!/web/downloads/links.json: Error: ENOENT, no such file or directory 'C:\web\downloads\links.json'

有人可以回答我为什么 r.js 在构建配置文件属性中使用“stubModules”参数时评估“text”插件吗?

我之前读过这篇文章:

  1. How can I prevent the Require.js optimizer from including the text plugin in optimized files?
  2. Inlining require.js text! using Grunt

提前致谢。

最佳答案

stubModules 不会告诉优化器跳过通过 text 插件加载的所有内容。它只是告诉优化器在最终包中用以下代码替换插件:

define('text',{load: function(id){throw new Error("Dynamic load not allowed: " + id);}});

如果您要创建一个包含代码所需的每个模块的包,这非常有用。如果是这种情况,那么通过 text 加载的所有模块都将已经在 bundle 中,从而将 text 插件的代码包含在其中该 bundle 毫无意义,因为它不会被使用。 (在这种情况下,该插件在构建时使用,但不在运行时使用。)

您收到的错误是因为优化器仍然尝试将通过text加载的那些模块包含在您的包中,但它找不到文件。

如果您想要做的是从 bundle 中排除通过 text 加载的所有内容,您可以做的一件事就是向构建配置添加一个 path ,其中列出了这些内容模块为空:。例如:

paths: {
    'web/downloads/links.json': 'empty:',
    ...
}

但是您在运行时将需要 text 插件来加载文本,因此您应该删除 stubModules

关于javascript - 尽管 'text' 参数,r.js 仍评估 'stubModules' 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054489/

相关文章:

c++ - 以高效的方式从 Node.js 运行 C 或 C++ 代码

javascript - 滚动过去的元素时更改固定标题的背景

javascript - 我如何强制所有 <video> 内容内联播放

javascript - IIS为js文件返回text/html,为什么?

javascript - 从另一个js文件获取数据表行的引用

node.js - 如何将 "Auto Prefixer"合并到 "package.json"和 "grunt.js"中?

node.js - Angular 版本未显示 "ng --version"

node.js - nodejs-为什么setInterval突然停止工作?

jquery - 使用 shim 在 Backbone + Requirejs 中包含自定义 Jquery 插件

javascript - RequireJS 两次加载同一个模块以获得两个独立的命名空间