web-dev-qa-db-fra.com

Error regeneratorRuntime n'est pas défini avec Babel 7

Je souhaite mettre à jour ma configuration Babel vers la version 7 du projet Electron.

J'ai ajouté tous mes plugins dont j'avais besoin:

"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",

La compilation est bonne, mais quand Electron exécute mon main.js (compilé), j'ai cette erreur:

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

J'ai essayé d'installer le module regeneratorRuntime, mais aucun résultat.

6
s-leg3ndz

Vous devez importer le Babel Polyfill dans votre code:

import "@babel/polyfill";
13

Je devais ajouter @babel/plugin-transform-runtime et définir 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

0
codeBelt