Skip to content

Commit a052523

Browse files
authored
Clarify modern mode in the readme (#625)
1 parent 1c8e7f4 commit a052523

1 file changed

Lines changed: 36 additions & 14 deletions

File tree

README.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,57 @@
4040
}
4141
```
4242

43-
### New: Modern JS
43+
## 🤖 Modern Output
4444

45-
Microbundle now has a new `modern` format (`microbundle -f modern`).
46-
Modern output still bundles and compresses your code, but it keeps useful syntax
47-
around that actually helps compression:
45+
Microbundle's `esm`, `cjs`, `umd` and `iife` output formats all compile your code to syntax that works pretty much everywhere. You can customize which browser or Node versions you wish to support by [adding a browserslist configuration](https://github.com/browserslist/browserslist#browserslist-), however the default setting is recommended for most cases since it supports as many browsers as possible.
46+
47+
In addition to the above formats, Microbundle also outputs a `modern` bundle specially designed to work in _all modern browsers_. This bundle preserves most modern JS features when compiling your code, but ensures the result runs in 90% of web browsers without needing to be transpiled. Specifically, it uses [preset-modules](https://github.com/babel/preset-modules) to target the set of browsers that support `<script type="module">` - that allows syntax like async/await, tagged templates, arrow functions, destructured and rest parameters, etc. The result is generally smaller and faster to execute than the `esm` bundle:
4848

4949
```js
5050
// Our source, "src/make-dom.js":
5151
export default async function makeDom(tag, props, children) {
52-
const el = document.createElement(tag);
53-
el.append(...(await children));
54-
return Object.assign(el, props);
52+
const el = document.createElement(tag);
53+
el.append(...(await children));
54+
return Object.assign(el, props);
55+
}
56+
```
57+
58+
Compiling the above using Microbundle produces the following `modern` and `esm` bundles:
59+
60+
<table>
61+
<thead><tr>
62+
<th align="left"><code>make-dom.modern.js</code> <sup>(123b)</sup></th>
63+
<th align="left"><code>make-dom.module.js</code> <sup>(166b)</sup></th>
64+
</tr></thead>
65+
<tbody><tr valign="top"><td>
66+
67+
```js
68+
export default async function(e, t, a) {
69+
var n = document.createElement(e);
70+
n.append(...await a);
71+
return Object.assign(n, t);
5572
}
5673
```
5774

58-
Microbundle compiles the above to this:
75+
</td><td>
5976

6077
```js
61-
export default async (e, t, a) => {
62-
const n = document.createElement(e);
63-
return n.append(...(await a)), Object.assign(n, t);
64-
};
78+
export default function(e, t, r) { try {
79+
var n = document.createElement(e);
80+
return Promise.resolve(r).then(function(e) {
81+
n.append.apply(n, e);
82+
return Object.assign(n, t);
83+
});
84+
} catch (e) { return Promise.reject(e) } }
6585
```
6686

67-
This is enabled by default - all you have to do is add the field to your `package.json`. You might choose to ship modern JS using the "module" field:
87+
</td></tbody></table>
88+
89+
This is enabled by default - all you have to do is add the field to your `package.json`. While the best way to point to modern source from a package.json is [still being discussed](https://twitter.com/_developit/status/1263174528974364675), you might choose to use the "module" field:
6890

6991
```js
7092
{
71-
"main": "dist/foo.umd.js", // legacy UMD bundle (for Node & CDN's)
93+
"main": "dist/foo.umd.js", // legacy UMD bundle (for Node & CDN use)
7294
"module": "dist/foo.modern.module.js", // modern ES2017 bundle
7395
"scripts": {
7496
"build": "microbundle src/foo.js -f modern,umd"

0 commit comments

Comments
 (0)