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
Copy file name to clipboardExpand all lines: README.md
+64-35Lines changed: 64 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,17 +36,18 @@
36
36
37
37
2️⃣ **Set up** your `package.json`:
38
38
39
-
```js
39
+
```jsonc
40
40
{
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
-
"exports":"./dist/foo.modern.js", // path to the modern output (see below)
45
-
"module":"dist/foo.module.js", // where to generate the ESM bundle
46
-
"unpkg":"dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
41
+
"name":"foo", // your package name
42
+
"type":"module",
43
+
"source":"src/foo.js", // your source code
44
+
"exports":"./dist/foo.modern.js", // where to generate the modern bundle (see below)
45
+
"main":"./dist/foo.cjs", // where to generate the CommonJS bundle
46
+
"module":"./dist/foo.module.js", // where to generate the ESM bundle
47
+
"unpkg":"./dist/foo.umd.js", // where to generate the UMD bundle (also aliased as "umd:main")
47
48
"scripts": {
48
-
"build":"microbundle", // compiles "source" to "main"/"module"/"unpkg"
49
-
"dev":"microbundle watch"// re-build when source files change
49
+
"build":"microbundle", // compiles "source" to "main"/"module"/"unpkg"
50
+
"dev":"microbundle watch"// re-build when source files change
50
51
}
51
52
}
52
53
```
@@ -55,11 +56,18 @@
55
56
56
57
## 💽 Output Formats <aname="formats"></a>
57
58
58
-
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.
59
+
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.
60
+
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.
59
61
60
62
## 🤖 Modern Mode <aname="modern"></a>
61
63
62
-
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:
64
+
In addition to the above formats, Microbundle also outputs a `modern` bundle specially designed to work in _all modern browsers_.
65
+
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.
66
+
Specifically, it uses Babel's ["bugfixes" mode](https://babeljs.io/blog/2020/03/16/7.9.0#babelpreset-envs-bugfixes-option-11083httpsgithubcombabelbabelpull11083)
67
+
(previously known as [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.
68
+
The result is generally smaller and faster to execute than the plain `esm` bundle.
69
+
70
+
Take the following source code for example:
63
71
64
72
```js
65
73
// Our source, "src/make-dom.js":
@@ -135,38 +143,65 @@ The `"exports"` field can also be an object for packages with multiple entry mod
135
143
136
144
## 📦 Usage & Configuration <aname="usage"></a>
137
145
138
-
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.
146
+
Microbundle includes two commands - `build` (the default) and `watch`.
147
+
Neither require any options, but you can tailor things to suit your needs a bit if you like.
148
+
149
+
-**`microbundle`** – bundles your code once and exits. (alias: `microbundle build`)
150
+
-**`microbundle watch`** – bundles your code, then re-bundles when files change.
139
151
140
152
> ℹ️ Microbundle automatically determines which dependencies to inline into bundles based on your `package.json`.
141
153
>
142
154
> Read more about [How Microbundle decides which dependencies to bundle](https://github.com/developit/microbundle/wiki/How-Microbundle-decides-which-dependencies-to-bundle), including some example configurations.
143
155
144
-
### `microbundle` / `microbundle build`
156
+
### Configuration
145
157
146
-
Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to locate the input file, and the `main`, `umd:main`, `module` and `exports` properties to figure out where it should place each generated bundle:
158
+
Unless overridden via the command line, microbundle uses the `source` property in your `package.json` to determine which of your JavaScript files to start bundling from (your "entry module").
159
+
The filenames and paths for generated bundles in each format are defined by the `main`, `umd:main`, `module` and `exports` properties in your `package.json`.
When deciding which bundle to use, Node.js 12+ and webpack 5+ will prefer the `exports` property, while older Node.js releases use the `main` property, and other bundlers prefer the `module` field. For more information about the meaning of the different properties, refer to the [Node.js documentation](https://nodejs.org/api/packages.html#packages_package_entry_points).
175
+
When deciding which bundle to use, Node.js 12+ and webpack 5+ will prefer the `exports` property, while older Node.js releases use the `main` property, and other bundlers prefer the `module` field.
176
+
For more information about the meaning of the different properties, refer to the [Node.js documentation](https://nodejs.org/api/packages.html#packages_package_entry_points).
177
+
178
+
For UMD builds, microbundle will use a camelCase version of the `name` field in your `package.json` as export name.
179
+
Alternatively, this can be explicitly by adding an `"amdName"` key in your `package.json`, or passing the `--name` command line argument.
164
180
165
-
For UMD builds, microbundle will use a camelCase 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.
181
+
### Additional Configuration Options
166
182
167
-
### `microbundle watch`
183
+
Config also can be overridded by the [`publishConfig`](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#publishconfig) property in your `package.json`.
168
184
169
-
Acts just like `microbundle build`, but watches your source files and rebuilds on any change.
185
+
```jsonc
186
+
{
187
+
"main":"src/index.ts", // this would be used in the dev environment (e.g. Jest)
188
+
"publishConfig": {
189
+
"source":"src/index.js", // input
190
+
"main":"dist/my-library.js", // output
191
+
},
192
+
"scripts": {
193
+
"build":"microbundle"
194
+
}
195
+
}
196
+
```
197
+
198
+
### Building a single bundle with fixed output name
199
+
200
+
By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:
To achieve the smallest possible bundle size, libraries often wish to rename internal object properties or class members to smaller names - transforming `this._internalIdValue` to `this._i`. Microbundle doesn't do this by default, however it can be enabled by creating a `mangle.json` file (or a `"mangle"` property in your package.json). Within that file, you can specify a regular expression pattern to control which properties should be mangled. For example: to mangle all property names beginning an underscore:
218
245
219
-
```json
246
+
```jsonc
220
247
{
221
248
"mangle": {
222
249
"regex":"^_"
@@ -236,6 +263,8 @@ The `--define` option can be used to inject or replace build-time constants when
0 commit comments