目标
- 语法: es6 -> es5
- api: promise、fetch等
工具 babel7
@babel/preset-env 会根据浏览器环境引入polyfill 主要参数见下 useBuiltIns: "entry" | "usage" | "false", 默认false
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": "3.7.0",
"targets": {
"browsers": [
"last 2 versions",
"ie >= 11"
]
}
}
],
"@babel/preset-react",
"@babel/preset-typescript"
]
插件: 箭头函数、Object.assign等
"plugins": [
"@babel/plugin-transform-arrow-functions",
"@babel/plugin-transform-object-assign",
"@babel/plugin-transform-modules-commonjs",
"@babel/plugin-transform-runtime"
]
fetch\promise
IE 11兼容fetch语法需要增加两个polyfill
- Promise polyfill promise-polyfill
- Fetch polyfill: whatwg-fetch A window.fetch JavaScript polyfill
import "promise-polyfill"
import "whatwg-fetch";
其他问题
有些库使用的es6 proxy(比如mobx5),babel是无法polyfill的,需要关注下
- Due to the limitations of ES5, Proxies cannot be transpiled or polyfilled