Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

unbuild

unjs401.2kMIT3.3.1TypeScript support: included

A unified javascript build system

readme

unbuild

npm version npm downloads

A unified JavaScript build system

📦 Optimized bundler

Robust rollup based bundler that supports TypeScript and generates commonjs and module formats + type declarations.

🪄 Automated config

Automagically infer build config and entries from package.json.

📁 Bundleless build

Integration with mkdist for generating bundleless dists with file-to-file transpilation.

✨ Passive watcher

Stub dist once using unbuild --stub (powered by jiti) and you can try and link your project without needing to watch and rebuild during development.

✍ Untype Generator

Integration with untyped.

✔️ Secure builds

Automatically check for various build issues such as potential missing and unused dependencies and fail CI.

CLI output also includes output size and exports for quick inspection.

Usage

Create src/index.ts:

export const log = (...args) => {
  console.log(...args);
};

Update package.json:

{
  "type": "module",
  "scripts": {
    "build": "unbuild",
    "prepack": "unbuild"
  },
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.cjs"
    }
  },
  "main": "./dist/index.cjs",
  "types": "./dist/index.d.ts",
  "files": ["dist"]
}

Note You can find a more complete example in unjs/template for project setup.

Build with unbuild:

npx unbuild

Configuration is automatically inferred from fields in package.json mapped to src/ directory. For more control, continue with next section.

Configuration

Create build.config.ts:

export default {
  entries: ["./src/index"],
};

You can either use unbuild key in package.json or build.config.{js,cjs,mjs,ts,mts,cts,json} to specify configuration.

See options here.

Example:

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  // If entries is not provided, will be automatically inferred from package.json
  entries: [
    // default
    "./src/index",
    // mkdist builder transpiles file-to-file keeping original sources structure
    {
      builder: "mkdist",
      input: "./src/package/components/",
      outDir: "./build/components",
    },
  ],

  // Change outDir, default is 'dist'
  outDir: "build",

  // Generates .d.ts declaration file
  declaration: true,
});

Or with multiple builds you can declare an array of configs:

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig([
  {
    // If entries is not provided, will be automatically inferred from package.json
    entries: [
      // default
      "./src/index",
      // mkdist builder transpiles file-to-file keeping original sources structure
      {
        builder: "mkdist",
        input: "./src/package/components/",
        outDir: "./build/components",
      },
    ],

    // Change outDir, default is 'dist'
    outDir: "build",

    /**
     * * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts".
     * * `node16` means "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts".
     * * `true` is equivalent to `compatible`.
     * * `false` will disable declaration generation.
     * * `undefined` will auto detect based on "package.json". If "package.json" has "types" field, it will be `"compatible"`, otherwise `false`.
     */
    declaration: "compatible",
  },
  {
    name: "minified",
    entries: ["./src/index"],
    outDir: "build/min",
    rollup: {
      esbuild: {
        minify: true,
      },
    },
  },
]);

Recipes

Decorators support

In build.config.ts

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  rollup: {
    esbuild: {
      tsconfigRaw: {
        compilerOptions: {
          experimentalDecorators: true,
        },
      },
    },
  },
});

Generate sourcemaps

import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
  sourcemap: true,
});

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.3.1

compare changes

