|
40 | 40 | } |
41 | 41 | ``` |
42 | 42 |
|
43 | | -### New: Modern JS |
| 43 | +## 🤖 Modern Output |
44 | 44 |
|
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: |
48 | 48 |
|
49 | 49 | ```js |
50 | 50 | // Our source, "src/make-dom.js": |
51 | 51 | 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); |
55 | 72 | } |
56 | 73 | ``` |
57 | 74 |
|
58 | | -Microbundle compiles the above to this: |
| 75 | +</td><td> |
59 | 76 |
|
60 | 77 | ```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) } } |
65 | 85 | ``` |
66 | 86 |
|
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: |
68 | 90 |
|
69 | 91 | ```js |
70 | 92 | { |
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) |
72 | 94 | "module": "dist/foo.modern.module.js", // modern ES2017 bundle |
73 | 95 | "scripts": { |
74 | 96 | "build": "microbundle src/foo.js -f modern,umd" |
|
0 commit comments