| name | tsdown |
| description | Bundle TypeScript and JavaScript libraries with blazing-fast speed powered by Rolldown. Use when building libraries, generating type declarations, bundling for multiple formats, or migrating from tsup. |
tsdown - The Elegant Library Bundler
Blazing-fast bundler for TypeScript/JavaScript libraries powered by Rolldown and Oxc.
When to Use
- Building TypeScript/JavaScript libraries for npm
- Generating TypeScript declaration files (.d.ts)
- Bundling for multiple formats (ESM, CJS, IIFE, UMD)
- Optimizing bundles with tree shaking and minification
- Migrating from tsup with minimal changes
- Building React, Vue, Solid, or Svelte component libraries
Quick Start
pnpm add -D tsdown
npx tsdown
npx tsdown --config tsdown.config.ts
npx tsdown --watch
npx tsdown-migrate
Basic Configuration
import { defineConfig } from "tsdown";
export default defineConfig({
entry: ["./src/index.ts"],
format: ["esm", "cjs"],
dts: true,
clean: true,
});
Core References
Build Options
| Option | Usage | Reference |
|---|
| Entry points | entry: ['src/*.ts', '!**/*.test.ts'] | option-entry |
| Output formats | format: ['esm', 'cjs', 'iife', 'umd'] | option-output-format |
| Output directory | outDir: 'dist', outExtensions | option-output-directory |
| Type declarations | dts: true, dts: { sourcemap, compilerOptions, vue } | option-dts |
| Target environment | target: 'es2020', target: 'esnext' | option-target |
| Platform | platform: 'node', platform: 'browser' | option-platform |
| Tree shaking | treeshake: true, custom options | option-tree-shaking |
| Minification | minify: true, minify: 'dce-only' | option-minification |
| Source maps | sourcemap: true, 'inline', 'hidden' | option-sourcemap |
| Watch mode | watch: true, watch options | option-watch-mode |
| Cleaning | clean: true, clean patterns | option-cleaning |
| Log level | logLevel: 'silent', failOnWarn: 'ci-only' | option-log-level |
Dependency Handling
Output Enhancement
Framework & Runtime Support
Common Patterns
Basic Library Bundle
export default defineConfig({
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
clean: true,
});
Multiple Entry Points
export default defineConfig({
entry: {
index: "src/index.ts",
utils: "src/utils.ts",
cli: "src/cli.ts",
},
format: ["esm", "cjs"],
dts: true,
});
Browser Library (IIFE/UMD)
export default defineConfig({
entry: ["src/index.ts"],
format: ["iife"],
globalName: "MyLib",
platform: "browser",
minify: true,
});
React Component Library
export default defineConfig({
entry: ["src/index.tsx"],
format: ["esm", "cjs"],
dts: true,
external: ["react", "react-dom"],
plugins: [
],
});
Preserve Directory Structure
export default defineConfig({
entry: ["src/**/*.ts", "!**/*.test.ts"],
unbundle: true,
format: ["esm"],
dts: true,
});
CI-Aware Configuration
export default defineConfig({
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
failOnWarn: "ci-only",
publint: "ci-only",
attw: "ci-only",
});
WASM Support
import { wasm } from "rolldown-plugin-wasm";
import { defineConfig } from "tsdown";
export default defineConfig({
entry: ["src/index.ts"],
plugins: [wasm()],
});
Advanced with Hooks
export default defineConfig({
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
hooks: {
"build:before": async (context) => {
console.log("Building...");
},
"build:done": async (context) => {
console.log("Build complete!");
},
},
});
Configuration Features
Multiple Configs
Export an array for multiple build configurations:
export default defineConfig([
{
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
},
{
entry: ["src/cli.ts"],
format: ["esm"],
platform: "node",
},
]);
Conditional Config
Use functions for dynamic configuration:
export default defineConfig((options) => {
const isDev = options.watch;
return {
entry: ["src/index.ts"],
format: ["esm", "cjs"],
minify: !isDev,
sourcemap: isDev,
};
});
Workspace/Monorepo
Use glob patterns to build multiple packages:
export default defineConfig({
workspace: "packages/*",
entry: ["src/index.ts"],
format: ["esm", "cjs"],
dts: true,
});
CLI Quick Reference
tsdown
tsdown --watch
tsdown --config custom.ts
npx tsdown-migrate
tsdown --format esm,cjs
tsdown --outDir lib
tsdown --minify
tsdown --dts
tsdown src/index.ts
tsdown src/*.ts
tsdown src/a.ts src/b.ts
tsdown --watch
tsdown --sourcemap
tsdown --clean
Best Practices
-
Always generate type declarations for TypeScript libraries:
{
dts: true;
}
-
Externalize dependencies to avoid bundling unnecessary code:
{
external: [/^react/, /^@myorg\//];
}
-
Use tree shaking for optimal bundle size:
{
treeshake: true;
}
-
Enable minification for production builds:
{
minify: true;
}
-
Add shims for better ESM/CJS compatibility:
{
shims: true;
}
-
Auto-generate package.json exports:
{
exports: true;
}
-
Use watch mode during development:
tsdown --watch
-
Preserve structure for utilities with many files:
{
unbundle: true;
}
-
Validate packages in CI before publishing:
{ publint: 'ci-only', attw: 'ci-only' }
Resources