Skip to content

Commit

Permalink
Merge branch 'master' into chore-upgrade-rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Dec 17, 2020
2 parents 1188a6c + 8b60fc8 commit 9d2e35f
Show file tree
Hide file tree
Showing 6 changed files with 2,941 additions and 1,889 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-spies-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Enable sourcemaps for CSS
5 changes: 5 additions & 0 deletions .changeset/silver-guests-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': minor
---

Use user's typescript first, fallback to bundled
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"name": "foo", // your package name
"source": "src/foo.js", // your source code
"main": "dist/foo.js", // where to generate the CommonJS/Node bundle
"exports": "dist/foo.modern.js", // path to the modern output (see below)
"module": "dist/foo.module.js", // where to generate the ESM bundle
"unpkg": "dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
"scripts": {
Expand All @@ -58,7 +59,7 @@ Microbundle produces <code title="ECMAScript Modules (import / export)">esm</cod

## 🤖 Modern Mode <a name="modern"></a>

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:
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 95% 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:

```js
// Our source, "src/make-dom.js":
Expand All @@ -73,13 +74,13 @@ Compiling the above using Microbundle produces the following `modern` and `esm`

<table>
<thead><tr>
<th align="left"><code>make-dom.modern.js</code> <sup>(123b)</sup></th>
<th align="left"><code>make-dom.module.js</code> <sup>(166b)</sup></th>
<th align="left"><code>make-dom.modern.js</code> <sup>(117b)</sup></th>
<th align="left"><code>make-dom.module.js</code> <sup>(194b)</sup></th>
</tr></thead>
<tbody><tr valign="top"><td>

```js
export default async function(e, t, a) {
export default async function (e, t, a) {
let n = document.createElement(e);
n.append(...(await a));
return Object.assign(n, t);
Expand All @@ -89,12 +90,11 @@ export default async function(e, t, a) {
</td><td>

```js
export default function(e, t, r) {
export default function (e, t, r) {
try {
var n = document.createElement(e);
return Promise.resolve(r).then(function(e) {
n.append.apply(n, e);
return Object.assign(n, t);
return Promise.resolve(r).then(function (e) {
return n.append.apply(n, e), Object.assign(n, t);
});
} catch (e) {
return Promise.reject(e);
Expand All @@ -104,21 +104,34 @@ export default function(e, t, r) {

</td></tbody></table>

This is enabled by default - all you have to do is add the field to your `package.json`.
**This is enabled by default.** All you have to do is add an `"exports"` field to your `package.json`:

<details><summary>💁‍♂️ <em>How to point to modern code in a package.json is <a href="https://twitter.com/_developit/status/1263174528974364675">being discussed</a>. You might use the "module" field.</em></summary>

```js
```jsonc
{
"main": "dist/foo.umd.js", // legacy UMD bundle (for Node & CDN use)
"module": "dist/foo.modern.module.js", // modern ES2017 bundle
"scripts": {
"build": "microbundle src/foo.js -f modern,umd"
}
"main": "./dist/foo.umd.js", // legacy UMD output (for Node & CDN use)
"module": "./dist/foo.module.js", // legacy ES Modules output (for bundlers)
"exports": "./dist/foo.modern.js", // modern ES2017 output
"scripts": {
"build": "microbundle src/foo.js"
}
}
```

</details>
The `"exports"` field can also be an object for packages with multiple entry modules:

```jsonc
{
"name": "foo",
"exports": {
".": "./dist/foo.modern.js", // import "foo" (the default)
"./lite": "./dist/lite.modern.js", // import "foo/lite"
"./full": "./dist/full.modern.js" // import "foo"
},
"scripts": {
"build": "microbundle src/*.js" // build foo.js, lite.js and full.js
}
}
```

## 📦 Usage & Configuration <a name="usage"></a>

Expand Down Expand Up @@ -248,7 +261,7 @@ Options
Examples
$ microbundle build --globals react=React,jquery=$
$ microbundle build --define API_KEY=1234
$ microbundle build --alias react=preact
$ microbundle build --alias react=preact/compat
$ microbundle watch --no-sourcemap # don't generate sourcemaps
$ microbundle build --tsconfig tsconfig.build.json
```
Expand Down
Loading

0 comments on commit 9d2e35f

Please sign in to comment.