简体   繁体   English

什么决定了babel-transiled脚本中`require`调用的顺序?

[英]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: 对于需要我的模块的用户,我看到以下问题:

  • The user is in an ES2015 environment and transpiles his code with 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只需要一次,他根本无法使用我的模块。
  • If I thus choose not to include 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 ,因为它使用asyncawait

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.

相关问题 使用依赖项构建monorepo babel编译的节点JS应用程序 - Building monorepo babel-transpiled node JS application with dependencies 没有为nodeJS编译的babel定义regeneratorRuntime - regeneratorRuntime is not defined for nodeJS transpiled babel 无法用节点运行babel转换文件 - Can't run babel transpiled files with node Babel 7中的外部进口不会被转化 - External imports in Babel 7 do not get transpiled 如何判断 Babel 6 正在转译哪些文件? - How to tell which files are being transpiled by Babel 6? Mocha无法读取已转译的@ babel /注册码 - Mocha cannot read transpiled @babel/register code 是什么决定了使用promises或setTimeout的延迟函数的调用顺序? - What determines the call order of deferred function using promises or setTimeout? 运行 Babel 转译代码时需要 JSON 文件引发 Node.js 加载程序错误“错误:找不到模块'example.json'” - require a JSON file throws a Node.js loader error "Error: Cannot find module 'example.json'" when running Babel transpiled code 使用laravel-mix和webpack订购需要脚本/依赖项 - Order require scripts / dependencies with laravel-mix and webpack 如何从babel-core中取出转译的字符串? - How do I get the transpiled string out of babel-core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM