简体   繁体   English

避免导入“regenerator-runtime/runtime”

[英]avoiding the import "regenerator-runtime/runtime"

I am using Parcel to bundle my project and jest to run my tests.我正在使用 Parcel 来捆绑我的项目并开玩笑地运行我的测试。

Everything works fine, but on tests that I have the async/await keyword, I had to import regenerator-runtime/runtime一切正常,但在我有async/await关键字的测试中,我不得不导入regenerator-runtime/runtime

Like this:像这样:

import "regenerator-runtime/runtime"

test("read armored key", async() => {

})

And this work.而这项工作。

But without this import ( import "regenerator-runtime/runtime" ) I got this error message:但是没有这个导入( import "regenerator-runtime/runtime" )我收到了这个错误信息:

ReferenceError: regeneratorRuntime is not defined

How can I change my project to run without this import on tests with async?如何在异步测试中更改我的项目以在没有此导入的情况下运行?

Example: https://github.com/skhaz/parcel-regeneratorRuntime-is-not-defined示例: https://github.com/skhaz/parcel-regeneratorRuntime-is-not-defined

As I know that you can configure @babel/preset-env to compile against current node version which is described here then it should work:据我所知,您可以配置@babel/preset-env以针对此处描述的当前节点版本进行编译,然后它应该可以工作:

.babelrc

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      }
    ]
  ]
}

Depending its version, not all features available in your browser runtime will work in the Node runtime.根据其版本,并非浏览器运行时中可用的所有功能都可以在 Node 运行时中使用。 Promises (with await/async ) are supported in current versions of node, but since you are using Parcel, which by default uses Babel , your async/await calls will be compiled to use regenerator-runtime , a polyfill for that functionality.当前版本node 支持 Promise(带有await/async ),但是由于您使用的是 Parcel,默认情况下使用 Babel ,因此您的async/await调用将被编译为使用regenerator-runtime ,这是该功能的 polyfill。 You can either import "regenerator-runtime/runtime" in every entry file (not recommended if you don't need the polyfill,).您可以在 每个条目文件import "regenerator-runtime/runtime" (如果您不需要 polyfill,则不推荐)。 or you can tell babel what your runtime is.或者你可以告诉 babel 你的运行时是什么。

You should be able to get it to work with the @babel/preset-env preset, configured like so in your .babelrc :你应该能够让它与@babel/preset-env预设一起工作,在你的.babelrc中配置如下:

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "10" // the target node version, boolean true, or "current".
        }
      }
    ]
  ]
}

See this Medium article for more information about configuring babel for this.有关为此配置 babel 的更多信息,请参阅这篇Medium 文章


Opinion :意见
Don't rely on zero-configuration tools like Parcel: they end up adding to your development time by creating unexpected behavior (like your issue), or you have to spend time learning how it works.不要依赖像 Parcel 这样的零配置工具:它们最终会通过创建意外行为(如您的问题)来增加您的开发时间,或者您必须花时间学习它的工作原理。 It's enough to debug your own application;调试自己的应用程序就足够了; you shouldn't have to debug your build tool as well.您也不必调试构建工具。

When using parcel, According to this article add the below to package.json.使用包裹时,根据 本文将以下内容添加到 package.json。 It worked for me:它对我有用:

"browserslist": [
  "since 2017-06"
]

or或者

"browserslist": [
  "last 3 Chrome versions"
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM