使用 babel-polyfill 为什么要使用 babel-plugin-transform-runtime

想在 node 中使用 Generator, 添加添加 babel-polyfill 后发现还是无法使用,加上 babel-plugin-transform-runtime 后没有问题了。

我想知道原因是什么,我认为 babel-plugin-transform-runtime 应该和 babel-runtime 配合

阅读 4.9k
1 个回答

https://babeljs.io/docs/plugi...

Another purpose of this transformer is to create a sandboxed environment for your code. If you use babel-polyfill and the built-ins it provides such as Promise, Set and Map, those will pollute the global scope. While this might be ok for an app or a command line tool, it becomes a problem if your code is a library which you intend to publish for others to use or if you can’t exactly control the environment in which your code will run.

就是说它会为 babel-polyfill 提供的功能创建沙盒环境。

示例中还有个配置,看样子是可以避免创建 polyfill 沙盒的:

{
  "plugins": [
    ["transform-runtime", {
      "helpers": false,
      "polyfill": false,
      "regenerator": true,
      "moduleName": "babel-runtime"
    }]
  ]
}

至于为什么添加 babel-polyfill 后无法使用……你到底是怎么添加的呢?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题