1. babel-polyfill #

1.1 安装 #

npm i @babel/polyfill

1.2 targets #

{
    test: /\.js?$/,
    exclude: /node_modules/,
    use: {
        loader: 'babel-loader',
        options: {
            targets: {
                //"browsers": ["ie >= 8", "chrome >= 62"],// 推荐使用 .browserslist
                "browsers": [">1%"]
            }
        }
    }
}

1.3 useBuiltIns: false #

1.3.1 src/index.js #

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));

1.3.2 webpack.config.js #

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: []
};

1.4 useBuiltIns: "entry" #

1.4.1 安装 #

 npm install --save core-js@2    npm install --save core-js@3

1.4.2 src\index.js #

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));

1.4.3 webpack.config.js #

{
  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

1.5 "useBuiltIns": "usage" #

1.5.1 src/index.js #

src/index.js

console.log([1, 2, 3].find(item => item === 2));
console.log(Array.prototype.find);
console.log(Array.prototype.hasOwnProperty('find'));

1.5.2 webpack.config.js #

webpack.config.js

{
  test: /\.js?$/,
  exclude: /node_modules/,
  use: {
      loader: 'babel-loader',
      options: {
+         presets: [["@babel/preset-env", { useBuiltIns: 'usage', corejs: { version: 3 } }]]
      }
  }
}

2. babel-runtime #

2.1 安装 #

npm i babel-runtime --save-dev

2.2 src/index.js #

src/index.js

import Promise from 'babel-runtime/core-js/promise';
const p = new Promise(()=> {});

3. babel-plugin-transform-runtime #

3.1 安装 #

npm i @babel/plugin-transform-runtime @babel/runtime-corejs3 --save-dev

3.2 webpack.config.js #

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
                    }
                ],
            ]
        }
    }
 }

3.3 corejs: 3 #

3.4 helpers: true #

class A {

}
class B extends A {

}
console.log(new B());

3.5 regenerator: true #

//var _regeneratorRuntime = __webpack_require__(/*! @babel/runtime-corejs3/regenerator */ "./node_modules/@babel/runtime-corejs3/regenerator/index.js");

function* gen() {

}
console.log(gen());

4. 最佳实践 #

4.1 项目开发 #

4.2 类库开发 #

{
    "presets": [
        [
            "@babel/preset-env"
        ]
    ],
    "plugins": [
        [
            "@babel/plugin-transform-runtime",
            {
                "corejs": {
                    "version": 3
                }
            }
        ]
    ]
}

5. polyfill-service #

<script src="https://polyfill.io/v3/polyfill.min.js"></script>