Bundlib
Bundlib is an automatic library bundler built on top of Rollup.js, designed to streamline the process of packaging JavaScript and TypeScript libraries. The current stable version is 0.21.9, with active development indicated by frequent patch releases (0.21.9, 0.21.8, 0.21.7, etc.) that introduce new features like parallel builds and improved CLI commands, alongside bug fixes. It aims to simplify Rollup configuration for common library bundling scenarios, offering both automatic configuration derived from `package.json` and advanced programmatic or file-based configuration via `defineConfig`. A key differentiator is its focus on opinionated defaults to reduce setup complexity, while still allowing fine-grained control for various build types (ESM, CJS, UMD, types). Users should note that while powerful, the project is explicitly stated to be "under development," suggesting potential for evolving APIs or unresolved edge cases. It ships with TypeScript types and requires Node.js >=18.
Common errors
-
Error: Cannot find module 'bundlib' or its corresponding type declarations.
cause Attempting to `require()` Bundlib in a CommonJS context when it's primarily an ESM package, or incorrect module resolution in a TypeScript project.fixEnsure you are using ESM `import` statements (e.g., `import { defineConfig } from 'bundlib';`). For TypeScript, verify `moduleResolution` in `tsconfig.json` supports modern ESM (e.g., `"node16"` or `"bundler"`). -
Error: Your current Node.js version is X. Bundlib requires Node.js >=18.
cause Running Bundlib with an unsupported Node.js version as specified in its `engines` field.fixUpdate your Node.js environment to version 18 or newer. Use `nvm install 18` and `nvm use 18` or similar version management tools. -
Error: Missing "input" option
cause The Rollup configuration generated by Bundlib lacks a specified entry point for bundling.fixEnsure your `bundlib.config.ts` or `package.json` configuration explicitly defines the `input` option, pointing to your library's main source file (e.g., `input: 'src/index.ts'`). -
TypeError: bundlib.defineConfig is not a function
cause Incorrect import of `defineConfig` or attempting to access it as a property of a default import.fixUse named import: `import { defineConfig } from 'bundlib';`. If using CommonJS, ensure your setup correctly transpiles ESM or update to an ESM-compatible environment.
Warnings
- gotcha Bundlib is explicitly stated to be "under development". This implies that APIs might still evolve, and users should be prepared for potential changes or edge cases as the project matures.
- breaking Bundlib requires Node.js version >=18. Running with older Node.js versions will result in execution errors.
- breaking CLI commands for `build` and `watch` were significantly refactored and introduced in version 0.21.5, potentially changing how users interact with the bundler from the command line.
- gotcha Upgrades to underlying Rollup plugins (e.g., `@rollup/plugin-commonjs` fix in v0.21.4) can introduce subtle breaking changes or require configuration adjustments, particularly for browser builds.
Install
-
npm install bundlib -
yarn add bundlib -
pnpm add bundlib
Imports
- defineConfig
const { defineConfig } = require('bundlib');import { defineConfig } from 'bundlib' - bundlib
const bundlib = require('bundlib').bundlib;import { bundlib } from 'bundlib' - BundlibConfig
import { BundlibConfig } from 'bundlib'import type { BundlibConfig } from 'bundlib'
Quickstart
/* bundlib.config.ts */
import { defineConfig } from 'bundlib';
export default defineConfig({
input: 'src/index.ts', // Your main entry point
output: [
{ format: 'es', file: 'dist/index.mjs', sourcemap: true },
{ format: 'cjs', file: 'dist/index.cjs', sourcemap: true },
{ format: 'umd', file: 'dist/index.umd.js', name: 'MyLibrary', sourcemap: true }
],
// Optional: Define external dependencies to prevent bundling them
external: ['lodash'],
// Optional: Configure TypeScript for type declarations
project: 'tsconfig.json',
// Optional: Enable minification for production builds
min: process.env.NODE_ENV === 'production'
});
/* package.json */
// Add a script to your package.json
// "scripts": {
// "build": "bundlib build",
// "watch": "bundlib watch"
// }
// To run the build:
// npm install --save-dev bundlib rollup typescript
// npm run build