Babel
默认只转换新的Javascript
语法,而不转换新的API,比如 Array.find
方法,Babel就不会转码这个方法babel-polyfill
来转换等npm i @babel/polyfill
targets
表示我们需要支持哪些平台和哪些版本{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
targets: {
//"browsers": ["ie >= 8", "chrome >= 62"],// 推荐使用 .browserslist
"browsers": [">1%"]
}
}
}
}
babel-polyfill
它是通过向全局对象和内置对象的prototype
上添加方法来实现的。比如运行环境中不支持Array.prototype.find
方法,引入polyfill
, 我们就可以使用ES6
方法来编写了,但是缺点就是会造成全局空间污染@babel/preset-env
默认只支持语法转化,需要开启useBuiltIns
配置才能转化API和实例方法useBuiltIns
可选值包括:"usage" | "entry" | false, 默认为 false,表示不对polyfills
处理,这个配置是引入 polyfills 的关键useBuiltIns: false
此时不对 polyfill
做操作。如果引入 @babel/polyfill
,则无视配置的浏览器兼容,引入所有的 polyfill
src/index.js
import '@babel/polyfill';
let sum = (a, b) => a + b;
let promise = Promise.resolve();
console.log([1, 2, 3].find(item => item === 2));
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
devtool: false,
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
},
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
sourceType: "unambiguous",
+ presets: [["@babel/preset-env", { useBuiltIns: false }]]
}
}
}
]
},
plugins: []
};
import '@babel/polyfill'
,会自动根据 browserslist 替换成浏览器不兼容的所有 polyfillcorejs
默认是2,corejs: 3
, 则import '@babel/polyfill'
需要改成 import 'core-js/stable';import 'regenerator-runtime/runtime';
corejs
默认是2 npm install --save core-js@2 npm install --save core-js@3
src\index.js core-js@2
import '@babel/polyfill';
let sum = (a, b) => a + b;
let promise = Promise.resolve();
console.log([1, 2, 3].find(item => item === 2));
core-js@3
import 'core-js/stable';
import 'regenerator-runtime/runtime';
let sum = (a, b) => a + b;
let promise = Promise.resolve();
console.log([1, 2, 3].find(item => item === 2));
{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
+ presets: [["@babel/preset-env", { useBuiltIns: 'entry', corejs: { version: 3 } }]]
}
}
}
WARNING (@babel/preset-env): We noticed you're using the `useBuiltIns` option without declaring a core-js version. Currently, we assume version 2.x when no version is passed. Since this default version will likely change in future versions of Babel, we recommend explicitly setting the core-js version you are using via the `corejs` option.
You should also be sure that the version you pass to the `corejs` option matches the version specified in your `package.json`'s `dependencies` section. If it doesn't, you need to run one of the following commands:
npm install --save core-js@2 npm install --save core-js@3
usage
会根据配置的浏览器兼容,以及你代码中用到的 API 来进行 polyfill,实现了按需添加@babel/polyfill
src/index.js
console.log([1, 2, 3].find(item => item === 2));
console.log(Array.prototype.find);
console.log(Array.prototype.hasOwnProperty('find'));
webpack.config.js
{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
+ presets: [["@babel/preset-env", { useBuiltIns: 'usage', corejs: { version: 3 } }]]
}
}
}
babel-runtime
更像是一种按需加载的实现,比如你哪里需要使用 Promise
,只要在这个文件头部import Promise from 'babel-runtime/core-js/promise'
就行了npm i babel-runtime --save-dev
src/index.js
import Promise from 'babel-runtime/core-js/promise';
const p = new Promise(()=> {});
@babel/plugin-transform-runtime
插件是为了解决babel-plugin-transform-runtime
后,Babel就会使用babel-runtime
下的工具函数babel-plugin-transform-runtime
插件能够将这些工具函数的代码转换成require
语句,指向为对babel-runtime
的引用babel-plugin-transform-runtime
就是可以在我们使用新 API 时自动 import babel-runtime
里面的 polyfill
async/await
时,自动引入 babel-runtime/regenerator
babel-runtime/core-js
babel helpers
并替换使用babel-runtime/helpers
来替换npm i @babel/plugin-transform-runtime @babel/runtime-corejs3 --save-dev
webpack.config.js
{
test: /\.js?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
sourceType: "unambiguous",
presets: [["@babel/preset-env", { useBuiltIns: 'usage', corejs: { version: 3 } }]],
plugins: [
[
"@babel/plugin-transform-runtime",
{
corejs: 3,
helpers: true,
regenerator: true
}
],
]
}
}
}
//var _Promise = __webpack_require__("./node_modules/@babel/runtime-corejs3/core-js-stable/promise.js");
const p = new Promise(() => { });
console.log(p);
babel-runtime/helpers
来替换class A {
}
class B extends A {
}
console.log(new B());
generator
函数转换成使用regenerator runtime
来避免污染全局域//var _regeneratorRuntime = __webpack_require__(/*! @babel/runtime-corejs3/regenerator */ "./node_modules/@babel/runtime-corejs3/regenerator/index.js");
function* gen() {
}
console.log(gen());
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"corejs": false
}
]
]
}
polyfill
,因此@babel/preset-env
只发挥语法转换的功能@babel/plugin-transform-runtime
来处理,推荐使用core-js@3{
"presets": [
[
"@babel/preset-env"
]
],
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"corejs": {
"version": 3
}
}
]
]
}
<script src="https://polyfill.io/v3/polyfill.min.js"></script>