Zero config bundler for npm packages
Zero-config bundler for JS/TS packages.
bunchee is a zero-configuration bundler designed to streamline package building by adhering to the exports field in your package.json. Powered by Rollup and SWC ⚡️, it generates output based on your config, supporting both CommonJS and ESModules.
By using the standard exports configuration as the single source of truth, bunchee automatically aligns entry file conventions with your exports, ensuring seamless and efficient builds.
Migrating from bunchee 6? See the bunchee 7 migration guide.
npm install --save-dev bunchee typescript[!NOTE] TypeScript 7+ compiles natively and its
typescriptpackage no longer ships the JavaScript compiler API used for generating type declarations. If you're on TypeScript 7 or above, also install the official compat API package@typescript/typescript6andbuncheewill pick it up automatically:npm install --save-dev @typescript/typescript6
Create entry files of your library and package.json.
cd ./coffee
mkdir src && touch ./src/index.ts && touch package.jsonAdd the package metadata in package.json.
{
"name": "coffee",
"type": "module",
"main": "./dist/index.js",
"scripts": {
"build": "bunchee"
}
}npm run buildThen files in src folders will be treated as entry files and match the export names in package.json.
Simply like Node.js module resolution, each export name will match the file in src/ directory.
Here's a example of entry files and exports configuration:
| File | Exports Name |
|---|---|
src/index.ts |
"." (default export) |
src/lite.ts |
"./lite" |
src/react/index.ts |
"./react" |
{
"name": "coffee",
"scripts": {
"build": "bunchee",
},
"type": "module",
"exports": {
// entry: ./src/index.ts
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
},
// entry: ./src/lite.ts
"./lite": "./dist/lite.js",
// entry: ./src/react/index.ts
"./react": "./dist/react.js",
},
}bunchee supports wildcard patterns in the exports field to automatically generate exports for multiple files:
{
"exports": {
".": "./dist/index.js",
"./features/*": "./dist/features/*.js"
}
}This will automatically discover files in src/features/ and generate exports like ./features/foo, ./features/bar, etc. The wildcard * is substituted in both the export path and output path.
bunchee detects the format of each entry-point based on export condition type or the file extension. It supports the following output formats:
package.json Field |
Output format |
|---|---|
main |
Default |
types |
TypeScript declaration |
exports |
Default |
exports.require |
CommonJS |
exports.import |
Default |
exports.types |
TypeScript declaration of export |
bin |
Default |
bin.<name> |
Default |
The Default output format is determined by the file extension:
| File Extension | Output format |
|---|---|
.js |
Determined by package.json#type, CommonJS by default |
.cjs |
CommonJS |
.mjs |
ECMAScript Modules |
The dependencies and peerDependencies will be marked as externalized and wont be included in the bundle. If you want to include them in the bundle, you can use the --no-external option. Or you can import the devDependencies in your source code to bundle them.
{
// Externalized
"dependencies": {
/* ... */
},
"peerDependencies": {
/* ... */
},
// Bundled
"devDependencies": {
/* ... */
},
}For exports condition like react-native, react-server and edge-light as they're special platforms, they could have different exports or different code conditions. In this case bunchee provides an override input source file convention if you want to build them as different code bundle.
For instance:
{
"exports": {
"react-server": "./dist/react-server.mjs",
"edge-light": "./dist/edge-light.mjs",
"import": "./dist/index.mjs",
},
}bunchee supports both TypeScript paths config and Node.js imports field in package.json for path aliasing. It will resolve the path alias to the correct file path. If you're using modern TypeScript versions, you can also directly configure the imports field in package.json and it will work as a charm.
// package.json
{
"imports": {
"#util": "./src/utils.ts",
},
}To build executable files with the bin field in package.json, bunchee requires you to create the bin directory under src directory. The source file matching will be same as the entry files convention.
For example:
|- src/
|- bin/
|- index.tsThis will match the bin field in package.json as:
{
"bin": "./dist/bin.js",
}If you have multiple binaries, you can create multiple files under the bin directory. Check the below example for more details.
For named executable files, you can create multiple files under the bin directory.
|- src/
|- bin/
This will match the bin field in package.json as:
{
"bin": {
"foo": "./dist/bin/a.js",
"bar": "./dist/bin/b.js",
},
}Note: For multiple
binfiles, the filename should match the key name in thebinfield.
bunchee supports bundling native Node.js addon files (.node binaries). When you import a .node file, it will be copied to the output directory and the import will be rewritten to load it at runtime.
// src/index.js
import addon from './native-addon.node'bunchee supports building React Server Components and Server Actions with directives like "use client" or "use server". It generates separate chunks for the server or client boundaries. When integrated to framework like Next.js, it can correctly handles the boundaries with the split chunks.
Sometimes, you may want to share a chunk across multiple bundles without promoting it to separate entries or exports, such as single instance of React context module, shared utils, etc. In these cases, shared modules will help you achieve the goal. Files or directories prefixed with an underscore (_<name>.<ext> or _<name>/**) will be treated as shared modules.
These conventions are kept private and are not going to be treat as shared modules or entry points. For example, test or mock files like _foo/a.test.ts will be ignored and not included as shared modules.
// src/_util.js
export function sharedUtil() {
/* ... */
}You can then use them in different entry files:
// src/index.js
import { sharedUtil } from './_util'// src/lite.js
import { sharedUtil } from './_util'bunchee will bundle the shared module into a separate chunk, keeping it private and ensuring it's referenced by multiple entry bundles.
For scenarios involving multiple runtime bundles, such as default and react-server, modules that need to be shared and remain as a single instance across different runtime bundles can also follow this convention. The leading underscore (_) ensures that these modules are private to your application while facilitating reuse.
'use client'
// src/_app-context.js
export const AppContext = React.createContext(null)These modules can be imported in various runtime entry files:
// src/index.js
import { AppContext } from './_app-context'// src/index.react-server.js
import { AppContext } from './_app-context'The _app-context module will be bundled into a shared chunk that exists as a single instance across different runtime bundles.
This convention keeps shared modules private while enabling efficient bundling and reuse across your codebase.
bunchee CLI provides few options to create different bundles or generating types. Call bunchee --help to see the help information in the terminal.
Here are the available options for the CLI:
cd <project-root-dir>
# Build based on the package.json configuration
bunchee --runtime node -o ./dist/bundle.js
# Specify the input source file
bunchee ./src/foo.ts -f esm --target es2022 -o ./dist/foo.jsbunchee --external=dep1,dep2,dep3Replace dep1, dep2, and dep3 with the names of the dependencies you want to exclude from the bundle.
To bundle your library without external dependencies, use the --no-external option:
bunchee --no-externalThis will include all dependencies within your output bundle.
A command to be executed after a build is successful can be specified using the --success option, which is useful for development watching mode:
bunchee --watch --success "node ./dist/index.js"# Use bunchee to prepare package.json configuration
npm exec bunchee prepare
# "If you're using other package manager such as pnpm"
# pnpm bunchee prepare
# "Or use with npx"
# npx bunchee@latest prepareOr you can checkout the following cases to configure your package.json.
JavaScript ESModuleThen use use the exports field in package.json to configure different conditions and leverage the same functionality as other bundlers, such as webpack. The exports field allows you to define multiple conditions.
{
"files": ["dist"],
"type": "module",
"exports": {
".": "./dist/es/index.js",
"./react": "./dist/es/react.js",
},
"scripts": {
"build": "bunchee",
},
}If you're building a TypeScript library, separate the types from the main entry file and specify the types path in packag
No open issues yet, or sync has not completed.