Babel 6 regeneratorRuntime is not defined

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

    Most of these answers recommend solutions for dealing with this error using WebPack. But in case anyone is using RollUp (like I am), here is what finally worked for me (just a heads-up and bundling this polyfill ads about 10k tot he output size):

    .babelrc

    {
        "presets": [
            [
                "env",
                {
                    "modules": false,
                    "targets": {
                        "browsers": ["last 2 versions"]
                    }
                }
            ]
        ],
        "plugins": ["external-helpers",
            [
                "transform-runtime",
                {
                    "polyfill": false,
                    "regenerator": true
                }
            ]]
    }
    

    rollup.config.js

    import resolve from 'rollup-plugin-node-resolve';
    import babel from 'rollup-plugin-babel';
    import uglify from 'rollup-plugin-uglify';
    import commonjs from 'rollup-plugin-commonjs';
    
    
    export default {
        input: 'src/entry.js',
        output: {
            file: 'dist/bundle.js',
            format: 'umd',
            name: 'MyCoolLib',
            exports: 'named'
        },
        sourcemap: true,
        plugins: [
            commonjs({
                // polyfill async/await
                'node_modules/babel-runtime/helpers/asyncToGenerator.js': ['default']
            }),
            resolve(),
            babel({
                runtimeHelpers: true,
                exclude: 'node_modules/**', // only transpile our source code
            }),
            uglify()
    
        ]
    };
    

提交回复
热议问题