You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-50Lines changed: 62 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,68 @@
1
-
# babel-preset-modules
1
+
# `@babel/preset-modules`
2
2
3
-
A carefully crafted Babel preset that targets browsers with ES Modules support.
3
+
A 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)).
4
4
5
-
Use this instead of `@babel/preset-env`'s `target.esmodules` option for smaller bundle size and improved startup performance.
5
+
It 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.
6
6
7
7
### Features Supported
8
8
9
-
- JSX uses native Object Spread properties for JSX spread attributes instead of a helper.
9
+
- JSX uses native Object Spread when spreading props instead of a helper.
10
10
- Default, destructured and optional parameters are all natively supported.
11
11
- Tagged Templates are fully supported, patched for Safari 10+ and Edge 16+.
12
+
- async/await is supported without being transpiled to generators.
13
+
- Function name inference works as expected, including Arrow Functions.
12
14
13
-
### How?
15
+
### Installation & Usage
14
16
15
-
`@babel/preset-env` is great, since it lets you define which Babel features are needed based on a browser support target.
16
-
However, in order to make that plumbing work the preset has to group all of the possible JavaScript syntax features you might be using into fairly large groups. It enables or disables these groups based on the browser support target you specify, including `targets.esmodules` which is effectively an alias for the set of browsers that support ES Modules.
17
+
Install the preset from [npm](https://www.npmjs.com/package/@babel/preset-modules):
18
+
19
+
```sh
20
+
npm install @babel/preset-modules --save-dev
21
+
```
22
+
23
+
To use the preset, add it to your [Babel Configuration](https://babeljs.io/docs/en/configuration):
24
+
25
+
```js
26
+
{
27
+
"presets": [
28
+
"@babel/preset-modules"
29
+
]
30
+
}
31
+
```
32
+
33
+
If you're implementing the module/nomodule pattern, your configuration might look something like this:
34
+
35
+
```js
36
+
{
37
+
"env": {
38
+
"modern": {
39
+
"presets": [
40
+
"@babel/preset-modules"
41
+
]
42
+
},
43
+
"legacy": {
44
+
"presets": [
45
+
"@babel/preset-env"
46
+
]
47
+
}
48
+
}
49
+
}
50
+
```
51
+
52
+
### Options
53
+
54
+
There's a single Boolean `loose` option, which defaults to `false`. Passing `true` further reduces output size.
55
+
56
+
The `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.
57
+
58
+
### How does it work?
59
+
60
+
Babel’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.
61
+
62
+
From 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+.
63
+
64
+
Here'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.
17
65
18
-
The problem is that one browser that supports ES Modules can spoil a whole set of features, since they're all grouped together.
19
66
For 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:
20
67
21
68
```js
@@ -29,7 +76,7 @@ function foo({ a = 1, b }, ...args) {}
29
76
constfoo= ({ a: a =1 }) => {};
30
77
```
31
78
32
-
In 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 like a shame to transpile all those great features down to ES5 just for Edge 16, right? I thought so.
79
+
In 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!
33
80
34
81
This 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:
35
82
@@ -47,7 +94,7 @@ const foo = ({ a: a = 1 }, b = 2, ...args) => [a,b,args];
47
94
48
95
That output works in all ES Modules-supporting browsers, and is only **59 bytes** minified & gzipped.
49
96
50
-
> Compare this to `@babel/preset-env`'s `targets.esmodules` output for the above, at **147 bytes** minified & gzipped:
97
+
> Compare this to `@babel/preset-env`'s `targets.esmodules` output (**147 bytes** minified & gzipped):
51
98
>
52
99
> ```js
53
100
>constfoo=functionfoo(_ref, b) {
@@ -67,37 +114,16 @@ That output works in all ES Modules-supporting browsers, and is only **59 bytes*
67
114
>};
68
115
>````
69
116
70
-
The result is dramatically improved bundle size and performance, while supporting the same browsers. All from removing grouping.
71
-
72
-
### Example Usage
73
-
74
-
```js
75
-
{
76
-
"env": {
77
-
"modern": {
78
-
"presets": ["babel-preset-modules"]
79
-
},
80
-
"legacy": {
81
-
"presets": {
82
-
["@babel/preset-env", {
83
-
"targets": {
84
-
"browsers": ">1%, not dead"
85
-
}
86
-
}]
87
-
}
88
-
}
89
-
}
90
-
}
91
-
```
117
+
The result is improved bundle size and performance, while supporting the same browsers.
92
118
93
119
94
120
### Important: Minification
95
121
96
-
The output generated by this preset includes workarounds for Safari 10, however minifiers like Terser sometimes remove these workarounds.
97
-
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.
122
+
The 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.
123
+
98
124
It'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.
99
125
100
-
With [Terser Node API](https://github.com/terser/terser#minify-options):
126
+
With [Terser'sNodeAPI](https://github.com/terser/terser#minify-options):
101
127
102
128
```js
103
129
terser.minify({
@@ -127,19 +153,5 @@ module.exports = {
127
153
};
128
154
```
129
155
130
-
All of the above configurations apply work for [uglify-es](https://github.com/mishoo/UglifyJS2/tree/harmony).
156
+
All of the above configurations also apply to [uglify-es](https://github.com/mishoo/UglifyJS2/tree/harmony).
131
157
UglifyJS (2.x and prior) does not support modern JavaScript, so it cannot be used in conjuction withthis preset.
132
-
133
-
134
-
### Bundle Size Comparison
135
-
136
-
Bundling an unmodified copy of the `redux-todos` codebase.
0 commit comments