Babel 6 regeneratorRuntime is not defined

前端 未结 30 1849
暖寄归人
暖寄归人 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:28

    Update: The Babel 7 post also has a more in-depth answer.


    Babel 7.4.0 or later (core-js 2 / 3)

    As of Babel 7.4.0, @babel/polyfill is deprecated.

    In general, there are two ways to install polyfills/regenerator: via global namespace (Option 1) or as ponyfill (Option 2, without global pollution).


    Option 1: @babel/preset-env

    presets: [
      ["@babel/preset-env", {
        "useBuiltIns": "usage",
        "corejs": 3, // or 2,
        "targets": {
            "firefox": "64", // or whatever target to choose .    
        },
      }]
    ]
    

    will automatically use regenerator-runtime and core-js according to your target. No need to import anything manually. Don't forget to install runtime dependencies:

    npm i --save regenerator-runtime core-js
    

    Alternatively, set useBuiltIns: "entry" and import it manually:

    import "regenerator-runtime/runtime";
    import "core-js/stable"; // if polyfills are also needed
    

    Option 2: @babel/transform-runtime with @babel/runtime

    This alternative has no global scope pollution and is suitable for libraries.

    {
      "plugins": [
        [
          "@babel/plugin-transform-runtime",
          {
            "regenerator": true,
            "corejs": 3 // or 2; if polyfills needed
            ...
          }
        ]
      ]
    }
    
    Install it:
    npm i -D @babel/plugin-transform-runtime
    npm i @babel/runtime
    

    If corejs polyfill is used, you replace @babel/runtime with @babel/runtime-corejs2 (for "corejs": 2) or @babel/runtime-corejs3 (for "corejs": 3).

    0 讨论(0)
  • 2020-11-22 04:28

    My working babel 7 boilerplate for react with regenerator runtime:

    .babelrc

    {
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": {
              "node": true,
            },
          },
        ],
        "@babel/preset-react",
      ],
      "plugins": [
        "@babel/plugin-syntax-class-properties",
        "@babel/plugin-proposal-class-properties"
      ]
    }
    

    package.json

    ...
    
    "devDependencies": {
      "@babel/core": "^7.0.0-0",
      "@babel/plugin-proposal-class-properties": "^7.4.4",
      "@babel/plugin-syntax-class-properties": "^7.2.0",
      "@babel/polyfill": "^7.4.4",
      "@babel/preset-env": "^7.4.5",
      "@babel/preset-react": "^7.0.0",
      "babel-eslint": "^10.0.1",
    ...
    

    main.js

    import "@babel/polyfill";
    
    ....
    
    0 讨论(0)
  • 2020-11-22 04:29

    New Answer Why you follow my answer ?

    Ans: Because I am going to give you a answer with latest Update version npm project .

    04/14/2017

    "name": "es6",
     "version": "1.0.0",
       "babel-core": "^6.24.1",
        "babel-loader": "^6.4.1",
        "babel-polyfill": "^6.23.0",
        "babel-preset-es2015": "^6.24.1",
        "webpack": "^2.3.3",
        "webpack-dev-server": "^2.4.2"
    

    If your Use this version or more UP version of Npm and all other ... SO just need to change :

    webpack.config.js

    module.exports = {
      entry: ["babel-polyfill", "./app/js"]
    };
    

    After change webpack.config.js files Just add this line to top of your code .

    import "babel-polyfill";
    

    Now check everything is ok. Reference LINK

    Also Thanks @BrunoLM for his nice Answer.

    0 讨论(0)
  • 2020-11-22 04:31

    Alternatively, if you don't need all the modules babel-polyfill provides, you can just specify babel-regenerator-runtime in your webpack config:

    module.exports = {
      entry: ['babel-regenerator-runtime', './test.js'],
    
      // ...
    };
    

    When using webpack-dev-server with HMR, doing this reduced the number of files it has to compile on every build by quite a lot. This module is installed as part of babel-polyfill so if you already have that you're fine, otherwise you can install it separately with npm i -D babel-regenerator-runtime.

    0 讨论(0)
  • 2020-11-22 04:32

    Update

    It works if you set the target to Chrome. But it might not work for other targets, please refer to: https://github.com/babel/babel-preset-env/issues/112

    So this answer is NOT quite proper for the original question. I will keep it here as a reference to babel-preset-env.

    A simple solution is to add import 'babel-polyfill' at the beginning of your code.

    If you use webpack, a quick solution is to add babel-polyfill as shown below:

    entry: {
        index: ['babel-polyfill', './index.js']
    }
    

    I believe I've found the latest best practice.

    Check this project: https://github.com/babel/babel-preset-env

    yarn add --dev babel-preset-env
    

    Use the following as your babel configuration:

    {
      "presets": [
        ["env", {
          "targets": {
            "browsers": ["last 2 Chrome versions"]
          }
        }]
      ]
    }
    

    Then your app should be good to go in the last 2 versions of Chrome browser.

    You can also set Node as the targets or fine-tune the browsers list according to https://github.com/ai/browserslist

    Tell me what, don't tell me how.

    I really like babel-preset-env's philosophy: tell me which environment you want to support, do NOT tell me how to support them. It's the beauty of declarative programming.

    I've tested async await and they DO work. I don't know how they work and I really don't want to know. I want to spend my time on my own code and my business logic instead. Thanks to babel-preset-env, it liberates me from the Babel configuration hell.

    0 讨论(0)
  • 2020-11-22 04:32

    for future reference :

    As of Babel version 7.0.0-beta.55 stage presets have been removed

    refer blog https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets

    async await can be still be used by

    https://babeljs.io/docs/en/babel-plugin-transform-async-to-generator#usage

    installation

    npm install --save-dev @babel/plugin-transform-async-to-generator
    

    usage in .babelrc

     { 
         "presets": ["@babel/preset-env"],
         "plugins": ["@babel/plugin-transform-async-to-generator"]
     }
    

    and using babel polyfill https://babeljs.io/docs/en/babel-polyfill

    installation

    npm install --save @babel/polyfill
    

    webpack.config.js

    module.exports = {
      entry: ["@babel/polyfill", "./app/js"],
    };
    
    0 讨论(0)
提交回复
热议问题