Many people like me used babel-preset- ES2015 when they were first introduced to Babel, but babel-preset-env is a better choice for now, Babel-preset -env can automatically convert ES2015+ code to ES5 based on the target browser or runtime environment configured.

Attraction is that we can [. Browserslistrc] (https://github.com/browserslist/browserslist) file browser to specify a particular target. Of course, you can also specify this with the Targets option. However, if your target browser supports es Modules, the Browsers option is disabled, as follows:

{
    "presets": [["@babel/preset-env"
            "targets": {
                "esmodules": true}}]]Copy the code

You can also specify the node.js version:

{
    "presets": [["@babel/preset-env"
            "targets": {
                "node": "6.10"}}]]Copy the code

If the node option value is current, the runtime version of Node.js is specified.

{
    "presets": [["@babel/preset-env"
            "targets": {
                "node": "current"}}]]Copy the code

All we need to know is that browsers are built using Browserslist when developing targets. We can specify browser version options in the.babelrc file, package.json file, or browserslist. The priority rule is that.babelrc will ignore browserslist if it is defined, and.babelrc will search for browserslist and package.json if it is not defined.

The main parameter options for babel-preset-env are:

  • targets

  • targets.node

  • targets.browsers

  • Spec: Enables more spec compliant conversions, but slower, default to false

  • Loose: Whether to use loose mode. The default value is false

  • Modules: the ES6 module into other module specification, optional “adm” | “umd” | “systemjs” | “commonjs” | “CJS |” false, the default is false

  • Debug: Enables debug. The default value is false

  • Include: An array of plugins to use

  • Exclude: An array containing unused plugins

  • UseBuiltIns: @ Babel for polyfills applications/preset – env, optional “usage” | | “entry” false, the default is false

Refs:

README of babel-preset-env