【问题标题】:r.js evaluates 'text' plugin despite on 'stubModules' param尽管有 'stubModules' 参数,r.js 仍会评估 'text' 插件
【发布时间】:2016-05-06 00:52:45
【问题描述】:

首先是一些代码:

我的 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 评估 'text' 插件,尽管在构建配置文件属性中使用了 'stubModules' 参数?

我以前读过这篇文章:

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

提前致谢。

【问题讨论】:

    标签: javascript node.js requirejs requirejs-optimizer


    【解决方案1】:

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

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

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

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

    如果您想要从捆绑包中排除通过text 加载的所有内容,您可以做的一件事是在构建配置中添加paths,将这些模块列为empty:。例如:

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

    但是你在运行时需要text插件来加载文本,所以你应该删除stubModules

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多