0Pricing
React Academy · Lesson

Bundling with Rollup and tsup for Libraries

Configure Rollup or tsup to bundle React components with proper externalization of React and ReactDOM.

Bundling with Rollup and tsup for Libraries is a free React Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Not Use Vite for Libraries

Vite's default configuration is optimized for applications — it bundles everything, handles assets, and generates an index.html. For a component library, you need a different output: pure ESM/CJS JavaScript files with no runtime overhead, no bundled React, and proper TypeScript declarations. Vite library mode works but Rollup and tsup are more purpose-built for this.

Rollup: The Library Standard

Rollup has been the standard bundler for JavaScript libraries for years. It produces clean, efficient output with excellent tree-shaking and supports both ESM and CJS output formats. Rollup's scope hoisting eliminates module wrapper overhead, producing the smallest possible library bundles.

tsup: Zero-Config Alternative

tsup is a zero-config TypeScript bundler powered by esbuild. It is dramatically faster than Rollup (esbuild compiles in microseconds vs milliseconds). The minimal config: { entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true } produces both module formats and TypeScript declarations automatically.

tsup Key Config Options

The core tsup options: entry is the array of entry point files. format specifies output formats: ['esm', 'cjs']. dts: true generates .d.ts TypeScript declaration files alongside the JS output. target sets the minimum JavaScript environment (esnext or es2020). splitting: false keeps output in single files.

esbuild vs Rollup Tradeoffs

esbuild (via tsup) compiles 10-100x faster than Rollup. Rollup produces slightly more optimized output with better tree-shaking granularity and more control over chunk boundaries. For most component libraries, tsup's speed wins in development. For maximum tree-shaking control or complex multi-entry setups, Rollup is preferred.

Marking React as External

The most critical bundler configuration for a React library: never bundle React or ReactDOM. Mark them as external so they are excluded from your output. If your library bundles React, consumers will end up with two copies of React — one from your bundle and one from their app. This breaks all React hooks due to multiple React instances.

Configuring Externals in tsup

In tsup config, use the external option: external: ['react', 'react-dom']. In Rollup config, set external: ['react', 'react-dom', 'react/jsx-runtime']. Include react/jsx-runtime if your library uses JSX Transform. Also externalize any other peer dependencies your library declares.

Rollup Plugins for TypeScript

A minimal Rollup config for a TypeScript library requires three plugins: @rollup/plugin-typescript for TypeScript compilation, @rollup/plugin-node-resolve to resolve node_modules imports, and @rollup/plugin-commonjs to convert CommonJS dependencies to ESM for the bundle. Add rollup-plugin-dts for declaration files.

Output Directory Structure

A typical dual-format library output: dist/esm/index.js and dist/esm/index.d.ts for ESM consumers, dist/cjs/index.js and dist/cjs/index.cjs for CJS consumers. The package.json exports field maps these paths to the correct environments. TypeScript declaration files live alongside their JS counterparts.

Multiple Entry Points

Large libraries benefit from multiple entry points so consumers only import what they need. For example: entry: ['src/index.ts', 'src/utils.ts', 'src/hooks.ts']. Each entry gets its own output files. The package.json exports field maps each public path to its output: './utils''./dist/utils.js'.

Build Script in package.json

Add the build command to package.json scripts: "build": "tsup". tsup reads its config from tsup.config.ts in the root. The prepublish script should run the build: "prepublishOnly": "npm run build" ensures your package is always built before publishing to npm.

React External in Library Bundle

Why must React be marked as external when bundling a React component library?

Lesson Recap: Library Bundling

tsup (esbuild-powered) provides fast zero-config bundling with format: ['esm', 'cjs'] and dts: true. Rollup offers more fine-grained tree-shaking control with @rollup/plugin-typescript. The critical rule: always mark react, react-dom, and all peer dependencies as external to prevent bundling them. Output to dist/esm/ and dist/cjs/ with parallel TypeScript declarations. Multiple entry points enable granular tree-shaking for consumers.

Frequently asked questions

Is the “Bundling with Rollup and tsup for Libraries” lesson free?

Yes — the full text of “Bundling with Rollup and tsup for Libraries” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Bundling with Rollup and tsup for Libraries”?

Configure Rollup or tsup to bundle React components with proper externalization of React and ReactDOM. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bundling with Rollup and tsup for Libraries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Bundling with Rollup and tsup for Libraries
  2. ESM and CJS Dual Package Output
  3. Peer Dependencies and Tree Shaking
  4. Publishing to npm and Semantic Versioning
← Back to React Academy