如何使用支持IE11的Babel启用异步/等待 [英] How to enable async/await with babel with support for IE11

查看:756
本文介绍了如何使用支持IE11的Babel启用异步/等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在源代码中使用async/await,并通过babel将其转换为>0.25% not dead可用的内容.

I am hoping to use async/await in my source code and have it transpiled by babel to something useable by >0.25% not dead.

我的脑子里有各种各样的攻击方法.有些已被弃用,有些则无法使用,而我已经工作了超过一倍的图书馆.

My head is spinning with the plethora of ways to attack this. Some are deprecated, some flat out don't work, and the one that I have gotten to work more than doubles the size of my library.

我尝试将@babel/polyfill@babel/plugin-transform-async-to-generator结合使用,并且效果很好,但是该库从〜500kB变为〜1.1MB.

I've tried using @babel/polyfill with @babel/plugin-transform-async-to-generator and it works well, but the library goes from ~500kB to ~1.1MB.

我还尝试通过将其赋予>0.25% not dead的方式将其留给@babel/preset-env,但这并没有什么不同.我得到:

I also tried leaving it to @babel/preset-env by giving it >0.25% not dead, but that hasn't made a difference. I get:

regeneratorRuntime未定义

regeneratorRuntime is undefined

我希望有比包括所有这些regeneratorRuntime东西更好的方法了……

I'm hoping there is a better way to do this than including all this regeneratorRuntime stuff...

我宁愿回到毁灭的回调金字塔,也不愿将库超过1mb ...

I'd rather go back to the callback pyramid of doom than ship a library over 1mb...

我正在使用:

  • webpack 4.41.0
  • babel 7.6.2

推荐答案

如果您只需要生成器polyfill(异步/等待需要),那么您可以使用

If you only need the generator polyfill — which is needed for async/await — then you can just use facebook/regenerator.

您可以按照以下步骤在IE 11中支持异步/等待:

You could follow the steps to support async/await in IE 11:

  • 使用 babel-preset-env
  • yarn add regeneratornpm install regenerator
  • node_modules/regenerator-runtime/runtime.js (10.7kb minified)添加到您的捆绑包中
  • use babel-preset-env
  • yarn add regenerator or npm install regenerator
  • add node_modules/regenerator-runtime/runtime.js (10.7kb minified) into your bundle

参考链接:通过3个简单的步骤为您的Webapp添加ES7异步/等待支持

这篇关于如何使用支持IE11的Babel启用异步/等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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