What is tsup?

T

teaganga

Guest
tsup is a super-fast JavaScript/TypeScript bundler built on esbuild. It’s designed for libraries: minimal config, great defaults, and it can spit out ESM + CJS builds (and type declarations) in one go.

Why people use tsup​

  • πŸš€ Speed (esbuild under the hood)
  • 🧩 Zero/low config (works from the CLI or a tiny config file)
  • πŸ“¦ Dual output (esm + cjs) for broad compatibility
  • πŸ“ Type defs: can emit .d.ts with dts: true
  • πŸ”§ Nice extras: watch mode, minify, treeshake, code splitting, sourcemaps

Quick start​


Code:
npm i -D tsup typescript

package.json


Code:
{
  "scripts": {
    "build": "tsup",
    "dev": "tsup --watch",
    "prepare": "npm run build"
  }
}

tsup.config.ts


Code:
import { defineConfig } from 'tsup';

export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm', 'cjs'],
  dts: true,        // emit .d.ts
  sourcemap: true,
  clean: true,
  target: 'es2020',
  treeshake: true
});

tsconfig.json (minimal)


Code:
{
  "compilerOptions": {
    "target": "ES2020",
    "moduleResolution": "bundler",
    "strict": true,
    "declaration": true,
    "emitDeclarationOnly": false,
    "skipLibCheck": true
  },
  "include": ["src"]
}

Run:


Code:
npm run build

You’ll get dist/index.mjs, dist/index.cjs, and dist/index.d.ts.

How it compares​

  • tsc: compiles TS β†’ JS, but doesn’t bundle (each file out). tsup bundles your code (and can mark deps as external).
  • rollup/webpack: very flexible; more config. tsup is β€œrollup-lite” for libsβ€”fast and simple.
  • vite: great for apps/dev servers; you can build libs, but tsup is often simpler for publishing packages.

When to use / not use​

  • Use tsup for publishing libraries quickly with dual builds + types.
  • If you need exotic bundling (custom plugins, unusual module targets), rollup/webpack may still be better.
  • If you want no bundling at all, just tsc might suffice.

Conclusion​


That’s itβ€”tsup = fast, simple library bundling with type definitions baked in.

Continue reading...
 


Join 𝕋𝕄𝕋 on Telegram
Channel PREVIEW:
Back
Top