Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
babel-preset-env
Advanced tools
babel-preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). It automatically determines the Babel plugins and polyfills you need based on your supported environments.
Automatic Polyfilling
Automatically includes polyfills for the features you use based on your target environments. The 'useBuiltIns' option set to 'usage' ensures that only the necessary polyfills are included.
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
Targeting Specific Environments
Allows you to specify the environments you want to support. The 'targets' option lets you define the browsers or Node.js versions you want to transpile your code for.
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"browsers": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
}
]
]
}
Debugging
Enables debug mode to print useful information about the targets and plugins being used. This helps in understanding what transformations are being applied.
{
"presets": [
[
"@babel/preset-env",
{
"debug": true
}
]
]
}
@babel/preset-react is a Babel preset that includes plugins needed to transform React JSX syntax. It is specifically tailored for React applications, whereas babel-preset-env is more general-purpose.
@babel/preset-typescript is a Babel preset that allows Babel to transform TypeScript code into JavaScript. It focuses on TypeScript syntax, while babel-preset-env focuses on modern JavaScript features.
core-js is a library that provides polyfills for modern JavaScript features. While babel-preset-env can automatically include core-js polyfills based on usage, core-js itself is a standalone library for polyfilling.
A Babel preset that can automatically determine the Babel plugins and polyfills you need based on your supported environments.
npm install babel-preset-env --save-dev
{
"presets": [
["env", {
"targets": {
"browsers": ["last 2 versions", "safari >= 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.
Ref: #7
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).
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
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.
"node": "current"
to compile for the currently running node version.For example, if you are building on Node 4, arrow functions won't be converted, but they will if you build on Node 0.12.
browsers
option like autoprefixerUse browserslist to declare supported environments by performing queries like > 1%, last 2 versions
.
Ref: #19
With npm:
npm install --save-dev babel-preset-env
Or yarn:
yarn add babel-preset-env --dev
The default behavior without options runs all transforms (behaves the same as babel-preset-latest).
{
"presets": ["env"]
}
For more information on setting options for a preset, refer to the plugin/preset options documentation.
targets
{ [string]: number }
, defaults to {}
.
Takes an object of environment versions to support.
Each target environment takes a number (you can also specify a minor versions like node: 6.5
)
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 | "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)
.
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
number | true
UglifyJS does not currently support any ES6 syntax, so if you are using Uglify to minify your code, targeting later browsers may cause Uglify to throw syntax errors.
To prevent these errors - specify the uglify option, which will enable all plugins and, as a result, fully compile your code to ES5. However, the useBuiltIns
option will still work as before, and only include the polyfills that your target(s) need.
NOTE: Uglify has a work-in-progress "Harmony" branch to address the lack of ES6 support, but it is not yet stable. You can follow its progress in UglifyJS2 issue #448. If you require an alternative minifier which does support ES6 syntax, we recommend using Babili.
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 []
.
An array of plugins to always include.
Valid options include any of the babel plugins or built-ins, such as transform-es2015-arrow-functions
, map
, set
, or object.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.
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
| "entry"
, defaults to true
.
A way to apply babel-preset-env
for polyfills (via babel-polyfill
).
npm install babel-polyfill --save
useBuiltIns: true
Adds specific imports for polyfills when they are used in each file. We take advantage of the fact that a bundler will load the same polyfill only once.
In
a.js
var a = new Promise();
b.js
var b = new Map();
Out (if environment doesn't support it)
import "babel-polyfill/core-js/modules/es6.promise";
var a = new Promise();
import "babel-polyfill/core-js/modules/es6.map";
var b = new Map();
Out (if environment supports it)
var a = new Promise();
var b = new Map();
useBuiltIns: 'entry'
NOTE: Only use
require("babel-polyfill");
once in your whole app. One option is to create a single entry file that only contains the require statement.
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.
In
import "babel-polyfill";
Out (different based on environment)
import "babel-polyfill/core-js/modules/es7.string.pad-start";
import "babel-polyfill/core-js/modules/es7.string.pad-end";
useBuiltIns: false
Don't add polyfills automatically per file, or transform import "babel-polyfill"
to individual polyfills.
export class A {}
.babelrc
{
"presets": [
["env", {
"targets": {
"chrome": 52
}
}]
]
}
Out
class A {}
exports.A = A;
.babelrc
{
"presets": [
["env", {
"targets": {
"chrome": 52
},
"modules": false,
"loose": true
}]
]
}
Out
export class A {}
.babelrc
{
"presets": [
["env", {
"targets": {
"chrome": 52,
"browsers": ["last 2 versions", "safari 7"]
}
}]
]
}
Out
export var A = function A() {
_classCallCheck(this, A);
};
node: true
or node: "current"
.babelrc
{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
}
Out
class A {}
exports.A = A;
.babelrc
{
"presets": [
[ "env", {
"targets": {
"safari": 10
},
"modules": false,
"useBuiltIns": true,
"debug": true
}]
]
}
stdout
Using targets:
{
"safari": 10
}
Modules transform: false
Using plugins:
transform-exponentiation-operator {}
transform-async-to-generator {}
Using polyfills:
es7.object.values {}
es7.object.entries {}
es7.object.get-own-property-descriptors {}
web.timers {}
web.immediate {}
web.dom.iterable {}
always include arrow functions, explicitly exclude generators
{
"presets": [
["env", {
"targets": {
"browsers": ["last 2 versions", "safari >= 7"]
},
"include": ["transform-es2015-arrow-functions", "es6.map"],
"exclude": ["transform-regenerator", "es6.set"]
}]
]
}
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
.
FAQs
A Babel preset for each environment.
We found that babel-preset-env demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.