Zero-configuration bundler for tiny modules.
The zero-configuration bundler for tiny modules, powered by Rollup.
Guide → Setup ✯ Formats ✯ Modern Mode ✯ Usage & Configuration ✯ All Options
package.jsoncli.js + index.js, etc)1️⃣ Install by running: npm i -D microbundle
2️⃣ Set up your package.json:
…3️⃣ Try it out by running npm run build.
Microbundle produces esm, cjs, umd 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, the default setting is optimal and strongly recommended.
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 Babel's "bugfixes" mode
(previously known as 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 plain esm bundle.
Take the following source code for example:
// Our source, "src/make-dom.js":
export default async function makeDom(tag, props, children) {
let el = document.createElement(tag);
el.append(...(await children));
return Object.assign(el, props);
}Compiling the above using Microbundle produces the following modern and esm bundles:
make-dom.modern.js (117b) |
make-dom.module.js (194b) |
|---|---|
| |
This is enabled by default. All you have to do is add an "exports" field to your package.json:
{
"main": "./dist/foo.umd.js", // legacy UMD output (for Node & CDN use)
"module": "./dist/foo.module.mjs", // legacy ES Modules output (for bundlers)
"exports": "./dist/foo.modern.mjs", // modern ES2017 output
"scripts": {
"build": "microbundle src/foo.js"
}
}The "exports" field can also be an object for packages with multiple entry modules:
{
"name": "foo",
"exports": {
".": "./dist/foo.modern.mjs", // import "foo" (the default)
"./lite": "./dist/lite.modern.mjs", // import "foo/lite"
"./full": "./dist/full.modern.mjs" // import "foo/full"
},
"scripts": {
"build": "microbundle src/*.js" // build foo.js, lite.js and full.js
}
}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.
microbundle – bundles your code once and exits. (alias: microbundle build)microbundle watch – bundles your code, then re-bundles when files change.ℹ️ Microbundle automatically determines which dependencies to inline into bundles based on your
package.json.Read more about How Microbundle decides which dependencies to bundle, including some example configurations.
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").
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.
{
"source": "src/index.js", // input
"main": "dist/foo.js", // CommonJS output bundle
"umd:main": "dist/foo.umd.js", // UMD output bundle
"module": "dist/foo.mjs", // ES Modules output bundle
"exports": {
"types": "./dist/foo.d.ts", // TypeScript typings for NodeNext modules
"require": "./dist/foo.js", // CommonJS output bundle
"default": "./dist/foo.modern.mjs", // Modern ES Modules output bundle
},
"types": "dist/foo.d.ts" // TypeScript typings
}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.
For UMD builds, microbundle will use a camelCase version of the name field in your package.json as export name.
Alternatively, this can be explicitly set by adding an "amdName" key in your package.json, or passing the --name command line argument.
{"type":"module"} in package.jsonNode.js 12.16+ adds a new "ES Module package", which can be enabled by adding {"type":"module"} to your package.json.
This property changes the default source type of .js files to be ES Modules instead of CommonJS.
When using {"type":"module"}, the file extension for CommonJS bundles generated by Microbundle must be changed to .cjs:
{
"type": "module",
"module": "dist/foo.js", // ES Module bundle
"main": "dist/foo.cjs", // CommonJS bundle
}Config also can be overridded by the publishConfig property in your package.json.
{
"main": "src/index.ts", // this would be used in the dev environment (e.g. Jest)
"publishConfig": {
"source": "src/index.js", // input
"main": "dist/my-library.js", // output
},
"scripts": {
"build": "microbundle"
}
}By default Microbundle outputs multiple bundles, one bundle per format. A single bundle with a fixed output name can be built like this:
microbundle -i lib/main.js -o dist/bundle.js --no-pkg-main -f umdJust point the input to a .ts file through either the cli or the source key in your package.json and you’re done.
Microbundle will generally respect your TypeScript config defined in a tsconfig.json file with notable exceptions being the "target" and "module" settings. To ensure your TypeScript configuration matches the configuration that Microbundle uses internally it's strongly recommended that you set "module": "ESNext" and "target": "ESNext" in your tsconfig.json.
To ensure Microbundle does not process extraneous files, by default it only includes your entry point. If you want to include other files for compilation, such as ambient declarations, make sure to add either "files" or "include" into your tsconfig.json.
If you're using TypeScript with CSS Modules, you will want to set "include": ["node_modules/microbundle/index.d.ts"] in your tsconfig.json to tell TypeScript how to handle your CSS Module imports.
To ensure that your module's .d.ts type info is visible to other TypeScript projects that use moduleResolution: 'NodeNext', add a types key to your package.json's corresponding exports mapping.
Importing CSS files is supported via import "./foo.css". By default, generated CSS output is written to disk. The --css inline command line option will inline generated CSS into your bundles as a string, returning the CSS string from the import:
// with the default external CSS:
import './foo.css'; // generates a minified .css file in the output directory
// with `microbundle --css inline`:
import css from './foo.css';
console.log(css); // the generated minified stylesheetCSS Modules: CSS files with names ending in .module.css are treated as a CSS Modules.
To instead treat imported .css files as modules, run Microbundle with --css-modules true. To disable CSS Modules for your project, pass --no-css-modules or --css-modules false.
The default scope name for CSS Modules is_[name]__[local]__[hash:base64:5] in watch mode, and _[hash:base64:5] for production builds.
This can be customized by passing the command line argument --css-modules "[name]_[hash:base64:7]", using these fields and naming conventions.
| flag | import | is css module? |
|---|---|---|
| null | import './my-file.css'; | :x: |
| null | import './my-file.module.css'; | :white_check_mark: |
| false | import './my-file.css'; | :x: |
| false | import './my-file.module.css'; | :x: |
| true | import './my-file.css'; | :white_check_mark: |
| true | import './my-file.module.css'; | :white_check_mark: |
Microbundle is able to detect and bundle Module Workers when generating bundles in the
esm and modern formats. To use this feature, instantiate your Web Worker as follows:
worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
// or simply:
worker = new Worker('./worker.js', { type: 'module' });... then add the --workers flag to your build command:
Security vulnerability via transitive dependency on `serialize-javascript` via `rollup-plugin-terser`
[Bug] build failed on deconstructing rest parameters
Transpiling local package dependencies that use TS file as entry point
package.json types/main/module take precedence over conditional exports
Disable minification by default
[Feature request] Use sourcemap files from imports
react-jsx does not work with Preact
Simultaneous builds are unreliable
IIFE uses the same name format as CJS?
Error: Unexpected token when including a file with `declare namespace` in it.