🩹 Fixes

  • rollup: Improve external detection (#488)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.3.0

compare changes

🚀 Enhancements

  • Allow specifying dependencies to inline in inlineDependencies (#480)

🩹 Fixes

  • rollup: Resolve aliases using pathe utils (#483)

💅 Refactors

  • Inline withTrailingSlash util (#482)

🏡 Chore

❤️ Contributors

v3.2.0

compare changes

🚀 Enhancements

  • mkdist: Fail build if mkdist errors in creating declarations (#473)

🩹 Fixes

  • Correct .d.cts default export type (#458)

🏡 Chore

❤️ Contributors

v3.1.0

compare changes

🚀 Enhancements

  • Support parallel builds (af19b1b)
  • Infer externals from package name, exports and imports (#469)
  • rollup: Resolve with production condition (#470)

🩹 Fixes

  • Resolve preset on directory (#465)
  • Only externalize @types/ from devDependencies (#471)

🏡 Chore

  • Remove unused imports (#463)
  • Apply automated updates (f724382)

❤️ Contributors

v3.0.1

compare changes

🩹 Fixes

  • rollup: De-default default export from stub (#455)

🏡 Chore

❤️ Contributors

v3.0.0

See github release notes.

v3.0.0-rc.11

compare changes

🩹 Fixes

  • untyped: Use schema module default export if is the only export (cc26726)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.10

compare changes

🚀 Enhancements

  • Add --config to the CLI (#440)

🩹 Fixes

  • Update to jiti 2.3 (9147c3e)
  • Untyped declaration output is optional (5820182)

📖 Documentation

  • Add more usage info (#401)

🏡 Chore

❤️ Contributors

v3.0.0-rc.9

compare changes

🩹 Fixes

  • stub: Enable interopDefault by default (8e6f7e4)
  • config: Only take default export (fefafec)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.8

compare changes

🩹 Fixes

  • Normalize resolved path (2640083)

💅 Refactors

  • Replace fast-glob with tinyglobby (#426)

📖 Documentation

  • Fix tiny typo (#414)

🏡 Chore

❤️ Contributors

v3.0.0-rc.7

compare changes

💅 Refactors

  • Replace globby w/ fast-glob (#418)

🏡 Chore

❤️ Contributors

v3.0.0-rc.6

compare changes

🩹 Fixes

  • untyped: Use custom jiti instance (00ded57)

🏡 Chore

  • Eslint ignore test/fixture/dist (66a8db3)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.5

compare changes

🩹 Fixes

  • rollup: Keep empty type-only modules (7a6469b)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.4

compare changes

🩹 Fixes

  • rollup: Keep empty (type-only) modules (a9158e2)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.3

compare changes

🚀 Enhancements

  • Upgrade to jiti v2 beta (#409)

🩹 Fixes

  • Type RollupOptions.plugins as array (62fa930)
  • Add all loader extensions to esbuild include (8ab22ff)
  • Enable jiti interopDefault for config loader and internals (ref: #409) (#409)

💅 Refactors

  • Overhaul builders structure (#410)
  • Add explicit return types (#412)
  • Use colors from consola/utils (6a8f36d)

🏡 Chore

  • Upgrade @rollup/plugin-commonjs to 26 (9392ec3)
  • Update deps (d886ad5)
  • Update release script (8ce4090)
  • Remove unused imports (04c2b5c)
  • Add explicit type for var exports (af26e40)
  • Stricter type checks (#413)
  • Add publishTag to release script (577484c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.0-rc.2

compare changes

🚀 Enhancements

  • Support copy builder (#389)
  • Experimental active watcher for rollup (#364)
  • rollup: Add .mts and .cts to supported extensions (633ffe9)
  • Support custom jiti plugins for stub mode (#368)

🩹 Fixes

  • Generate stub types of with explicit extension import if pkg type is module (#371)
  • Correct dts generation for stub mode (#314)
  • rollup: Handle aliases when checking for externals (#384)
  • rollup: Update importAttributesKey to with (27bcba8)

📖 Documentation

  • Add more jsdocs (#363)
  • Add examples (#334)

🏡 Chore

🤖 CI

❤️ Contributors

v3.0.0-rc.1

compare changes

🚀 Enhancements

  • ⚠️ Upgrade to rollup v4 (#327)
  • Support disabling preserveDynamicImports (#322)
  • rollup: ⚠️ Default to esnext build target (#335)

🩹 Fixes

  • Don't clean root directory if set as outDir (#343)

💅 Refactors

  • Use jiti.import for esm stubs and improve templates (#300)

📖 Documentation

  • Update jsdocs for inferEntries (#310)

🏡 Chore

⚠️ Breaking Changes

  • ⚠️ Upgrade to rollup v4 (#327)
  • rollup: ⚠️ Default to esnext build target (#335)

❤️ Contributors

v2.0.0

compare changes

🏡 Chore

🤖 CI

  • Use conventional commit for autofix (#294)

❤️ Contributors

v2.0.0-rc.0

compare changes

🚀 Enhancements

  • Generate .d.mts and .d.cts declarations (#273)
  • Support multiple build configs (#275)
  • Support nested subpaths (#280)
  • Allow customizing jiti options for stub entries (#282)
  • Support aliases for stub mode (#283)
  • Migrate to unjs/citty (#284)
  • cli: Support --minify (#285)
  • Sourcemap support (#286)

🩹 Fixes

  • Respect directory separator when matching entries (#214)
  • esbuild: Typo in sourcemap option (#259)
  • Remove extensions from default output name (#279)
  • rollup: Check module id against externals as well (#270)
  • esbuild: Apply minify with corresponding loader (#254)
  • esbuild: Do not minify declarations (83b3ed2)
  • mkdist: Allow passing all possible options (f40a889)
  • Clean dist directories only once for multi builds (11c47c8)
  • auto: Avoid warning for existing files (#287)

💅 Refactors

  • rollup: ⚠️ Improve esbuild options handling (#278)

📦 Build

  • ⚠️ Make typescript a peer dependency (f31c6a4)

🏡 Chore

⚠️ Breaking Changes

  • rollup: ⚠️ Improve esbuild options handling (#278)
  • ⚠️ Make typescript a peer dependency (f31c6a4)

❤️ Contributors

v1.2.1

compare changes

💅 Refactors

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.2.0

compare changes

🚀 Enhancements

  • rollup: Add jsx and tsx to esbuild loader defaults (#198)
  • rollup: Allow regular expressions in externals array (#145)
  • mkdist: Add new pattern option (#139)
  • Support esbuild charset option (#190)
  • rollup: Allow passing any all common esbuild options (8e81e2a)
  • rollup: Show size of bundled npm packages in cli output (#243)

🩹 Fixes

  • Pass missing esbuild jsx factory options (#224)
  • rollup: Handle array format for rollup.alias.entries option (#220)
  • rollup: Enable interop: compat for cjs compatibility (#215)

📖 Documentation

  • Add types to default exports (#226)
  • Remove types field suggestion for now (e8988ae)

🏡 Chore

❤️ Contributors

v1.1.2

compare changes

💅 Refactors

  • Use fs.mkdir instead of mkdirp (6ee6384)
  • Use fs.mkdir instead of mkdirp (8e6962e)

🏡 Chore

❤️ Contributors

v1.1.1

compare changes

🩹 Fixes

  • Use fs instead of rimraf (bbd374d)

❤️ Contributors

v1.1.0

compare changes

🚀 Enhancements

  • Update all rollup dependencies (3d1b976)

🩹 Fixes

  • Prevent minification of .d.ts files (#185)

🎨 Styles

❤️ Contributors

v1.0.2

compare changes

🩹 Fixes

  • Do not throw error on absolute windows paths (#166)

🏡 Chore

  • Remove unused dependency (79cc03d)

❤️ Contributors

1.0.1 (2022-11-16)

1.0.0 (2022-11-16)

0.8.9 (2022-08-18)

Bug Fixes

  • rollup: remove wrong context (67d1d38)

0.8.8 (2022-08-11)

Bug Fixes

  • rollup: seperate dynamic chunks from shared chunk names (aaf8227)

0.8.7 (2022-08-10)

Bug Fixes

  • include all files for total size report (7bce76d)

0.8.6 (2022-08-10)

0.8.5 (2022-08-10)

Features

  • name option defaulting to package name (c3979ab)
  • rollup: use hashed chunk names (850b013)

Bug Fixes

  • rollup: unmark chunk names as external imports (59debad)

0.8.4 (2022-08-10)

Bug Fixes

  • rollup: handle stubbing multiple exports (f215525)

0.8.3 (2022-08-10)

Bug Fixes

  • rollup: escape stub import paths (63a3c11)
  • rollup: fix stub type hints (38d95be)
  • rollup: normalize stub entry path (f7272e0)

0.8.2 (2022-08-10)

Bug Fixes

  • rollup: fix stub export resolution issues (63961e4)

0.8.1 (2022-08-10)

Bug Fixes

0.8.0 (2022-08-10)

⚠ BREAKING CHANGES

  • always enable esmResolve for jiti (stub and config)
  • exit with code (1) on build warnings (#98)

Features

  • always enable esmResolve for jiti (stub and config) (b87c8df)
  • exit with code (1) on build warnings (#98) (ffc0d7c)
  • rollup: generated named exports in esm stub (c9fce24)

0.7.6 (2022-07-20)

Bug Fixes

  • pkg: use default export condition to avoid breaking change (858d35d), closes #89

0.7.5 (2022-07-20)

Features

  • enable esmResolve when loading build.config (#93) (c856812)

Bug Fixes

  • pkg: add types field for exports (#89) (457f043)
  • properly calculate bytes of output size (#82) (1888978)

0.7.4 (2022-04-13)

Bug Fixes

0.7.3 (2022-04-12)

Bug Fixes

  • resolve asbolute path to jiti for pnpm support (#58) (81d6da7)
  • stub: use file:// protocol for windows compatibility (12a99c1)
  • work around issue building with pnpm (#57) (eb7da84)

0.7.2 (2022-03-25)

Bug Fixes

  • revert builtins from default externals (0b808c6)

0.7.1 (2022-03-25)

Bug Fixes

  • add builtins and node: prefixes to externals (2af233d)

0.7.0 (2022-03-09)

⚠ BREAKING CHANGES

  • update all dependencies

Features

  • make rollup-plugin-dts options configurable (#52) (e710503)
  • update all dependencies (fc7c164)

0.6.9 (2022-01-27)

Features

  • allow peerDeps as import entry points (#43) (755981d)

0.6.8 (2022-01-21)

Bug Fixes

  • rollup: ensure output subdirectory exists (#40) (b964655)

0.6.7 (2021-12-14)

Features

  • rollup: replace and per-plugin configuration (6d185b2)

0.6.6 (2021-12-14)

Bug Fixes

0.6.5 (2021-12-14)

Bug Fixes

  • fix nested config types to be optional (d48e0ac)

0.6.4 (2021-12-14)

Features

Bug Fixes

0.6.3 (2021-12-03)

Bug Fixes

0.6.2 (2021-12-03)

Features

0.6.1 (2021-12-03)

Features

  • allow programmatic inputConfig (9493837)

Bug Fixes

  • hooks type in config should be partial (62fd953)
  • register programmatic hooks (867ebc5)
  • resolve outDir relative to rootDir (ba18055)
  • show error if code is not MODULE_NOT_FOUND (82d9432)
  • use tryRequire for package.json (31ab840)

0.6.0 (2021-12-03)

⚠ BREAKING CHANGES

  • extract rollup specific global options

Features

Bug Fixes

  • stub: re-export default in dts stub (#26) (468347f)

  • extract rollup specific global options (0dfb39f)

0.5.13 (2021-11-18)

Features

  • upgrade to untyped 0.3.x (a7f50b8)

0.5.12 (2021-11-18)

Features

Bug Fixes

  • de-default rollup-plugin-esbuild (resolves #23) (e88fc72)

0.5.11 (2021-10-22)

Bug Fixes

  • allow cjs for ext in types (e878d1d)

0.5.10 (2021-10-21)

Bug Fixes

0.5.9 (2021-10-21)

Features

  • use json plugin to allow treeshaking (562e4d1)

0.5.8 (2021-10-20)

0.5.7 (2021-10-13)

0.5.6 (2021-10-01)

Features

0.5.5 (2021-09-29)

Bug Fixes

  • rollup: don't ignore built-in requires to transform into import (00c6b21)

0.5.4 (2021-09-21)

0.5.3 (2021-09-21)

Bug Fixes

  • enable back interopDefault for esm stub for now (079bb51)

0.5.2 (2021-09-21)

Bug Fixes

  • avoid interopDefault for esm stub (6a36080)

0.5.1 (2021-09-21)

Bug Fixes

  • rollup: add interopDefault for jiti stub (b1bf926)

0.5.0 (2021-09-20)

⚠ BREAKING CHANGES

  • use .cjs extension

Features

Bug Fixes

  • pkg: use explicit .cjs extension (909a539)
  • rollup: stub esm with jiti for typescript support (6bab21f)

0.4.2 (2021-08-09)

Bug Fixes

  • stub: use junction links for windows support (56aaf4b)

0.4.1 (2021-07-29)

0.4.0 (2021-07-22)

⚠ BREAKING CHANGES

  • upgrade untyped (#14)

  • upgrade untyped (#14) (caa57c9)

0.3.2 (2021-07-08)

Bug Fixes

  • don't warn for every src file in windows (#10) (e4d18aa)

0.3.1 (2021-06-16)

Bug Fixes

  • change esbuild target to es2019 (a91ec26)

0.3.0 (2021-05-24)

⚠ BREAKING CHANGES

  • update dependencies

  • update dependencies (293dd7f)

0.2.3 (2021-04-23)

Features

  • pass ext option to mkdist (2c9a513)

0.2.2 (2021-04-21)

Bug Fixes

  • mkdist: rmdir before stubbing (1ca50a4)

0.2.1 (2021-04-21)

Bug Fixes

  • avoid duplicate name in dist (cbc8f55)

0.2.0 (2021-04-21)

⚠ BREAKING CHANGES

  • support per-entry outDir

Features

  • support per-entry outDir (5bb7ac3)

Bug Fixes

  • enable declarations and export defineBuildConfig (0e7d62b)

0.1.13 (2021-04-16)

Bug Fixes

  • rollup config fixes (inline related) (ae8a3d3)

0.1.12 (2021-04-09)

Features

  • inlineDependencies option (9f320a1)

0.1.11 (2021-04-09)

Features

  • declaration option to generate types (936478a)

0.1.10 (2021-04-09)

Bug Fixes

  • exclude package itself from externals (617a9f9)

0.1.9 (2021-04-09)

Bug Fixes

  • pkg: move typescript to dependencies (51cd53b)

0.1.8 (2021-04-09)

Bug Fixes

  • rollup: set respectExternal option for dts (a596fa3)

0.1.7 (2021-04-09)

Bug Fixes

  • pkg: directly add typescript dependency for standalone usage (adeda2f)

0.1.6 (2021-04-09)

Bug Fixes

  • pkg: specify peerDependencies (71e8994)

0.1.5 (2021-04-09)

Bug Fixes

  • ensure parent dir for link (87e75b3)

0.1.4 (2021-04-09)

Bug Fixes

0.1.3 (2021-04-09)

Bug Fixes

  • update deps and use interopDefault (6b0f39a)

0.1.2 (2021-04-09)

Features

0.1.1 (2021-04-07)