patternjavascriptTip
tsup: zero-config TypeScript library bundler built on esbuild
Viewed 0 times
tsup 7+
tsuptypescript library builddual esm cjsdts generationesbuild library
Problem
Publishing a TypeScript library requires a complex Rollup or tsc setup to produce ESM, CJS, and type declarations. Configuration is verbose and fragile.
Solution
Use tsup — it wraps esbuild with smart defaults for library publishing. One command produces ESM, CJS, and .d.ts files.
# Install
npm install -D tsup
# tsup.config.ts
import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true, // generate .d.ts declaration files
sourcemap: true,
clean: true, // empty dist/ before each build
splitting: false, // disable code splitting for simpler output
treeshake: true,
});
# package.json scripts
{ "build": "tsup", "dev": "tsup --watch" }
# package.json exports
{
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
}
# Install
npm install -D tsup
# tsup.config.ts
import { defineConfig } from 'tsup';
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true, // generate .d.ts declaration files
sourcemap: true,
clean: true, // empty dist/ before each build
splitting: false, // disable code splitting for simpler output
treeshake: true,
});
# package.json scripts
{ "build": "tsup", "dev": "tsup --watch" }
# package.json exports
{
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
}
Why
tsup uses esbuild for speed and adds DTS (TypeScript declaration) generation via the TypeScript compiler API. It handles the dual ESM/CJS output naming conventions automatically, saving hours of Rollup config.
Gotchas
- tsup renames ESM output to .mjs and CJS to .js by default — adjust package.json exports accordingly
- dts: true runs tsc internally; ensure your tsconfig.json has 'declaration: true' semantics
- Complex Rollup plugin needs (e.g. WASM, custom loaders) require Rollup directly
- Peer dependencies must be manually listed in external — tsup does not auto-detect them
Context
Publishing a TypeScript library to npm with minimal configuration
Revisions (0)
No revisions yet.