Skip to content

Commit 0832977

Browse files
authored
Readme Tweaks (#628)
1 parent a052523 commit 0832977

1 file changed

Lines changed: 57 additions & 27 deletions

File tree

README.md

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99

1010
---
1111

12-
## ✨ Features:
12+
<p align="center">
13+
<strong>Guide → </strong>
14+
<a href="#setup">Setup</a> ✯
15+
<a href="#formats">Formats</a> ✯
16+
<a href="#modern">Modern Mode</a> ✯
17+
<a href="#usage">Usage &amp; Configuration</a> ✯
18+
<a href="#options">All Options</a>
19+
</p>
20+
21+
---
22+
23+
## ✨ Features <a name="features"></a>
1324

1425
- **One dependency** to bundle your library using only a `package.json`
1526
- Support for ESnext & async/await _(via [Babel] & [async-to-promises])_
@@ -19,37 +30,40 @@
1930
- 0 configuration TypeScript support
2031
- Built-in Terser compression & gzipped bundle size tracking
2132

22-
## 🔧 Installation
33+
## 🔧 Installation & Setup <a name="setup"></a> <a name="installation"></a>
2334

24-
### Download
35+
1️⃣ **Install** by running: `npm i -D microbundle`
2536

26-
`npm i -D microbundle`
27-
28-
### Set up your `package.json`
37+
2️⃣ **Set up** your `package.json`:
2938

3039
```js
3140
{
32-
"source": "src/foo.js", // Your source file (same as 1st arg to microbundle)
33-
"main": "dist/foo.js", // output path for CommonJS/Node
34-
"module": "dist/foo.module.js", // output path for JS Modules
35-
"unpkg": "dist/foo.umd.js", // optional, for unpkg.com
41+
"name": "foo", // your package name
42+
"source": "src/foo.js", // your source code
43+
"main": "dist/foo.js", // where to generate the CommonJS/Node bundle
44+
"module": "dist/foo.module.js", // where to generate the ESM bundle
45+
"unpkg": "dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
3646
"scripts": {
37-
"build": "microbundle", // uses "source" and "main" as input and output paths by default
38-
"dev": "microbundle watch"
47+
"build": "microbundle", // compiles "source" to "main"/"module"/"unpkg"
48+
"dev": "microbundle watch" // re-build when source files change
3949
}
4050
}
4151
```
4252

43-
## 🤖 Modern Output
53+
3️⃣ **Try it out** by running `npm run build`.
54+
55+
## 💽 Output Formats <a name="formats"></a>
56+
57+
Microbundle produces <code title="ECMAScript Modules (import / export)">esm</code>, <code title="CommonJS (Node-style module.exports)">cjs</code>, <code title="Universal Module Definition (works everywhere)">umd</code> bundles with your code compiled to syntax that works pretty much everywhere. While it's possible to customize the browser or Node versions you wish to support using a [browserslist configuration](https://github.com/browserslist/browserslist#browserslist-), the default setting is optimal and strongly recommended.
4458

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.
59+
## 🤖 Modern Mode <a name="modern"></a>
4660

4761
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:
4862

4963
```js
5064
// Our source, "src/make-dom.js":
5165
export default async function makeDom(tag, props, children) {
52-
const el = document.createElement(tag);
66+
let el = document.createElement(tag);
5367
el.append(...(await children));
5468
return Object.assign(el, props);
5569
}
@@ -66,7 +80,7 @@ Compiling the above using Microbundle produces the following `modern` and `esm`
6680

6781
```js
6882
export default async function(e, t, a) {
69-
var n = document.createElement(e);
83+
let n = document.createElement(e);
7084
n.append(...await a);
7185
return Object.assign(n, t);
7286
}
@@ -86,7 +100,9 @@ export default function(e, t, r) { try {
86100

87101
</td></tbody></table>
88102

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:
103+
This is enabled by default - all you have to do is add the field to your `package.json`.
104+
105+
<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>
90106

91107
```js
92108
{
@@ -98,15 +114,27 @@ This is enabled by default - all you have to do is add the field to your `packag
98114
}
99115
```
100116

101-
## 📦 Usage
117+
</details>
118+
119+
## 📦 Usage & Configuration <a name="usage"></a>
102120

103121
Microbundle includes two commands - `build` (the default) and `watch`. Neither require any options, but you can tailor things to suit your needs a bit if you like.
104122

105123
### `microbundle` / `microbundle build`
106124

107-
Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to locate the input file, and the `main` property for the output.
125+
Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to locate the input file, and the `main` property for the output:
126+
127+
```js
128+
{
129+
"source": "src/index.js", // input
130+
"main": "dist/my-library.js", // output
131+
"scripts": {
132+
"build": "microbundle"
133+
}
134+
}
135+
```
108136

109-
For UMD builds, microbundle will use a snake case version of the `name` field in your `package.json` as export name. This can be overridden either by providing an `amdName` key in your `package.json` or via the `--name` flag in the cli.
137+
For UMD builds, microbundle will use a snake_case version of the `name` field in your `package.json` as export name. This can be customized using an `"amdName"` key in your `package.json` or the `--name` command line argument.
110138

111139
### `microbundle watch`
112140

@@ -138,14 +166,16 @@ imports will be scoped.
138166

139167
### Specifying builds in `package.json`
140168

141-
You can specify output builds in a `package.json` as follows:
169+
Microbundle uses the fields from your `package.json` to figure out where it should place each generated bundle:
142170

143171
```
144-
"main": "dist/foo.js", // CJS bundle
145-
"umd:main": "dist/foo.umd.js", // UMD bundle
146-
"module": "dist/foo.m.js", // ES Modules bundle
147-
"source": "src/foo.js", // custom entry module (same as 1st arg to microbundle)
148-
"types": "dist/foo.d.ts", // TypeScript typings
172+
{
173+
"main": "dist/foo.js", // CommonJS bundle
174+
"umd:main": "dist/foo.umd.js", // UMD bundle
175+
"module": "dist/foo.m.js", // ES Modules bundle
176+
"esmodule": "dist/foo.modern.js", // Modern bundle
177+
"types": "dist/foo.d.ts" // TypeScript typings
178+
}
149179
```
150180

151181
### Mangling Properties
@@ -162,7 +192,7 @@ To achieve the smallest possible bundle size, libraries often wish to rename int
162192

163193
It's also possible to configure repeatable short names for each mangled property, so that every build of your library has the same output. **See the wiki for a [complete guide to property mangling in Microbundle](https://github.com/developit/microbundle/wiki/mangle.json).**
164194

165-
### All CLI Options
195+
### All CLI Options <a name="options"></a>
166196

167197
```
168198
Usage

0 commit comments

Comments
 (0)