tsdown
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesetsdown - The Elegant Library Bundler
tsdown - 优雅的库打包工具
Blazing-fast bundler for TypeScript/JavaScript libraries powered by Rolldown and Oxc.
由Rolldown和Oxc驱动的、速度极快的TypeScript/JavaScript库打包工具。
When to Use
适用场景
- Building TypeScript/JavaScript libraries for npm
- Generating TypeScript declaration files (.d.ts)
- Bundling for multiple formats (ESM, CJS, IIFE, UMD)
- Optimizing bundles with tree shaking and minification
- Migrating from tsup with minimal changes
- Building React, Vue, Solid, or Svelte component libraries
- 为npm构建TypeScript/JavaScript库
- 生成TypeScript声明文件(.d.ts)
- 打包为多种格式(ESM、CJS、IIFE、UMD)
- 通过Tree Shaking和代码压缩优化包体积
- 从tsup迁移,只需少量改动
- 构建React、Vue、Solid或Svelte组件库
Quick Start
快速开始
bash
undefinedbash
undefinedInstall
安装
pnpm add -D tsdown
pnpm add -D tsdown
Basic usage
基础用法
npx tsdown
npx tsdown
With config file
使用配置文件
npx tsdown --config tsdown.config.ts
npx tsdown --config tsdown.config.ts
Watch mode
监听模式
npx tsdown --watch
npx tsdown --watch
Migrate from tsup
从tsup迁移
npx tsdown-migrate
undefinednpx tsdown-migrate
undefinedBasic Configuration
基础配置
ts
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['./src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
})ts
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['./src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
})Core References
核心参考
| Topic | Description | Reference |
|---|---|---|
| Getting Started | Installation, first bundle, CLI basics | guide-getting-started |
| Configuration File | Config file formats, multiple configs, workspace | option-config-file |
| CLI Reference | All CLI commands and options | reference-cli |
| Migrate from tsup | Migration guide and compatibility notes | guide-migrate-from-tsup |
| Plugins | Rolldown, Rollup, Unplugin support | advanced-plugins |
| Hooks | Lifecycle hooks for custom logic | advanced-hooks |
| Programmatic API | Build from Node.js scripts | advanced-programmatic |
| Rolldown Options | Pass options directly to Rolldown | advanced-rolldown-options |
| CI Environment | CI detection, | advanced-ci |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| 快速入门 | 安装、首次打包、CLI基础 | guide-getting-started |
| 配置文件 | 配置文件格式、多配置、工作区 | option-config-file |
| CLI参考 | 所有CLI命令和选项 | reference-cli |
| 从tsup迁移 | 迁移指南和兼容性说明 | guide-migrate-from-tsup |
| 插件 | 支持Rolldown、Rollup、Unplugin | advanced-plugins |
| 钩子 | 用于自定义逻辑的生命周期钩子 | advanced-hooks |
| 程序化API | 通过Node.js脚本构建 | advanced-programmatic |
| Rolldown选项 | 直接传递选项给Rolldown | advanced-rolldown-options |
| CI环境 | CI检测、'ci-only'/'local-only'参数 | advanced-ci |
Build Options
构建选项
| Option | Usage | Reference |
|---|---|---|
| Entry points | | option-entry |
| Output formats | | option-output-format |
| Output directory | | option-output-directory |
| Type declarations | | option-dts |
| Target environment | | option-target |
| Platform | | option-platform |
| Tree shaking | | option-tree-shaking |
| Minification | | option-minification |
| Source maps | | option-sourcemap |
| Watch mode | | option-watch-mode |
| Cleaning | | option-cleaning |
| Log level | | option-log-level |
| 选项 | 用法 | 参考链接 |
|---|---|---|
| 入口文件 | | option-entry |
| 输出格式 | | option-output-format |
| 输出目录 | | option-output-directory |
| 类型声明 | | option-dts |
| 目标环境 | | option-target |
| 平台 | | option-platform |
| Tree Shaking | | option-tree-shaking |
| 代码压缩 | | option-minification |
| 源映射 | | option-sourcemap |
| 监听模式 | | option-watch-mode |
| 清理输出 | | option-cleaning |
| 日志级别 | | option-log-level |
Dependency Handling
依赖处理
| Feature | Usage | Reference |
|---|---|---|
| External deps | | option-dependencies |
| Inline deps | | option-dependencies |
| Auto external | Automatic peer/dependency externalization | option-dependencies |
| 特性 | 用法 | 参考链接 |
|---|---|---|
| 外部依赖 | | option-dependencies |
| 内联依赖 | | option-dependencies |
| 自动外部化 | 自动将peer依赖/依赖设为外部 | option-dependencies |
Output Enhancement
输出增强
| Feature | Usage | Reference |
|---|---|---|
| Shims | | option-shims |
| CJS default | | option-cjs-default |
| Package exports | | option-package-exports |
| CSS handling | [experimental] Still in development | option-css |
| Unbundle mode | | option-unbundle |
| Package validation | | option-lint |
| 特性 | 用法 | 参考链接 |
|---|---|---|
| 兼容性垫片 | | option-shims |
| CJS默认导出 | | option-cjs-default |
| Package导出 | | option-package-exports |
| CSS处理 | [实验性] 仍在开发中 | option-css |
| 非打包模式 | | option-unbundle |
| 包验证 | | option-lint |
Framework & Runtime Support
框架与运行时支持
| Framework | Guide | Reference |
|---|---|---|
| React | JSX transform, Fast Refresh | recipe-react |
| Vue | SFC support, JSX | recipe-vue |
| WASM | WebAssembly modules via | recipe-wasm |
| 框架 | 指南 | 参考链接 |
|---|---|---|
| React | JSX转换、Fast Refresh | recipe-react |
| Vue | SFC支持、JSX | recipe-vue |
| WASM | 通过 | recipe-wasm |
Common Patterns
常见配置示例
Basic Library Bundle
基础库打包
ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
})ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
clean: true,
})Multiple Entry Points
多入口文件
ts
export default defineConfig({
entry: {
index: 'src/index.ts',
utils: 'src/utils.ts',
cli: 'src/cli.ts',
},
format: ['esm', 'cjs'],
dts: true,
})ts
export default defineConfig({
entry: {
index: 'src/index.ts',
utils: 'src/utils.ts',
cli: 'src/cli.ts',
},
format: ['esm', 'cjs'],
dts: true,
})Browser Library (IIFE/UMD)
浏览器端库(IIFE/UMD)
ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['iife'],
globalName: 'MyLib',
platform: 'browser',
minify: true,
})ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['iife'],
globalName: 'MyLib',
platform: 'browser',
minify: true,
})React Component Library
React组件库
ts
export default defineConfig({
entry: ['src/index.tsx'],
format: ['esm', 'cjs'],
dts: true,
external: ['react', 'react-dom'],
plugins: [
// React Fast Refresh support
],
})ts
export default defineConfig({
entry: ['src/index.tsx'],
format: ['esm', 'cjs'],
dts: true,
external: ['react', 'react-dom'],
plugins: [
// React Fast Refresh支持
],
})Preserve Directory Structure
保留目录结构
ts
export default defineConfig({
entry: ['src/**/*.ts', '!**/*.test.ts'],
unbundle: true, // Preserve file structure
format: ['esm'],
dts: true,
})ts
export default defineConfig({
entry: ['src/**/*.ts', '!**/*.test.ts'],
unbundle: true, // 保留文件结构
format: ['esm'],
dts: true,
})CI-Aware Configuration
感知CI环境的配置
ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
failOnWarn: 'ci-only',
publint: 'ci-only',
attw: 'ci-only',
})ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
failOnWarn: 'ci-only',
publint: 'ci-only',
attw: 'ci-only',
})WASM Support
WASM支持
ts
import { wasm } from 'rolldown-plugin-wasm'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [wasm()],
})ts
import { wasm } from 'rolldown-plugin-wasm'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [wasm()],
})Advanced with Hooks
钩子进阶用法
ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
hooks: {
'build:before': async (context) => {
console.log('Building...')
},
'build:done': async (context) => {
console.log('Build complete!')
},
},
})ts
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
hooks: {
'build:before': async (context) => {
console.log('正在构建...')
},
'build:done': async (context) => {
console.log('构建完成!')
},
},
})Configuration Features
配置特性
Multiple Configs
多配置
Export an array for multiple build configurations:
ts
export default defineConfig([
{
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
},
{
entry: ['src/cli.ts'],
format: ['esm'],
platform: 'node',
},
])导出数组以使用多个构建配置:
ts
export default defineConfig([
{
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
},
{
entry: ['src/cli.ts'],
format: ['esm'],
platform: 'node',
},
])Conditional Config
条件配置
Use functions for dynamic configuration:
ts
export default defineConfig((options) => {
const isDev = options.watch
return {
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
minify: !isDev,
sourcemap: isDev,
}
})使用函数实现动态配置:
ts
export default defineConfig((options) => {
const isDev = options.watch
return {
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
minify: !isDev,
sourcemap: isDev,
}
})Workspace/Monorepo
工作区/单体仓库
Use glob patterns to build multiple packages:
ts
export default defineConfig({
workspace: 'packages/*',
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
})使用通配符模式构建多个包:
ts
export default defineConfig({
workspace: 'packages/*',
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
dts: true,
})CLI Quick Reference
CLI快速参考
bash
undefinedbash
undefinedBasic commands
基础命令
tsdown # Build once
tsdown --watch # Watch mode
tsdown --config custom.ts # Custom config
npx tsdown-migrate # Migrate from tsup
tsdown # 单次构建
tsdown --watch # 监听模式
tsdown --config custom.ts # 使用自定义配置
npx tsdown-migrate # 从tsup迁移
Output options
输出选项
tsdown --format esm,cjs # Multiple formats
tsdown --outDir lib # Custom output directory
tsdown --minify # Enable minification
tsdown --dts # Generate declarations
tsdown --format esm,cjs # 多种格式
tsdown --outDir lib # 自定义输出目录
tsdown --minify # 启用代码压缩
tsdown --dts # 生成类型声明
Entry options
入口选项
tsdown src/index.ts # Single entry
tsdown src/*.ts # Glob patterns
tsdown src/a.ts src/b.ts # Multiple entries
tsdown src/index.ts # 单个入口
tsdown src/*.ts # 通配符模式
tsdown src/a.ts src/b.ts # 多个入口
Development
开发相关
tsdown --watch # Watch mode
tsdown --sourcemap # Generate source maps
tsdown --clean # Clean output directory
undefinedtsdown --watch # 监听模式
tsdown --sourcemap # 生成源映射
tsdown --clean # 清理输出目录
undefinedBest Practices
最佳实践
-
Always generate type declarations for TypeScript libraries:ts
{ dts: true } -
Externalize dependencies to avoid bundling unnecessary code:ts
{ external: [/^react/, /^@myorg\//] } -
Use tree shaking for optimal bundle size:ts
{ treeshake: true } -
Enable minification for production builds:ts
{ minify: true } -
Add shims for better ESM/CJS compatibility:ts
{ shims: true } // Adds __dirname, __filename, etc. -
Auto-generate package.json exports:ts
{ exports: true } // Creates proper exports field -
Use watch mode during development:bash
tsdown --watch -
Preserve structure for utilities with many files:ts
{ unbundle: true } // Keep directory structure -
Validate packages in CI before publishing:ts
{ publint: 'ci-only', attw: 'ci-only' }
-
始终为TypeScript库生成类型声明:ts
{ dts: true } -
将依赖设为外部,避免打包不必要的代码:ts
{ external: [/^react/, /^@myorg\//] } -
启用Tree Shaking以优化包体积:ts
{ treeshake: true } -
为生产构建启用代码压缩:ts
{ minify: true } -
添加兼容性垫片以提升ESM/CJS兼容性:ts
{ shims: true } // 添加__dirname、__filename等变量 -
自动生成package.json的exports字段:ts
{ exports: true } // 创建规范的exports字段 -
开发时使用监听模式:bash
tsdown --watch -
对于包含多个文件的工具库,保留目录结构:ts
{ unbundle: true } // 保持目录结构 -
在CI环境中验证包后再发布:ts
{ publint: 'ci-only', attw: 'ci-only' }
Resources
相关资源
- Documentation: https://tsdown.dev
- GitHub: https://github.com/rolldown/tsdown
- Rolldown: https://rolldown.rs
- Migration Guide: https://tsdown.dev/guide/migrate-from-tsup