andrew
andrew

Reputation: 11

Is it possible to transpile generators with Babel?

I have simple code of generator:

function *foo(){}

I have a gulp babel which looks like this:

const gulp = require('gulp');
const babel = require('gulp-babel');

gulp.task('default', () => {
    return gulp.src('src/app.js')
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(gulp.dest('dist'));
});

When u run gulp it finished nice but when i go to html page where I'm adding my dist/app.js file i see an error in console:

ReferenceError: regeneratorRuntime is not defined

I've read about it but its recommended to add a plugin which handle generators and when I'm doing that I have an error that require is not defined... Can someone please tell how to make babel working well with generators step-by-step?

Upvotes: 1

Views: 1070

Answers (1)

Mulan
Mulan

Reputation: 135357

You need to include babel-polyfill in your build

Babel includes a polyfill that includes a custom regenerator runtime and core-js.

Install babel-polyfill

$ npm install --save-dev babel-polyfill

Make sure you add this to src/app.js

// src/app.js
import "babel-polyfill";

Upvotes: 3

Related Questions