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 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({
entries: [
"./src/index",
{
builder: "mkdist",
input: "./src/package/components/",
outDir: "./build/components",
},
],
outDir: "build",
declaration: true,
});
Or with multiple builds you can declare an array of configs:
import { defineBuildConfig } from "unbuild";
export default defineBuildConfig([
{
entries: [
"./src/index",
{
builder: "mkdist",
input: "./src/package/components/",
outDir: "./build/components",
},
],
outDir: "build",
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