Giter Club home page Giter Club logo

babel-preset-env's Introduction

babel-preset-env npm travis

Babel preset for all environments.

npm install babel-preset-env --save-dev

// .babelrc
{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 54
      }
    }]
  ]
}

How it Works

Determine environment support for ECMAScript features

#7 - Use external data such as compat-table to determine browser support. (We should create PRs there when necessary)

We can periodically run build-data.js which generates plugins.json.

Maintain a mapping between javascript features and babel plugins

Currently located at pluginFeatures.js.

This should be straightforward to do in most cases. There might be cases were plugins should be split up more or certain plugins aren't standalone enough (or impossible to do).

Support all plugins in Babel that are considered latest

Default behavior without options is the same as babel-preset-latest.

#14 - It won't include stage-x plugins. env will support all plugins in what we consider the latest version of Javascript (by matching what we do in babel-preset-latest).

Support a node option "node": "current" to only compile for the current running node version.

Determine the lowest common denominator of plugins to be included in the preset

If you are targeting IE 8 and Chrome 55 it will include all plugins required by IE 8 since you would need to support both still.

Support a browsers option like autoprefixer

#19 - Use browserslist to also queries like > 1%, last 2 versions.

Install

$ npm install --save-dev babel-preset-env

Usage

The default behavior without options runs all transforms (acts as babel-preset-latest).

{
  "presets": ["env"]
}

targets: { [string]: number }

Defaults to {}.

Takes an object of environment versions to support. Each target environment takes a number (you can specify a decimal like node: 6.5)

Example environments: "chrome, edge, firefox, safari, ie, ios, node".

The data for this is currently at: /data/plugins.json and being generated by /scripts/build-data.js using https://kangax.github.io/compat-table.

node: number | "current" | true

If you want to compile against the current node version, you can specify "node": true or "node": "current" which would be the same as node": parseFloat(process.versions.node)

browsers: Array<string> | string

A query to select browsers (ex: last 2 versions, > 5%) using browserslist.

Note, browsers' results are overridden by explicit items from targets.

loose: boolean

Defaults to false. Enable "loose" transformations for any plugins in this preset that allow them.

modules: "amd" | "umd" | "systemjs" | "commonjs" | false

Defaults to "commonjs". Enable transformation of ES6 module syntax to another module type. Can be false to not transform modules.

debug: boolean

Defaults to false console.log out the targets and plugins being used as well as the version specified in /data/plugins.json.

whitelist: Array<string>

Defaults to [] Enable a whitelist of plugins to always include.

Useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work.

Ex: Node 4 supports native classes but not spread.

Examples

// src
export class A {}
// target chrome 52
{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 52
      }
    }]
  ]
}

// ...

class A {}
exports.A = A;
// target chrome 52 with webpack 2/rollup and loose mode
{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 52
      },
      "modules": false,
      "loose": true
    }]
  ]
}

// ...

export class A {}
// using browserslist
{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 52,
        "browsers": ["last 2 versions", "safari 7"]
      }
    }]
  ]
}

// ...

export var A = function A() {
  _classCallCheck(this, A);
};

Example with node: true or node: "current"

// process.versions.node -> 6.9.0
{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}

// ...

class A {}
exports.A = A;

Example with debug: true

Using targets: {
  "node": 6.5
}

Using plugins:

module: false
transform-exponentiation-operator {}
transform-async-to-generator {}
syntax-trailing-function-commas {}

Example with whitelist

// target chrome 52 with whitelist on arrow functions
{
  "presets": [
    ["env", {
      "targets": {
        "chrome": 52
      },
      "whitelist": ["transform-es2015-arrow-functions"]
    }]
  ]
}

Using plugins:

transform-exponentiation-operator {}
transform-async-to-generator {}
syntax-trailing-function-commas {}
transform-es2015-arrow-functions {}

Caveats

Using babel-plugin-transform-object-rest-spread and targeting node.js 6.5 or higher

You may get a SyntaxError: Unexpected token ... error if using the object-rest-spread feature and targeting node.js 6.5 or higher.

This is a known issue at babel/babel#4074.

A simple workaround would be to re-enable the following plugins: babel-plugin-transform-es2015-destructuring and babel-plugin-transform-es2015-parameters, which can be done with the whitelist option.

{
  "presets": [
    ["env", {
      "targets": {
        "node": 6.5
      },
      "whitelist": [
        "transform-es2015-destructuring",
        "transform-es2015-parameters"
      ]
    }]
  ],
  "plugins": ["transform-object-rest-spread"]
}

babel-preset-env's People

Contributors

baer avatar basicdays avatar fson avatar hzoo avatar rmariuzzo avatar yavorsky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.