baao
baao

Reputation: 73251

How to prevent babel from transpiling generator functions

I have kind of a weird problem with babel. When I use a simple generator function in one of my classes, babel creates a function out of it containing a call to regeneratorRuntime.

var marked3$0 = [getQueryDummy].map(regeneratorRuntime.mark);
function getQueryDummy(start, end, step) {
    return regeneratorRuntime.wrap(function getQueryDummy$(context$4$0) {

Bad thing is, it doesn't create this function which always results in an error when I forget to manually replace the compiled generator with the original one (which happens all the time)

I know I can add

require('babel/polyfill')

to my file. The polyfill holds the regeneratorRuntime function. And here is where it's getting really weird. Even though I place the require(...) at the very top of the file, babel calls regeneratorRuntime before the polyfill is included, which again leads to the same error.

For completeness sake, here's the generator

function *getQueryDummy(start, end, step) {
  while (start < end) {
    yield [start, '@dummy'];
      start += step;
  }
}

I'm using babel version 5.8.23.

Is there a way to tell babel to not touch generators at all? node supports them natively and there's no need for me to compile it...

Upvotes: 6

Views: 2020

Answers (2)

Pranay Dutta
Pranay Dutta

Reputation: 2591

We can also use exclude https://babeljs.io/docs/en/babel-preset-env#exclude

And make use of .babelrc

Upvotes: 0

Amit
Amit

Reputation: 46341

You could blacklist regenerator. If you're building with transform:

babel.transform(code, {blacklist:['regenerator']});

Or from command line with:

--blacklist regenerator

Upvotes: 7

Related Questions