鍍金池/ 問答/iOS  網(wǎng)絡(luò)安全  HTML/ 使用 babel-polyfill 為什么要使用 babel-plugin-tr

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

想在 node 中使用 Generator, 添加添加 babel-polyfill 后發(fā)現(xiàn)還是無法使用,加上 babel-plugin-transform-runtime 后沒有問題了。

我想知道原因是什么,我認(rèn)為 babel-plugin-transform-runtime 應(yīng)該和 babel-runtime 配合

回答
編輯回答
小眼睛

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 提供的功能創(chuàng)建沙盒環(huán)境。

示例中還有個配置,看樣子是可以避免創(chuàng)建 polyfill 沙盒的:

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

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

2017年8月27日 13:47