Babel 6 regeneratorRuntime is not defined

前端 未结 30 1870
暖寄归人
暖寄归人 2020-11-22 03:49

I\'m trying to use async, await from scratch on Babel 6, but I\'m getting regeneratorRuntime is not defined.

.babelrc file

{
    \"presets\": [ \"es2         


        
30条回答
  •  [愿得一人]
    2020-11-22 04:17

    If you are building an app, you just need the @babel/preset-env and @babel/polyfill:

    npm i -D @babel/preset-env
    npm i @babel/polyfill
    

    (Note: you don't need to install the core-js and regenerator-runtime packages because they both will have been installed by the @babel/polyfill)

    Then in .babelrc:

    {
      "presets": [
        [
          "@babel/preset-env",
          {
            "useBuiltIns": "entry"  // this is the key. use 'usage' for further codesize reduction, but it's still 'experimental'
          }
        ]
      ]
    }
    

    Now set your target environments. Here, we do it in the .browserslistrc file:

    # Browsers that we support
    
    >0.2%
    not dead
    not ie <= 11
    not op_mini all
    

    Finally, if you went with useBuiltIns: "entry", put import @babel/polyfill at the top of your entry file. Otherwise, you are done.

    Using this method will selectively import those polyfills and the 'regenerator-runtime' file(fixing your regeneratorRuntime is not defined issue here) ONLY if they are needed by any of your target environments/browsers.

提交回复
热议问题