Babel 6 regeneratorRuntime is not defined

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

提交回复
热议问题