Microbundle
Microbundle is a zero-configuration bundler for small JavaScript libraries, currently at version 0.15.1. Powered by Rollup, it simplifies the build process by automatically generating optimized bundles in multiple formats: CommonJS (CJS), ECMAScript Modules (ESM), Universal Module Definition (UMD), and a 'modern' ESM bundle for contemporary browsers. It supports ESnext features, async/await, multiple entry points, TypeScript out-of-the-box, built-in Terser compression, and gzipped bundle size tracking. While not adhering to a strict release cadence, it receives frequent updates, often in the form of patch releases, indicating active maintenance. Key differentiators include its minimal setup (often just `package.json` configuration) and focus on producing tiny, performant outputs with modern browser targets by default.
Common errors
-
Error: 'source' field missing from package.json
cause Microbundle requires a `source` field in `package.json` to identify the primary entry point(s) for your library.fixAdd a `"source": "src/index.js"` (or your actual source file path) entry to your `package.json`. -
SyntaxError: Cannot use import statement outside a module
cause This typically occurs when a CommonJS consumer tries to `require()` an ESM bundle or vice-versa, or when Node.js encounters an `import` statement in a file treated as CommonJS.fixEnsure your `package.json`'s `main`, `module`, and `exports` fields correctly point to the appropriate CJS and ESM bundles. For Node.js, ensure `type: "module"` is set for ESM-only packages, or use `.mjs`/`.cjs` extensions explicitly in `exports`. -
TypeError: Cannot read properties of undefined (reading 'length') related to CSS bundling.
cause This error (or similar unexpected behavior with CSS) can occur in Microbundle versions prior to v0.15.1 when multiple entry files reference distinct CSS assets, as the bundler might fail to process all of them correctly.fixUpdate Microbundle to version `0.15.1` or newer. If the issue persists, verify your CSS import paths and ensure they are valid for bundling.
Warnings
- breaking Starting with v0.15.0, Microbundle now outputs ESM files with the `.mjs` extension when the `package.json` type is explicitly set to `"type": "module"` and the target is CJS, or if the package type is CJS and ESM is output.
- gotcha When bundling multiple entry points that reference different CSS assets, versions prior to 0.15.1 might incorrectly bundle only the CSS referenced by the first entry point, leading to missing styles in other bundles.
- gotcha Terser annotations (e.g., `/*@__NOINLINE__*/`) might have been incorrectly removed during the Babel pass in versions prior to v0.13.3, impacting optimization hints.
- gotcha Relying solely on `main` and `module` fields in `package.json` without the `exports` field can lead to incorrect module resolution, especially in modern Node.js environments or when supporting different environments (ESM vs CJS).
Install
-
npm install microbundle -
yarn add microbundle -
pnpm add microbundle
Imports
- microbundle
import microbundle from 'microbundle'; // or const microbundle = require('microbundle');/* Microbundle is a CLI tool. Use via package.json scripts or npx. */
- build command
import { build } from 'microbundle';npm run build // or npx microbundle
- watch command
import { watch } from 'microbundle';npm run dev // (where 'dev' script is 'microbundle watch') // or npx microbundle watch
Quickstart
{
"name": "my-tiny-lib",
"version": "1.0.0",
"type": "module",
"source": "src/index.js",
"exports": {
"require": "./dist/index.cjs",
"default": "./dist/index.modern.js"
},
"main": "./dist/index.cjs",
"module": "./dist/index.module.js",
"unpkg": "./dist/index.umd.js",
"scripts": {
"build": "microbundle",
"dev": "microbundle watch"
},
"devDependencies": {
"microbundle": "^0.15.0"
}
}
// src/index.js
export const greet = (name) => `Hello, ${name}!`;
export default function sum(a, b) {
return a + b;
}
// To run:
// 1. npm install
// 2. npm run build
// This will generate dist/index.cjs, dist/index.modern.js, dist/index.module.js, dist/index.umd.js