# bunchee bunchee is a zero-config TypeScript & JavaScript bundler designed to create optimized Node.js and browser-friendly packages. It focuses on simplicity, speed, and developer-friendly defaults, making it an excellent choice for bundling modern JavaScript libraries. ## Latest Release: bunchee 7.0.0 - Up to 5.3x faster than bunchee 6.12.2 on a 57-entry build with declaration output (median of seven warm runs on Node.js 24.18). - New package lint checks detect invalid export-condition ordering, missing declared outputs, and unsafe `workspace:` dependency ranges before publishing. - Package preparation is ESM-first and uses standards-based package exports. Release notes: https://github.com/huozhi/bunchee/releases/tag/v7.0.0 ### Migrating from bunchee 6 - bunchee 7 requires Node.js 22.12 or newer. - The default JavaScript target is ES2022. Pass `--target` or configure `compilerOptions.target` when an older output target is required. - The `bunchee` package itself is ESM. Replace CommonJS `require('bunchee')` calls with ESM imports. - `bunchee prepare` generates ESM-only packages by default. Pass `--cjs` to generate both ESM and CommonJS exports. - The old `--prepare` build flag has been removed. Run `bunchee prepare` explicitly. - TypeScript 7 projects must install `@typescript/typescript6` for declaration generation. Complete migration guide: https://github.com/huozhi/bunchee/blob/main/docs/MIGRATION.md ## Key Features - **Zero Configuration**: Bundles projects with sensible defaults out of the box. - **Tree Shaking**: Removes unused code to produce smaller bundles. - **TypeScript Support**: Handles `.ts` and `.tsx` files seamlessly. - **ESM and CJS Outputs**: Supports ESM-only, CJS-only, and dual-module packages. - **Source Maps**: Supports source map generation for easier debugging. - **CSS/JSON Asset Bundling**: Automatically processes non-JavaScript assets for compatibility. - **Raw File Imports**: Import any file as string content using `.txt`/`.data` extensions or `?raw` query parameter. - **Server Components**: Supports React Server Components and Server Actions with `"use client"` and `"use server"` directives, automatically splitting code into separate chunks. - **Node Binary Support**: Builds executable CLI tools from `bin` field in package.json with automatic source file matching. - **Wildcard Exports**: Supports wildcard patterns in package.json exports field for dynamic subpath exports. - **Fast Build Times**: Built with optimized tooling for rapid iteration. ## Installation Install bunchee as a development dependency in your project: ```bash npm install --save-dev bunchee typescript ``` bunchee 7 requires Node.js 22.12 or newer and targets ES2022 by default. TypeScript 7 users must also install the official compatibility API package: ```bash npm install --save-dev @typescript/typescript6 ``` Migrating from bunchee 6? See the [bunchee 7 migration guide](https://github.com/huozhi/bunchee/blob/main/docs/MIGRATION.md). ## Usage ### Build Build a project: ```sh mkdir src && touch ./src/index.ts && touch package.json ``` Add the package metadata in `package.json`. ```json { "name": "coffee", "type": "module", "main": "./dist/index.js", "scripts": { "build": "bunchee" } } ``` Run build: ```sh npm run build ``` ### CLI Bundle a JavaScript or TypeScript entry file: ```bash bunchee src/index.js --output dist/bundle.js ``` ### API Use bunchee programmatically in your Node.js scripts: ```javascript import path from 'node:path' import { bundle } from 'bunchee' await bundle(path.resolve('src/index.js'), { file: path.resolve('dist/bundle.js'), }) ``` ## Example Configuration Although bunchee requires no config, you can customize options via CLI flags or programmatically: - Specify input and output files - Enable/disable source maps - Choose output formats (ESM, CJS) ## Asset Handling ### Text Files Import files as string content using `.txt` or `.data` extensions: ```javascript import data from './content.txt' // Imports as string ``` ### Raw Imports Import any file as raw text using the `?raw` query parameter: ```javascript import readme from './README.md?raw' // Markdown as string import config from './config.json?raw' // JSON as string (not parsed) import styles from './styles.css?raw' // CSS as string ``` ## Server Components bunchee supports building React Server Components and Server Actions with directives like `"use client"` or `"use server"`. It automatically generates separate chunks for server or client boundaries, preserving the directives properly. When integrated with frameworks like Next.js, it correctly handles the boundaries with split chunks. Example: ```javascript // src/client.tsx 'use client' export function ClientComponent() { return