如何防止Babel转换生成器功能 [英] How to prevent babel from transpiling generator functions

查看:92
本文介绍了如何防止Babel转换生成器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对babel有点奇怪的问题.当我在一个类中使用一个简单的生成器函数时,babel会在其中创建一个函数,其中包含对regeneratorRuntime的调用.

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)

我知道我可以添加

require('babel/polyfill')

到我的文件. polyfill具有regeneratorRuntime函数.这就是它变得很奇怪的地方.即使我放置了require(...) 在文件的最顶部,babel在包含polyfill之前调用regeneratorRuntime,这再次导致相同的错误.

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;
  }
}

我正在使用babel版本5.8.23.

I'm using babel version 5.8.23.

有没有办法告诉babel根本不要触摸generators?节点本身支持它们,而无需我进行编译...

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...

推荐答案

您可以黑名单 regenerator.如果您要使用transform:

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

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

或在命令行中输入:

--blacklist regenerator

这篇关于如何防止Babel转换生成器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