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
1️⃣ **Install** by running: `npm i -D microbundle`
25
36
26
-
`npm i -D microbundle`
27
-
28
-
### Set up your `package.json`
37
+
2️⃣ **Set up** your `package.json`:
29
38
30
39
```js
31
40
{
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")
36
46
"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
39
49
}
40
50
}
41
51
```
42
52
43
-
## 🤖 Modern Output
53
+
3️⃣ **Try it out** by running `npm run build`.
54
+
55
+
## 💽 Output Formats <aname="formats"></a>
56
+
57
+
Microbundle produces <codetitle="ECMAScript Modules (import / export)">esm</code>, <codetitle="CommonJS (Node-style module.exports)">cjs</code>, <codetitle="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.
44
58
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 <aname="modern"></a>
46
60
47
61
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:
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 <ahref="https://twitter.com/_developit/status/1263174528974364675">being discussed</a>. You might use the "module" field.</em></summary>
90
106
91
107
```js
92
108
{
@@ -98,15 +114,27 @@ This is enabled by default - all you have to do is add the field to your `packag
98
114
}
99
115
```
100
116
101
-
## 📦 Usage
117
+
</details>
118
+
119
+
## 📦 Usage & Configuration <aname="usage"></a>
102
120
103
121
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.
104
122
105
123
### `microbundle` / `microbundle build`
106
124
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
+
```
108
136
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.
110
138
111
139
### `microbundle watch`
112
140
@@ -138,14 +166,16 @@ imports will be scoped.
138
166
139
167
### Specifying builds in `package.json`
140
168
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:
142
170
143
171
```
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
+
}
149
179
```
150
180
151
181
### Mangling Properties
@@ -162,7 +192,7 @@ To achieve the smallest possible bundle size, libraries often wish to rename int
162
192
163
193
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).**
0 commit comments