javascript 如何防止 Require.js 优化器在优化文件中包含文本插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10196977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:00:08  来源:igfitidea点击:

How can I prevent the Require.js optimizer from including the text plugin in optimized files?

javascriptoptimizationrequirejs

提问by nrabinowitz

tl;dr: How do I keep the text.js plugin out of my optimized file when all my text dependencies are inlined?

tl; dr:当我的所有文本依赖项都被内联时,如何将 text.js 插件排除在我的优化文件之外?

I'm using the Require.js optimizer(via Node) to optimize some of the JS files in my project. I'm using the text pluginto load text dependencies (HTML templates, CSS). I've got a module I want to optimize, including its dependencies, like this:

我正在使用Require.js 优化器(通过 Node)来优化我项目中的一些 JS 文件。我正在使用文本插件加载文本依赖项(HTML 模板、CSS)。我有一个要优化的模块,包括它的依赖项,如下所示:

define(['text!core/core.css'], function(styles) {
    // do setup stuff, return an object
});

The Require.js docs say that the core/core.cssfile will be inlined when I run the r.jsoptimizer, which I'm invoking like this:

Require.js 文档说core/core.css当我运行r.js优化器时,文件将被内联,我像这样调用:

$ r.js -o baseUrl=./src name=core out=release/test.js

Tracing dependencies for: core
Uglifying file: c:/path/release/test.js

c:/path/release/test.js
----------------
c:/path/src/text.js
text!core/core.css
c:/path/src/core.js

The good news is, this works. When I look at the optimized file, I can see the inlined text, something like this:

好消息是,这是有效的。当我查看优化文件时,我可以看到内联文本,如下所示:

define("text!core/core.css",[],function(){return"some CSS text"}),
define("core",["text!core/core.css"],function(a){ ... })

The bad news is, the text.js plugin is also included - it adds about 3K, and consists of (as far as I can tell) now entirely unnecessary code for loading external text files. I know 3K isn't much, but I'm trying to keep my code highly optimized, and as far as I understand the code for the text plugin is not at all necessary if my text dependencies are inlined. I can keep the text plugin out by adding exclude=textto my r.jscall, but if I do, I get an error when I try to use the optimized code in the browser saying the text.js plugin couldn't be loaded.

坏消息是,text.js 插件也包括在内——它增加了大约 3K,并且包含(据我所知)现在完全不必要的用于加载外部文本文件的代码。我知道 3K 并不多,但我正在努力保持我的代码高度优化,据我所知,如果我的文本依赖项被内联,则文本插件的代码根本没有必要。我可以通过添加exclude=text到我的r.js调用中来保留文本插件,但是如果我这样做了,当我尝试在浏览器中使用优化的代码时,我会收到一个错误,说无法加载 text.js 插件。

So:

所以:

  1. Is there any reason the text.js plugin is actually requiredhere?

  2. If not, is there a configuration optionfor r.jsthat can fix this behavior, or

  3. Is there an easy shimfor the text.js plugin that I can include to convince Require.js that the unnecessary plugin is loaded?

  1. 这里实际上需要text.js 插件有什么原因吗?

  2. 如果没有,是否有一个配置选项r.js,可以解决此行为,或

  3. 我可以包含 text.js 插件的简单垫片以说服 Require.js 加载了不必要的插件吗?

采纳答案by Miller Medeiros

The text plugin is really required since RequireJS needs to check if the plugin implements the method normalizebefore retrieving the proper module ID.

确实需要文本插件,因为 RequireJS 需要normalize在检索正确的模块 ID 之前检查插件是否实现了该方法。

The way to remove the text plugin from the build is to use the onBuildWritesetting to create an empty plugin module as described on this issue comment: https://github.com/jrburke/r.js/issues/116#issuecomment-4185237- This feature should land on a future version of r.js

从构建中删除文本插件的方法是使用onBuildWrite设置创建一个空插件模块,如本问题评论所述:https: //github.com/jrburke/r.js/issues/116#issuecomment-4185237-这个特性应该会出现在 r.js 的未来版本中

Edit:

编辑:

r.js now have a setting called stubModulesthat does exactly that:

r.js 现在有一个名为的设置stubModules,它正是这样做的:

//Specify modules to stub out in the optimized file. The optimizer will
//use the source version of these modules for dependency tracing and for
//plugin use, but when writing the text into an optimized layer, these
//modules will get the following text instead:
//If the module is used as a plugin:
// define({load: function(id){throw new Error("Dynamic load not allowed: " + id);}});
//If just a plain module:
// define({});
//This is useful particularly for plugins that inline all their resources
//and use the default module resolution behavior (do *not* implement the
//normalize() method). In those cases, an AMD loader just needs to know
//that the module has a definition. These small stubs can be used instead of
//including the full source for a plugin.
stubModules : ['text']

For more r.js options check the example.build.jsfile.

有关更多 r.js 选项,请检查example.build.js文件。