Unbuild: Unified JavaScript Build System

3.6.1 · active · verified Sun Apr 19

Unbuild is a robust, unified JavaScript build system leveraging Rollup for efficient bundling. It targets current Node.js and browser environments, producing CommonJS, ES module, and TypeScript declaration outputs. Currently at version 3.6.1, unbuild sees active development with frequent patch and minor releases. Key differentiators include automated configuration inference from `package.json`, support for bundleless distribution via `mkdist`, a passive watcher using `jiti` for rapid development cycles, and integrated 'secure builds' that detect and report missing or unused dependencies. It also features integration with `untyped` for schema generation. While actively maintained, the project has also announced experimentation with `obuild` as a potential next-generation successor, which users should be aware of for future planning.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up `unbuild` for a TypeScript project, including `src/index.ts`, the necessary `package.json` fields, an optional `build.config.ts`, and how to trigger the build from the command line.

import { defineBuildConfig } from 'unbuild';

// 1. Create src/index.ts
// This is your main entry point.
// export const log = (...args: any[]) => {
//   console.log('Hello from unbuild:', ...args);
// };

// 2. Update package.json (example excerpt):
/*
{
  "name": "my-cool-lib",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "build": "unbuild",
    "prepack": "unbuild" // Good practice to build before packing
  },
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs",
      "types": "./dist/index.d.ts"
    }
  },
  "main": "./dist/index.cjs",
  "types": "./dist/index.d.ts",
  "files": ["dist"]
}
*/

// 3. (Optional) Create build.config.ts for custom configuration:
// This file specifies build entries, output directory, etc.
export default defineBuildConfig({
  entries: [
    // Default entry point
    './src/index',
    // Example for mkdist bundleless build
    {
      builder: 'mkdist',
      input: './src/components/',
      outDir: './dist/components'
    }
  ],
  outDir: 'dist',
  declaration: true, // Generates .d.ts files
  clean: true // Cleans output directory before build
});

// 4. Run the build from your terminal:
// npx unbuild

// 5. Example usage of the built library (e.g., in another project):
// import { log } from 'my-cool-lib'; // Assuming `my-cool-lib` is installed
// log('Building amazing things!');

// const { log: cjsLog } = require('my-cool-lib');
// cjsLog('CJS module loaded!');

view raw JSON →