gentleeduck/duck-ui / .claude/skills/tsdown
tsdown
VerifiedBundle TypeScript and JavaScript libraries with blazing-fast speed powered by Rolldown. Use when building libraries, generating type declarations, bundling for multiple formats, or migrating from tsup.
references/option-package-exports.md
View on GitHub# Auto-Generate Package Exports
Automatically generate package.json exports from build output.
## Overview
tsdown can automatically infer and generate the `exports` field in your `package.json` based on your build outputs.
Top-level `main`, `module`, and `types` fields are not generated by default. Enable `exports.legacy` if you need those fields for older tools.
Review the generated exports before publishing, or enable publint for validation.
## Basic Usage
### CLI
```bash
tsdown --exports
```
### Config File
```ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
exports: true,
})
```
## What Gets Generated
### Single Entry
**Config:**
```ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
exports: true,
})
```
**Generated in package.json:**
```json
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}
```
### Multiple Entries
**Config:**
```ts
export default defineConfig({
entry: {
index: 'src/index.ts',
utils: 'src/utils.ts',
},
format: ['esm', 'cjs'],
dts: true,
exports: true,
})
```
**Generated in package.json:**
```json
{
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./utils": {
"types": "./dist/utils.d.ts",
"import": "./dist/utils.mjs",
"require": "./dist/utils.cjs"
}
}
}
```
## Export All Files
Include all output files, not just entry points:
```ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
exports: {
all: true,
},
})
```
**Result:** All `.mjs`, `.cjs`, and `.d.ts` files will be added to exports.
## Legacy Package Fields
Generate top-level `main`, `module`, and `types` fields for older tools:
```ts
export default defineConfig({
exports: {
legacy: true,
},
})
```
These fields are not generated by default.
## Dev-Time Source Linking
### Dev Exports
Link to source files during development:
```ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
exports: {
devExports: true,
},
})
```
**Generated:**
```json
{
"exports": {
".": "./src/index.ts" // Points to source
},
"publishConfig": {
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}
}
```
**Note:** Supported by pnpm/yarn, not npm.
### Conditional Dev Exports
Use specific condition for dev exports:
```ts
export default defineConfig({
exports: {
devExports: '@my-org/source',
},
})
```
**Generated:**
```json
{
"exports": {
".": {
"@my-org/source": "./src/index.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}
```
**Use with TypeScript customConditions:**
```json
// tsconfig.json
{
"compilerOptions": {
"customConditions": ["@my-org/source"]
}
}
```
## Custom Exports
Add custom export mappings:
```ts
export default defineConfig({
entry: ['src/index.ts'],
exports: {
customExports(pkg, context) {
// Add custom export
pkg['./foo'] = './dist/foo.js'
// Add package.json export
pkg['./package.json'] = './package.json'
return pkg
},
},
})
```
## Common Patterns
### Complete Library Setup
```ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
exports: true,
clean: true,
})
```
### Multiple Exports with Dev Mode
```ts
export default defineConfig({
entry: {
index: 'src/index.ts',
client: 'src/client.ts',
server: 'src/server.ts',
},
format: ['esm', 'cjs'],
dts: true,
exports: {
all: false, // Only entries
devExports: '@my-org/source',
},
})
```
### Monorepo Package
```ts
export default defineConfig({
workspace: 'packages/*',
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
exports: true, // Generate for each package
})
```
## Validation
### Enable Publint
Validate generated exports:
```bash
tsdown --exports --publint
```
Or in config:
```ts
export default defineConfig({
exports: true,
publint: true, // Validate exports
})
```
## Tips
1. **Review before publishing** - Check generated fields
2. **Use with publint** - Validate exports field
3. **Enable for libraries** - Especially with multiple exports
4. **Use devExports** - Better DX during development
5. **Test exports** - Verify imports work correctly
## Troubleshooting
### Exports Not Generated
- Ensure `exports: true` is set
- Check build completed successfully
- Verify output files exist
### Wrong Export Paths
- Check `outDir` configuration
- Verify entry names match expectations
- Review `format` settings
### Dev Exports Not Working
- Only supported by pnpm/yarn
- Check package manager
- Use `publishConfig` for publishing
### Types Not Exported
- Enable `dts: true`
- Ensure TypeScript is installed
- Check `.d.ts` files are generated
## CLI Examples
```bash
# Generate exports
tsdown --exports
# With publint validation
tsdown --exports --publint
# Export all files
tsdown --exports
# With dev exports
tsdown --exports
```
## Related Options
- [Entry](option-entry.md) - Configure entry points
- [Output Format](option-output-format.md) - Module formats
- [DTS](option-dts.md) - Type declarations
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |