Skip to content

Exports

This plugin automatically generates and updates the exports field in your package.json file after each build.

Bunup handles mapping all entry points to their corresponding output files, including ESM/CJS formats and type declarations. The exports field stays perfectly in sync with your build configuration always - no manual updates needed when you make any change to config.

Usage

bunup.config.ts
ts
import { defineConfig } from 'bunup';
import { exports } from 'bunup/plugins';

export default defineConfig({
	entry: ['src/index.ts'],
	format: ['esm', 'cjs'],
	plugins: [exports()],
});

When using the exports plugin, your package.json will be automatically updated with the correct exports field each time you build. For example:

package.json
json
{
  "name": "my-package",
  "version": "1.0.0",
  "type": "module",
  "files": [
    "dist"
  ],
  "main": "./dist/index.cjs",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs",
      "types": "./dist/index.d.ts"
    }
  }
}

Options

customExports

The customExports option allows you to specify additional export fields that will be preserved alongside the automatically generated exports. This is useful when you need custom export conditions or paths that aren't automatically generated by the build process.

bunup.config.ts
ts
import { defineConfig } from 'bunup';
import { exports } from 'bunup/plugins';

export default defineConfig({
	entry: ['src/index.ts'],
	format: ['esm', 'cjs'],
	plugins: [
		exports({
			customExports: (ctx) => ({
				'./package.json': './package.json',
			})
		})
	],
});

exclude

The exclude option allows you to prevent specific entry points from being included in the exports field. You can provide either an array of glob patterns or exact entry point names, or a function that returns such an array.

bunup.config.ts
ts
import { defineConfig } from 'bunup';
import { exports } from 'bunup/plugins';

export default defineConfig({
	entry: ['src/index.ts', 'src/cli.ts', 'src/plugins.ts'],
	format: ['esm', 'cjs'],
	plugins: [
		exports({
			exclude: ['src/cli.ts']
		})
	],
});

You can also use glob patterns:

bunup.config.ts
ts
import { defineConfig } from 'bunup';
import { exports } from 'bunup/plugins';

export default defineConfig({
	entry: ['src/index.ts', 'src/cli/*.ts', 'src/plugins.ts'],
	format: ['esm', 'cjs'],
	plugins: [
		exports({
			exclude: ['src/cli/*']
		})
	],
});

For more dynamic control, you can use a function:

bunup.config.ts
ts
import { defineConfig } from 'bunup';
import { exports } from 'bunup/plugins';

export default defineConfig({
	entry: ['src/index.ts', 'src/cli.ts', 'src/plugins.ts'],
	format: ['esm', 'cjs'],
	plugins: [
		exports({
			exclude: (ctx) => {
				// Access build context information
				const { options, output, meta } = ctx;
				return ['src/cli.ts'];
			}
		})
	],
});

Released under the MIT License.