source: trip-planner-front/node_modules/@babel/preset-modules/README.md@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[6a3a178]1# `@babel/preset-modules`
2
3> ℹ️ Starting from `@babel/preset-env` 7.9.0, you can enable the [`bugfixes: true` option](https://babeljs.io/docs/en/babel-preset-env#bugfixes) to get the same behavior as using `@babel/preset-modules`, but with support for custom `targets`.
4> If you need to target browsers with native modules support (like this preset does), you can use `targets: { esmodules: true }`.
5
6A Babel preset that enables async/await, Tagged Templates, arrow functions, destructured and rest parameters, and more **in all modern browsers** ([88% of traffic](https://caniuse.com/#feat=es6-module)).
7
8It works around bugs and inconsistencies in modern JavaScript engines by converting broken syntax to the _closest non-broken modern syntax_. Use this in place of `@babel/preset-env`'s [target.esmodules](https://babeljs.io/docs/en/babel-preset-env#targetsesmodules) option for smaller bundle size and improved performance.
9
10This preset is only useful for browsers. You can serve the output to modern browsers while still supporting older browsers using the [module/nomodule pattern](https://philipwalton.com/articles/deploying-es2015-code-in-production-today/):
11
12```html
13<!-- transpiled with preset-modules: -->
14<script type="module" src="modern.js"></script>
15<!-- transpiled with preset-env: -->
16<script nomodule src="legacy.js"></script>
17```
18
19### Features Supported
20
21- JSX spread attributes are compiled to Object.assign() instead of a helper.
22- Default, destructured and optional parameters are all natively supported.
23- Tagged Templates are fully supported, patched for Safari 10+ and Edge 16+.
24- async/await is supported without being transpiled to generators.
25- Function name inference works as expected, including Arrow Functions.
26
27### Installation & Usage
28
29Install the preset from [npm](https://www.npmjs.com/package/@babel/preset-modules):
30
31```sh
32npm install @babel/preset-modules --save-dev
33```
34
35To use the preset, add it to your [Babel Configuration](https://babeljs.io/docs/en/configuration):
36
37```js
38{
39 "presets": [
40 "@babel/preset-modules"
41 ]
42}
43```
44
45If you're implementing the module/nomodule pattern, your configuration might look something like this:
46
47```js
48{
49 "env": {
50 "modern": {
51 "presets": [
52 "@babel/preset-modules"
53 ]
54 },
55 "legacy": {
56 "presets": [
57 "@babel/preset-env"
58 ]
59 }
60 }
61}
62```
63
64### Options
65
66There's a single Boolean `loose` option, which defaults to `false`. Passing `true` further reduces output size.
67
68The `loose` setting turns off a rarely-needed function name workaround for older versions of Edge. If you're not relying on `Function.prototype.name`, it's worth enabling loose mode.
69
70### How does it work?
71
72Babel’s `preset-env` is great, since it lets you define which Babel features are needed based on a browser support target. In order to make that plumbing work automatically, the preset has configuration that groups all of the new JavaScript syntax features into collections of related syntax transforms. These groups are fairly large, for example "function arguments" includes destructured, default and rest parameters. The groupings come from the fact that Babel’s transforms often rely on other transforms, so they can’t always be applied in isolation.
73
74From this grouping information, Babel enables or disables each group based on the browser support target you specify to preset-env’s [targets](https://babeljs.io/docs/en/babel-preset-env#targets) option. For modern output, the [targets.esmodules](https://babeljs.io/docs/en/babel-preset-env#targetsesmodules) option is effectively an alias for the set of browsers that support ES Modules: Edge 16+, Safari 10.1+, Firefox 60+ and Chrome 61+.
75
76Here's the problem: if any version of any browser in that list contains a bug triggered by modern syntax, the only solution we have is to enable the corresponding transform group that fixes that bug. This means that fundamentally, preset-env converts code to ES5 in order to get around syntax bugs in ES2017. Since that's the only solution at our disposal, eventually it becomes overused.
77
78For example, all of the new syntax features relating to function parameters are grouped into the same Babel plugin (`@babel/plugin-transform-function-parameters`). That means because Edge 16 & 17 support ES Modules but have a bug related to parsing shorthand destructured parameters with default values within arrow functions, all functions get compiled from the new compact argument syntaxes down to ES5:
79
80```js
81// this breaks in Edge 16:
82const foo = ({ a = 1 }) => {};
83
84// .. but this doesn't:
85function foo({ a = 1, b }, ...args) {}
86
87// ... and neither does this:
88const foo = ({ a: a = 1 }) => {};
89```
90
91In fact, there are 23 syntax improvements for function parameters in ES2017, and only one of them is broken in ES Modules-supporting browsers. It seems unfortunate to transpile all those great features down to ES5 just for one browser!
92
93This plugin takes a different approach than we've historically taken with JavaScript: it transpiles the broken syntax to the closest _non-broken modern syntax_. In the above case, here's what is generated to fix all ES Modules-supporting browsers:
94
95**input:**
96
97```js
98const foo = ({ a = 1 }, b = 2, ...args) => [a,b,args];
99```
100
101**output:**
102
103```js
104const foo = ({ a: a = 1 }, b = 2, ...args) => [a,b,args];
105```
106
107That output works in all ES Modules-supporting browsers, and is only **59 bytes** minified & gzipped.
108
109> Compare this to `@babel/preset-env`'s `targets.esmodules` output (**147 bytes** minified & gzipped):
110>
111> ```js
112>const foo = function foo(_ref, b) {
113> let { a = 1 } = _ref;
114>
115> if (b === void 0) { b = 2; }
116>
117> for (
118> var _len = arguments.length,
119> args = new Array(_len > 2 ? _len - 2 : 0),
120> _key = 2; _key < _len; _key++
121> ) {
122> args[_key - 2] = arguments[_key];
123> }
124>
125> return [a, b, args];
126>};
127>````
128
129The result is improved bundle size and performance, while supporting the same browsers.
130
131
132### Important: Minification
133
134The output generated by this preset includes workarounds for Safari 10, however minifiers like Terser sometimes remove these workarounds. In order to avoid shipping broken code, it's important to tell Terser to preserve the workarounds, which can be done via the `safari10` option.
135
136It's also generally the case that minifiers are configured to output ES5 by default, so you'll want to change the output syntax to ES2017.
137
138With [Terser's Node API](https://github.com/terser/terser#minify-options):
139
140```js
141terser.minify({
142 ecma: 8,
143 safari10: true
144})
145```
146
147With [Terser CLI](https://npm.im/terser):
148
149```sh
150terser --ecma 8 --safari10 ...
151```
152
153With [terser-webpack-plugin](https://webpack.js.org/plugins/terser-webpack-plugin/):
154
155```js
156module.exports = {
157 optimization: {
158 minimizer: [
159 new TerserPlugin({
160 terserOptions: {
161 ecma: 8,
162 safari10: true
163 }
164 })
165 ]
166 }
167};
168```
169
170All of the above configurations also apply to [uglify-es](https://github.com/mishoo/UglifyJS2/tree/harmony).
171UglifyJS (2.x and prior) does not support modern JavaScript, so it cannot be used in conjunction with this preset.
Note: See TracBrowser for help on using the repository browser.