unbuild

A unified javascript build system

README

unbuild


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 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:

  1. ```ts
  2. export const log = (...args) => {
  3.   console.log(...args);
  4. };
  5. ```

Update package.json:

  1. ```json
  2. {
  3.   "type": "module",
  4.   "exports": {
  5.     ".": {
  6.       "import": "./dist/index.mjs",
  7.       "require": "./dist/index.cjs"
  8.     }
  9.   },
  10.   "main": "./dist/index.cjs",
  11.   "types": "./dist/index.d.ts",
  12.   "files": ["dist"]
  13. }
  14. ```

Note

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


Build with unbuild:

  1. ```sh
  2. npx unbuild
  3. ```

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:

  1. ```ts
  2. export default {
  3.   entries: ["./src/index"],
  4. };
  5. ```

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:

  1. ```ts
  2. import { defineBuildConfig } from "unbuild";

  3. export default defineBuildConfig({
  4.   // If entries is not provided, will be automatically inferred from package.json
  5.   entries: [
  6.     // default
  7.     "./src/index",
  8.     // mkdist builder transpiles file-to-file keeping original sources structure
  9.     {
  10.       builder: "mkdist",
  11.       input: "./src/package/components/",
  12.       outDir: "./build/components",
  13.     },
  14.   ],

  15.   // Change outDir, default is 'dist'
  16.   outDir: "build",

  17.   // Generates .d.ts declaration file
  18.   declaration: true,
  19. });
  20. ```

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

  1. ```ts
  2. import { defineBuildConfig } from "unbuild";

  3. export default defineBuildConfig([
  4.   {
  5.     // If entries is not provided, will be automatically inferred from package.json
  6.     entries: [
  7.       // default
  8.       "./src/index",
  9.       // mkdist builder transpiles file-to-file keeping original sources structure
  10.       {
  11.         builder: "mkdist",
  12.         input: "./src/package/components/",
  13.         outDir: "./build/components",
  14.       },
  15.     ],

  16.     // Change outDir, default is 'dist'
  17.     outDir: "build",

  18.     /**
  19.      * * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts".
  20.      * * `node16` means "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts".
  21.      * * `true` is equivalent to `compatible`.
  22.      * * `false` will disable declaration generation.
  23.      * * `undefined` will auto detect based on "package.json". If "package.json" has "types" field, it will be `"compatible"`, otherwise `false`.
  24.      */
  25.     declaration: "compatible",
  26.   },
  27.   {
  28.     name: "minified",
  29.     entries: ["./src/index"],
  30.     outDir: "build/min",
  31.     rollup: {
  32.       esbuild: {
  33.         minify: true,
  34.       },
  35.     },
  36.   },
  37. ]);
  38. ```

💻 Development


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