Package Exports
- bunchee
- bunchee/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bunchee) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
bunchee
Zero-config bundler for frontend libraries.
Bunchee makes bundling your library into one file effortless, with zero configuration required. It is built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple module types (CommonJS, ESModules) simultaneously.
It uses the standard exports configuration in package.json as the only source of truth, and uses entry file conventions to match your exports and build them into bundles.
Quick Start
Installation
npm install --save-dev buncheeIf you're using TypeScript
npm install --save-dev bunchee typescriptConfiguration
Create your library entry file and package.json.
cd ./my-lib
mkdir src && touch ./src/index.tsPrepare
# Use bunchee to prepare package.json configuration
npm 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
Then 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"],
"exports": {
"import": "./dist/es/index.mjs",
"require": "./dist/cjs/index.cjs"
},
"scripts": {
"build": "bunchee"
}
}TypeScript
If you're build a TypeScript library, separate the types from the main entry file and specify the types path in package.json. When you're using .mjs or .cjs extensions with TypeScript and modern module resolution (above node16), TypeScript will require specific type declaration files like .d.mts or .d.cts to match the extension. bunchee can automatically generate them to match the types to match the condition and extensions. One example is to configure your exports like this in package.json:
{
"files": ["dist"],
"exports": {
"import": {
"types": "./dist/es/index.d.mts",
"default": "./dist/es/index.mjs"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"scripts": {
"build": "bunchee"
}
}Hybrid (CJS & ESM) Module Resolution with TypeScript
If you're using TypeScript with Node 10 and Node 16 module resolution, you can use the `types` field in package.json to specify the types path. Then `bunchee` will generate the types file with the same extension as the main entry file.{
"files": ["dist"],
"main": "./dist/cjs/index.cjs",
"module": "./dist/es/index.mjs",
"types": "./dist/cjs/index.d.ts",
"exports": {
"import": {
"types": "./dist/es/index.d.mts",
"default": "./dist/es/index.mjs"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"scripts": {
"build": "bunchee"
}
}Build
Then files in src folders will be treated as entry files and match the export names in package.json. For example:
src/index.ts will match the exports name "." or the only main export.
Now just run npm run build (or pnpm build / yarn build) if you're using these package managers, bunchee will find the entry files and build them.
The output format will based on the exports condition and also the file extension. Given an example:
- It's CommonJS for
requireand ESM forimportbased on the exports condition. - It's CommonJS for
.jsand ESM for.mjsbased on the extension regardless the exports condition. Then for export condition like "node" you could choose the format with your extension.
[!NOTE] All the
dependenciesandpeerDependencieswill be marked as external automatically and not included in the bundle. If you want to include them in the bundle, you can use the--no-externaloption.
Usage
File Conventions
While exports field is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command.
Provide entry files with the name ([name].[ext]) that matches the exported name from exports field in package.json. For instance:
<cwd>/src/index.tswill match"."export name or the if there's only one main export.<cwd>/src/lite.tswill match"./lite"export name.
The build script can be just bunchee without configure any input sources for each exports. Of course you can still specify other arguments as you need.
Briefly, the entry files from src/ folder will do matching with exports conditions from package.json and build them into bundles.
Assuming you have default export package as "." and subpath export "./lite" with different exports condition listed in package.json
{
"name": "example",
"scripts": {
"build": "bunchee"
},
"exports": {
"./lite": "./dist/lite.js"
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}Then you need to add two entry files index.ts and lite.ts in project root directory to match the export name "." and "./lite", bunchee will associate these entry files with export names then use them as input source and output paths information.
- my-lib/
|- src/
|- lite.ts
|- index.ts
|- package.jsonIt will also look up for index.<ext> file under the directory having the name of the export path. For example, if you have "./lite": "./dist/lite.js" in exports field, then it will look up for ./lite/index.js as the entry file as well.
Multiple Runtime
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"
}
}Executables
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"
}For multiple executable files, you can create multiple files under the bin directory.
|- src/
|- bin/
|- foo.ts
|- bar.tsThis 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.
Server Components
bunchee supports to build server components and server actions with library directives like "use client" or "use server". It will generate the corresponding chunks for client and server that scope the client and server boundaries properly.
Then when the library is integrated to an app such as Next.js, app bundler can transform the client components and server actions correctly and maximum the benefits.
If you're using "use client" or "use server" in entry file, then it will be preserved on top and the dist file of that entry will become a client component.
If you're using "use client" or "use server" in a file that used as a dependency for an entry, then that file containing directives be split into a separate chunk and hoist the directives to the top of the chunk.
CLI
CLI Options
bunchee CLI provides few options to create different bundles or generating types.
- Output (
-o <file>): Specify output filename. - Format (
-f <format>): Set output format (default:'esm'). - External (
--external <dep,>): Specifying extra external dependencies, by default it is the list ofdependenciesandpeerDependenciesfrompackage.json. Values are separate by comma. - Target (
--target <target>): Set ECMAScript target (default:'es2015'). - Runtime (
--runtime <runtime>): Set build runtime (default:'browser'). - Environment (
--env <env,>): Define environment variables. (default:NODE_ENV, separate by comma) - Working Directory (
--cwd <cwd>): Set current working directory where containingpackage.json. - Types (
--dts): Generate TypeScript declaration files along with assets. - Minify (
-m): Compress output. - Watch (
-w): Watch for source file changes.
cd <project-root-dir>
# specifying input, output and format
bunchee ./src/index.js -f cjs -o ./dist/bundle.js
bunchee ./src/index.js -f esm -o ./dist/bundle.esm.js
# build node.js library, or change target to es2019
bunchee ./src/index.js --runtime node --target es2019Specifying extra external dependencies
By default, bunchee will mark all the dependencies and peerDependencies as externals so you don't need to pass them as CLI args.
But if there's any dependency that used but not in the dependency list and you want to mark as external, you can use the --external option to specify them.
bunchee --external=dep1,dep2,dep3Replace dep1, dep2, and dep3 with the names of the dependencies you want to exclude from the bundle.
Bundling everything without external dependencies
To bundle your library without external dependencies, use the --no-external option:
bunchee --no-externalThis will include all dependencies within your output bundle.
Environment Variables
To pass environment variables to your bundled code, use the --env option followed by a comma-separated list of environment variable names:
bunchee --env=ENV1,ENV2,ENV3Replace ENV1, ENV2, and ENV3 with the names of the environment variables you want to include in your bundled code. These environment variables will be inlined during the bundling process.
You can use index.<export-type>.<ext> to override the input source file for specific export name. Or using <export-path>/index.<export-type>.<ext> also works. Such as:
|- src/
|- index/.ts
|- index.react-server.ts
|- index.edge-light.tsThis will match the export name "react-server" and "edge-light" then use the corresponding input source file to build the bundle.
Wildcard Exports
Bunchee implements the Node.js feature of using the asterisk * as a wildcard to match the exportable entry files.
For example:
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./*": {
"import": "./dist/*.mjs",
"require": "./dist/*.cjs"
}
}
}The asterisk * will be replaced with your entry files, such as:
- my-lib/
|- src/
|- foo/
|- index.ts
|- bar.ts
|- index.ts
|- package.jsonThis will match the export names "foo" and "bar" and will be treated as the new entries as they matched the ./* wildcard in my-lib folder.
{
"exports": {
".": {
"import": "./dist/index.js"
},
"./foo": {
"import": "./dist/foo/index.mjs",
"require": "./dist/foo/index.cjs"
},
"./bar": {
"import": "./dist/bar.mjs",
"require": "./dist/bar.cjs"
}
}
}Note: Wildcard Exports currently only supports the exports key
"./*", which will match all the available entries.
CSS
bunchee has basic CSS support for pure CSS file imports. It will be bundled into js bundle and insert the style tag into the document head when the bundle is loaded by browser.
/* src/style.css */
.foo {
color: orange;
}// src/index.tsx
import './style.css'
export const Foo = () => <div className="foo">foo</div>Text Files
If you just want to import a file as string content, you can name the extension as .txt or .data and it will be bundled as string content.
For example:
src/index.ts
import data from './data.txt'
export default datasrc/data.txt
hello worldoutput
export default "hello world"Node.js API
import path from 'path'
import { bundle, type BundleConfig } from 'bunchee'
// The definition of these options can be found in help information
await bundle(path.resolve('./src/index.ts'), {
dts: false, // Boolean
watch: false, // Boolean
minify: false, // Boolean
sourcemap: false, // Boolean
external: [], // string[]
format: 'esm', // 'esm' | 'cjs'
target: 'es2015', // ES syntax target
runtime: 'nodejs', // 'browser' | 'nodejs'
cwd: process.cwd(), // string
})Watch Mode
Bunchee offers a convenient watch mode for rebuilding your library whenever changes are made to the source files. To enable this feature, use either -w or --watch.
target
If you specify target option in tsconfig.json, then you don't have to pass it again through CLI.
Package lint
bunchee has support for checking the package bundles are matched with package exports configuration.
License
MIT