[英]Module loading order in requirejs
We ran into an interesting problem in an existing project, 我们在一个现有项目中遇到了一个有趣的问题,
babel-polyfill
as an AMD module to the project. babel-polyfill
作为该项目的AMD模块。 r.js
r.js
进行了优化和捆绑 Our main file looks something like this : // main.js
我们的主文件如下所示://
main.js
require(
[
'router',
'someOtherModule', /* In reality we have quite a few more here */
], function(Router, AppModule) {/*... app code ...*/}
)
So we'd like to load this polyfill
module before any other module is loaded in main.js
所以我们想加载此
polyfill
的任何其他模块在加载之前模块main.js
What won't work : 什么不起作用:
router
- in the minified code that r.js spits out, there's no guarantee that polyfill
will actually be in the code before router
, it's not the defined behavior and thus cannot be counted on. router
-这种r.js吐出来,但也不能保证该缩小的代码polyfill
实际上是在代码前router
,它没有规定的行为,因此不能指望。 require['polyfill']
call seems to screw up the r.js optimizer, it won't seem the bundle together the other modules if they're wrapped in require[]
. require['polyfill']
调用包装所有内容似乎使r.js优化器搞砸了,如果将其他模块包装在require[]
,则似乎不会将它们捆绑在一起。 polyfill
is an AMD module, we can't just include it in the <HEAD>
polyfill
是AMD模块,因此我们不能仅将其包含在<HEAD>
Option 3 is still something we're investigating to see if it's possible. 选项3仍在研究中,以查看是否可行。
So the question is - Is what we're trying to do possible? 所以问题是-我们正在尝试做些什么?
Whenever I load polyfills I always always always load them in a script
element in head
. 每当我加载polyfills我永远永远永远载入他们在一个
script
在元素head
。 I never load them through RequireJS. 我从不通过RequireJS加载它们。 Loading them through RequireJS gives rise to all kinds of problems, as you discovered.
正如您所发现的,通过RequireJS加载它们会引起各种问题。 You wrote:
你写了:
Since the polyfill is an AMD module, we can't just include it in the
<HEAD>
.由于polyfill是AMD模块,因此我们不能仅将其包含在
<HEAD>
。
The babel-polyfill
package builds itself into a script that can be loaded through a script
element by using Browserify: babel-polyfill
软件包将自身构建为脚本,可以使用Browserify通过script
元素加载该script
:
#!/bin/sh
set -ex
BROWSERIFY_CMD="../../node_modules/browserify/bin/cmd.js"
UGLIFY_CMD="../../node_modules/uglify-js/bin/uglifyjs"
mkdir -p dist
node $BROWSERIFY_CMD lib/index.js \
--insert-global-vars 'global' \
--plugin bundle-collapser/plugin \
--plugin derequire/plugin \
>dist/polyfill.js
node $UGLIFY_CMD dist/polyfill.js \
--compress keep_fnames,keep_fargs,warnings=false \
--mangle keep_fnames \
>dist/polyfill.min.js
If you inspect dist/polyfill.js
, you'll see it is not an AMD module. 如果检查
dist/polyfill.js
,则会看到它不是AMD模块。 (The define
function it calls is not AMD's define
function.) (该
define
函数调用不是AMD的define
功能。)
You should be able to adapt this method to your whatever subset of babel-polyfill
you use. 您应该能够使此方法适应您使用的
babel-polyfill
任何子集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.