PSA: this repo has been moved into babel/babel -->
Installations
npm install babel-preset-env
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.9.0
NPM Version
6.0.0
Statistics
3,499 Stars
330 Commits
158 Forks
68 Watching
8 Branches
57 Contributors
Updated on 19 Nov 2024
Bundle Size
75.92 kB
Minified
20.45 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
569,286,094
Last day
-14.4%
210,549
Compared to previous day
Last week
1.9%
1,362,125
Compared to previous week
Last month
56.3%
4,804,370
Compared to previous month
Last year
-22.1%
42,477,176
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
30
Now that babel-preset-env
has stabilized, it has been moved into the main Babel mono-repo and this repo has been archived.
The move makes it much easier to release and develop in sync with the rest of Babel!
This repo will be made read-only, as all of the issues/labels have been moved over as well. Please report any bugs and open pull requests over on the main mono-repo.
babel-preset-env
A Babel preset that compiles ES2015+ down to ES5 by automatically determining the Babel plugins and polyfills you need based on your targeted browser or runtime environments.
1npm install babel-preset-env --save-dev
Without any configuration options, babel-preset-env behaves exactly the same as babel-preset-latest (or babel-preset-es2015, babel-preset-es2016, and babel-preset-es2017 together).
However, we don't recommend using
preset-env
this way because it doesn't take advantage of it's greater capabilities of targeting specific browsers.
1{ 2 "presets": ["env"] 3}
You can also configure it to only include the polyfills and transforms needed for the browsers you support. Compiling only what's needed can make your bundles smaller and your life easier.
This example only includes the polyfills and code transforms needed for coverage of users > 0.25%, ignoring Internet Explorer 11 and Opera Mini.. We use browserslist to parse this information, so you can use any valid query format supported by browserslist.
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 // The % refers to the global coverage of users from browserslist 6 "browsers": [ ">0.25%", "not ie 11", "not op_mini all"] 7 } 8 }] 9 ] 10}
You can also target individual versions of browsers instead of using a query with
"targets": { "chrome": "52" }
.
Similarly, if you're targeting Node.js instead of the browser, you can configure babel-preset-env to only include the polyfills and transforms necessary for a particular version:
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "node": "6.10" 6 } 7 }] 8 ] 9}
For convenience, you can use "node": "current"
to only include the necessary polyfills and transforms for the Node.js version that you use to run Babel:
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "node": "current" 6 } 7 }] 8 ] 9}
Check out the many options (especially useBuiltIns
to polyfill less)!
How it Works
Determine environment support for ECMAScript features
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.
Ref: #7
Maintain a mapping between JavaScript features and Babel plugins
Currently located at plugin-features.js.
This should be straightforward to do in most cases. There might be cases where 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
.
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
).
Ref: #14
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 target option "node": "current"
to compile for the currently running node version.
For example, if you are building on Node 6, arrow functions won't be converted, but they will if you build on Node 0.12.
Support a browsers
option like autoprefixer
Use browserslist to declare supported environments by performing queries like > 1%, last 2 versions
.
Ref: #19
Install
With npm:
1npm install --save-dev babel-preset-env
Or yarn:
1yarn add babel-preset-env --dev
Usage
The default behavior without options runs all transforms (behaves the same as babel-preset-latest).
1{ 2 "presets": ["env"] 3}
Options
For more information on setting options for a preset, refer to the plugin/preset options documentation.
targets
{ [string]: number | string }
, defaults to {}
.
Takes an object of environment versions to support.
Each target environment takes a number or a string (we recommend using a string when specifying minor versions like node: "6.10"
).
Example environments: chrome
, opera
, edge
, firefox
, safari
, ie
, ios
, android
, node
, electron
.
The data for this is generated by running the build-data script which pulls in data from compat-table.
targets.node
number | string | "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": process.versions.node
.
targets.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
.
targets.uglify
true
When using uglify-js
to minify your code, you may run into syntax errors when targeting later browsers since uglify-js
does not support any ES2015+ syntax.
To prevent these errors - set the uglify
option to true
, which enables all transformation plugins and as a result, your code is fully compiled to ES5. However, the useBuiltIns
option will still work as before and only include the polyfills that your target(s) need.
Uglify has support for ES2015 syntax via uglify-es. If you are using syntax unsupported by
uglify-es
, we recommend using babel-minify.
Note: This option is deprecated in 2.x and replaced with a
forceAllTransforms
option.
spec
boolean
, defaults to false
.
Enable more spec compliant, but potentially slower, transformations for any plugins in this preset that support them.
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.
Setting this to false
will not transform modules.
debug
boolean
, defaults to false
.
Outputs the targets/plugins used and the version specified in plugin data version to console.log
.
include
Array<string>
, defaults to []
.
NOTE:
whitelist
is deprecated and will be removed in the next major in favor of this.
An array of plugins to always include.
Valid options include any:
-
Babel plugins - both with (
babel-plugin-transform-es2015-spread
) and without prefix (transform-es2015-spread
) are supported. -
Built-ins, such as
map
,set
, orobject.assign
.
This option is useful if there is a bug in a native implementation, or a combination of a non-supported feature + a supported one doesn't work.
For example, Node 4 supports native classes but not spread. If super
is used with a spread argument, then the transform-es2015-classes
transform needs to be include
d, as it is not possible to transpile a spread with super
otherwise.
NOTE: The
include
andexclude
options only work with the plugins included with this preset; so, for example, includingtransform-do-expressions
or excludingtransform-function-bind
will throw errors. To use a plugin not included with this preset, add them to your config directly.
exclude
Array<string>
, defaults to []
.
An array of plugins to always exclude/remove.
The possible options are the same as the include
option.
This option is useful for "blacklisting" a transform like transform-regenerator
if you don't use generators and don't want to include regeneratorRuntime
(when using useBuiltIns
) or for using another plugin like fast-async instead of Babel's async-to-gen.
useBuiltIns
boolean
, defaults to false
.
A way to apply babel-preset-env
for polyfills (via "babel-polyfill").
NOTE: This does not currently polyfill experimental/stage-x built-ins like the regular "babel-polyfill" does. This will only work with npm >= 3 (which should be used with Babel 6 anyway)
npm install babel-polyfill --save
This option enables a new plugin that replaces the statement import "babel-polyfill"
or require("babel-polyfill")
with individual requires for babel-polyfill
based on environment.
NOTE: Only use
require("babel-polyfill");
once in your whole app. Multiple imports or requires ofbabel-polyfill
will throw an error since it can cause global collisions and other issues that are hard to trace. We recommend creating a single entry file that only contains therequire
statement.
In
1import "babel-polyfill";
Out (different based on environment)
1import "core-js/modules/es7.string.pad-start"; 2import "core-js/modules/es7.string.pad-end"; 3import "core-js/modules/web.timers"; 4import "core-js/modules/web.immediate"; 5import "core-js/modules/web.dom.iterable";
This will also work for core-js
directly (import "core-js";
)
npm install core-js --save
Examples
Export with various targets
1export class A {}
Target only Chrome 52
.babelrc
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "chrome": 52 6 } 7 }] 8 ] 9}
Out
1class A {} 2exports.A = A;
Target Chrome 52 with webpack 2/rollup and loose mode
.babelrc
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "chrome": 52 6 }, 7 "modules": false, 8 "loose": true 9 }] 10 ] 11}
Out
1export class A {}
Target specific browsers via browserslist
.babelrc
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "chrome": 52, 6 "browsers": ["last 2 versions", "safari 7"] 7 } 8 }] 9 ] 10}
Out
1export var A = function A() { 2 _classCallCheck(this, A); 3};
Target latest node via node: true
or node: "current"
.babelrc
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "node": "current" 6 } 7 }] 8 ] 9}
Out
1class A {} 2exports.A = A;
Show debug output
.babelrc
1{ 2 "presets": [ 3 [ "env", { 4 "targets": { 5 "safari": 10 6 }, 7 "modules": false, 8 "useBuiltIns": true, 9 "debug": true 10 }] 11 ] 12}
stdout
1Using targets: 2{ 3 "safari": 10 4} 5 6Modules transform: false 7 8Using plugins: 9 transform-exponentiation-operator {} 10 transform-async-to-generator {} 11 12Using polyfills: 13 es7.object.values {} 14 es7.object.entries {} 15 es7.object.get-own-property-descriptors {} 16 web.timers {} 17 web.immediate {} 18 web.dom.iterable {}
Include and exclude specific plugins/built-ins
always include arrow functions, explicitly exclude generators
1{ 2 "presets": [ 3 ["env", { 4 "targets": { 5 "browsers": ["last 2 versions", "safari >= 7"] 6 }, 7 "include": ["transform-es2015-arrow-functions", "es6.map"], 8 "exclude": ["transform-regenerator", "es6.set"] 9 }] 10 ] 11}
Caveats
If you get a SyntaxError: Unexpected token ...
error when using the object-rest-spread transform then make sure the plugin has been updated to, at least, v6.19.0
.
Other Cool Projects
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 13/25 approved changesets -- score normalized to 5
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch '1.x'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 20 are checked with a SAST tool
Reason
74 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-832h-xg76-4gv6
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-mh2h-6j8q-x246
- Warn: Project is vulnerable to: GHSA-5q88-cjfq-g2mh / GHSA-xp63-6vf5-xf3v
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-p28h-cc7q-c4fg
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-hr2v-3952-633q
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-xf7w-r453-m56c
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-44pw-h2cw-w3vq
- Warn: Project is vulnerable to: GHSA-jp4x-w63m-7wgm
- Warn: Project is vulnerable to: GHSA-c429-5p7v-vgjp
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-4hpf-3wq7-5rpr
- Warn: Project is vulnerable to: GHSA-f522-ffg8-j8r6
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-282f-qqgm-c34q
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-6394-6h9h-cfjg
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-gqgv-6jq5-jjj9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-6g33-f262-xjp4
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-2m39-62fm-q8r3
- Warn: Project is vulnerable to: GHSA-mf6x-7mm4-x2g7
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-g7q5-pjjr-gqvp
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-xc7v-wxcw-j472
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
2.4
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More