[英]What determines the order of `require` calls in babel-transpiled scripts?
So, my workflow up to this point was to put 所以,我到目前为止的工作流程就是这样
import "babel-polyfill";
when using features like async
/ await
to ask babel to include the regenerator runtime in the transpilation. 当使用像async
/ await
这样的功能来让babel在转换中包含再生器运行时。
I see the the following problems for users requiring my module: 对于需要我的模块的用户,我看到以下问题:
babel-polyfill
, too. 用户处于ES2015环境中,并使用babel-polyfill
polyfill转换代码。 Since babel-polyfill
can only be required once, he will not be able to use my module at all. 由于babel-polyfill
只需要一次,他根本无法使用我的模块。 babel-polyfill
, babel doesn't know that the module does require babel-polyfill
and won't respect that in the generated require
order (at least that's what I think happens). 如果我因此选择不包括babel-polyfill
,那么babel不知道该模块确实需要babel-polyfill
并且不会在生成的require
命令中尊重它(至少我认为发生这种情况)。 I've recently created an npm module that does not come with babel-polyfill
, but requires the user to include babel-polyfill
before calling require
on my npm module, since it uses async
and await
. 我最近创建了一个没有babel-polyfill
的npm模块,但要求用户在我的npm模块上调用require
之前包含babel-polyfill
,因为它使用async
和await
。
Thus, in my current project, I'd like to use my module like so in index.js
: 因此,在我当前的项目中,我想在index.js
使用我的模块:
import "babel-polyfill";
import Server from "./Server";
import foo from "bar";
import baz from "qux";
where Server
is a class that extends my module that requires babel-polyfill
. 其中Server
是一个扩展我的模块的类,需要babel-polyfill
。
However, the transpilation of index.js
starts like this: 但是, index.js
如下所示:
!function(e, r) {
if ("function" == typeof define && define.amd)
define(["bar", "qux", "./Server", "babel-polyfill"], r);
else if ("undefined" != typeof exports)
r(require("bar"), require("qux"), require("./Server"), require("babel-polyfill"));
// etc.
}();
Here, I can clearly see that ./Server
is required before babel-polyfill
, although my ES2015 import
syntax asks for the opposite. 在这里,我可以清楚地看到, 在 babel-polyfill
./Server
之前需要./Server
,尽管我的ES2015 import
语法要求相反。 In fact, the entire order is mixed up. 事实上,整个订单都是混乱的。
That's why I'm getting the error: 这就是我收到错误的原因:
ReferenceError: regeneratorRuntime is not defined
How can I tell babel to respect the order in my source? 如何告诉babel尊重我的来源订单?
From what I can tell, you can't tell Babel to respect the order of your source - it will always hoist the imports and evaluate everything else afterwards. 从我所知道的,你不能告诉Babel尊重你的来源的顺序 - 它将始终提升进口并在之后评估其他一切。 The only way seems to be switching to require when you want to ensure evaluation of some code (usually global assignments) first. 当您想要首先确保评估某些代码(通常是全局分配)时,唯一的方法似乎是切换到需要。 As per my answer here . 根据我在这里的答案。
Numerous issues raised (and closed/rejected) against Babel relating to this. 针对巴贝尔提出(并关闭/拒绝)与此相关的众多问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.