Babel - regeneratorRuntime is not defined, when using transform-async-to-generator plugin

前端 未结 2 1177
萌比男神i
萌比男神i 2020-12-20 15:10

I am not able to setup babel correctly for the usage of async / await.

I am using babel 7 and webpack 4.

I do not want to use babel-polyfill if possible!

相关标签:
2条回答
  • 2020-12-20 15:41

    You also need the transform-runtime plugin, so your .babelrc should ready:

    {
        "presets": [[
            "@babel/env",
            {"modules": false}
        ]],
        "plugins": [
          "syntax-dynamic-import",
          "transform-runtime",
          "transform-async-to-generator"
        ]
    }
    

    Note that you'll also need to npm install --save-dev transform-runtime

    0 讨论(0)
  • 2020-12-20 15:42

    The latest documentation here is pretty clear: https://babeljs.io/docs/en/next/babel-plugin-transform-runtime

    What worked for me is installing the two packages for build and for runtime (the final script for the browser):

    npm install --save-dev @babel/plugin-transform-runtime

    npm install --save @babel/runtime

    In the plugin Array of my webpack configuration I just added '@babel/plugin-transform-runtime' without any options. (Please also have a look at the documentation linked above, since some options (that you might find in older tutorials or answers) have been removed.)

    plugins: [
    '@babel/plugin-transform-runtime'
    ]
    

    I now can use async functions without errors, and it didn't add much code in the production build.

    0 讨论(0)
提交回复
热议问题