使用Babel 7未定义错误regeneratorRuntime

时间:2018-03-13 10:26:58

标签: node.js electron babel

我想在Electron项目中将我的Babel配置更新到版本7。

我已经添加了我需要的所有插件:

"devDependencies": {
    "@babel/cli": "^7.0.0-beta.40",
    "@babel/core": "^7.0.0-beta.40",
    "@babel/node": "^7.0.0-beta.40",
    "@babel/plugin-proposal-class-properties": "^7.0.0-beta.40",
    "@babel/plugin-proposal-decorators": "^7.0.0-beta.40",
    "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.40",
    "@babel/plugin-proposal-optional-chaining": "^7.0.0-beta.40",
    "@babel/plugin-transform-async-to-generator": "^7.0.0-beta.40",
    "@babel/polyfill": "^7.0.0-beta.40",
    "@babel/preset-env": "^7.0.0-beta.40",
    "@babel/preset-react": "^7.0.0-beta.40",
    "babel-eslint": "^7.1.1",

汇编很好,但是当Electron运行我的main.js(已编译)时,我发现了这个错误:

A JavaScript error occurred in the main process Uncaught Exception: ReferenceError: regeneratorRuntime is not defined

我尝试安装regeneratorRuntime模块,但没有结果。

3 个答案:

答案 0 :(得分:20)

您应该在代码中导入Babel Polyfill:

import "@babel/polyfill";

答案 1 :(得分:0)

我必须添加@babel/plugin-transform-runtime并设置runtimeHelpers: true

rollup.config.js

import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

const extensions = [
    '.js', '.jsx', '.ts', '.tsx',
];

const name = 'RollupTypeScriptBabel';

export default {
    input: './src/index.ts',

    // Specify here external modules which you don't want to include in your bundle (for instance: 'lodash', 'moment' etc.)
    // https://rollupjs.org/guide/en#external-e-external
    external: [...Object.keys(pkg.dependencies || {})],

    plugins: [
        // Allows node_modules resolution
        resolve({extensions}),

        // Allow bundling cjs modules. Rollup doesn't understand cjs
        commonjs(),

        // Compile TypeScript/JavaScript files
        babel({extensions, include: ['src/**/*'], runtimeHelpers: true}),
    ],

    output: [{
        file: pkg.main,
        format: 'cjs',
    }, {
        file: pkg.module,
        format: 'es',
    }],
};

.babelrc

{
    "presets": [
        "@babel/env",
        "@babel/typescript"
    ],
    "plugins": [
        "@babel/plugin-transform-runtime",
        "@babel/proposal-class-properties",
        "@babel/proposal-object-rest-spread"
    ]
}

https://github.com/codeBelt/generate-template-files

https://github.com/a-tarasyuk/rollup-typescript-babel

答案 2 :(得分:0)

对我来说,我在转译的代码中使用了此代码来成功运行:

require("@babel/polyfill");