其他
用 tsdown 打包 TypeScript/JavaScr…
构建 npm 库、生成 .d.ts、打包 ESM/CJS/IIFE/UMD、优化包体、迁移自 tsup 或构建 React/Vue/Solid/Svelte 组件库时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15
适用场景
用 tsdown 打包 TypeScri特定
解决思路
- 构建时使用 Node.js 22.18.0 或更高版本运行 tsdown;通过 `target` 选项让打包输出支持更低的 Node.js 版本。
- 若包需要支持 Node.js 18/20,在 CI 中用 Node.js 22+ 构建,例如设置 `target: 'node18'` 或 `target: 'node20'`。
- 在打算支持的低版本 Node.js 上测试构建输出或 packed tarball,例如用矩阵任务在 Node.js 18/20/22 上运行已发布包的测试。
- 安装:`pnpm add -D tsdown`。
- 基本用法:`npx tsdown`。
- 使用配置文件:`npx tsdown --config tsdown.config.ts`。
- 监听模式:`npx tsdown --watch`。
- 从 tsup 迁移:`npx tsdown-migrate`。
- 基础配置:`defineConfig({ entry: ['./src/index.ts'], format: ['esm', 'cjs'], dts: true, clean: true })`。
- 需要完整迁移辅助和完整选项映射时,安装专用 tsdown-migrate skill:`npx skills add rolldown/tsdown --skill tsdown-migrate`。
- 入口点:`entry: ['src/*.ts', '!**/*.test.ts']`。
- 输出格式:`format: ['esm', 'cjs', 'iife', 'umd']`。
- 输出目录:`outDir: 'dist'`,`outExtensions`。
- 类型声明:`dts: true`,`dts: { sourcemap, compilerOptions, vue }`。
- 目标环境:`target: 'es2020'`,`target: 'esnext'`。
- 平台:`platform: 'node'`,`platform: 'browser'`。
- Tree shaking:`treeshake: true`,自定义选项。
- 压缩:`minify: true`,`minify: 'dce-only'`。
- Source maps:`sourcemap: true`,`'inline'`,`'hidden'`。
- Watch 模式:`watch: true`,watch 选项。
- 清理:`clean: true`,clean patterns。
- 日志级别:`logLevel: 'silent'`,`failOnWarn: false`。
- 依赖从不打包:`deps: { neverBundle: ['react', /^@myorg\//] }`。
- 依赖总是打包:`deps: { alwaysBundle: ['dep-to-bundle'] }`。
- 仅打包白名单:`deps: { onlyBundle: ['cac', 'bumpp'] }`。
- 跳过 node_modules 打包:`deps: { skipNodeModulesBundle: true }`。
- 自动 external:自动 externalize dependency/peer/optional。
- Shims:`shims: true`,添加 ESM/CJS 兼容。
- CJS default:`cjsDefault: true`(默认)/ `false`。
- Package exports:`exports: true`,生成 exports 字段。
- CSS 处理:实验性 `css: { ... }`,使用完整 pipeline(预处理器、Lightning CSS、PostCSS、CSS modules、代码分割);需要 `@tsdown/css`。
- CSS modules:`css: { modules: { localsConvention: 'camelCase' } }`,为 `.module.css` 文件生成作用域类名。
- CSS inject:`css: { inject: true }`,在 JS 输出中保留 CSS imports。
- Unbundle 模式:`unbundle: true`,保留目录结构。
- Root 目录:`root: 'src'`,控制输出目录映射。
- 可执行文件:实验性 `exe: true`,通过 `@tsdown/exe` 跨平台打包为独立可执行文件。
- 包验证:`publint: true`,`attw: true`。
- 基础库打包配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, clean: true }`。
- 多入口配置:`{ entry: { index: 'src/index.ts', utils: 'src/utils.ts', cli: 'src/cli.ts' }, format: ['esm', 'cjs'], dts: true }`。
- 浏览器库(IIFE/UMD)配置:`{ entry: ['src/index.ts'], format: ['iife'], globalName: 'MyLib', platform: 'browser', minify: true }`。
- React 组件库配置:`{ entry: ['src/index.tsx'], format: ['esm', 'cjs'], dts: true, deps: { neverBundle: ['react', 'react-dom'] }, inputOptions: { jsx: { runtime: 'automatic' } } }`。
- 保留目录结构配置:`{ entry: ['src/**/*.ts', '!**/*.test.ts'], unbundle: true, format: ['esm'], dts: true }`。
- CI 感知配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, failOnWarn: 'ci-only', publint: 'ci-only', attw: 'ci-only' }`。
- WASM 支持配置:`plugins: [wasm()]`,从 `rolldown-plugin-wasm` 导入 `wasm`。
- 带 CSS 和 Sass 的库配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, target: 'chrome100', css: { preprocessorOptions: { scss: { additionalData: '@use "src/styles/variables" as *;' } } } }`。
- 独立可执行文件配置:`{ entry: ['src/cli.ts'], exe: true }`。
- 跨平台可执行文件配置(需要 `@tsdown/exe`):`{ entry: ['src/cli.ts'], exe: { targets: [{ platform: 'linux', arch: 'x64', nodeVersion: '25.7.0' }, { platform: 'darwin', arch: 'arm64', nodeVersion: '25.7.0' }, { platform: 'win', arch: 'x64', nodeVersion: '25.7.0' }] } }`。
- 高级 Hooks 配置:`hooks: { 'build:before': async (context) => { console.log('Building...') }, 'build:done': async (context) => { console.log('Build complete!') } }`。
- 多配置:从 `defineConfig` 导出数组,每个元素是一份构建配置。
- 条件配置:`defineConfig((options) => { const isDev = options.watch; return { entry: ['src/index.ts'], format: ['esm', 'cjs'], minify: !isDev, sourcemap: isDev } })`。
- Workspace/Monorepo:`{ workspace: 'packages/*', entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true }`。
- CLI 构建一次:`tsdown`。
- CLI 监听模式:`tsdown --watch`。
- CLI 自定义配置:`tsdown --config custom.ts`。
- CLI 从 tsup 迁移:`npx tsdown-migrate`。
- CLI 多格式输出:`tsdown --format esm,cjs`。
- CLI 自定义输出目录:`tsdown -d lib`(`--out-dir`)。
- CLI 启用压缩:`tsdown --minify`。
- CLI 生成声明:`tsdown --dts`。
- CLI 打包为独立可执行文件:`tsdown --exe`。
- CLI bundleless 模式:`tsdown --unbundle`。
- CLI 单入口:`tsdown src/index.ts`。
- CLI glob 入口:`tsdown src/*.ts`。
- CLI 多入口:`tsdown src/a.ts src/b.ts`。
- CLI workspace 模式:`tsdown -W`。
- CLI 过滤特定包:`tsdown -W -F my-package`。
- CLI 按正则过滤:`tsdown --filter /^pkg-/`。
- CLI 生成 source map:`tsdown --sourcemap`。
- CLI 清理输出目录:`tsdown --clean`。
- CLI 复用 Vite 配置:`tsdown --from-vite`。
- CLI 自定义 tsconfig:`tsdown --tsconfig tsconfig.build.json`。
- 最佳实践:始终为 TypeScript 库生成类型声明:`{ dts: true }`。
- 最佳实践:externalize dependencies 以避免打包不必要代码:`{ deps: { neverBundle: [/^react/, /^@myorg\//] } }`。
- 最佳实践:使用 tree shaking 优化 bundle 大小:`{ treeshake: true }`。
- 最佳实践:为生产构建启用压缩:`{ minify: true }`。
- 最佳实践:添加 shims 以提升 ESM/CJS 兼容性:`{ shims: true }`(添加 `__dirname`、`__filename` 等)。
- 最佳实践:自动生成 package.json exports:`{ exports: true }`。
- 最佳实践:开发时使用 watch mode:`tsdown --watch`。
- 最佳实践:对包含许多文件的工具库保留结构:`{ unbundle: true }`。
- 最佳实践:在 CI 发布前验证包:`{ publint: 'ci-only', attw: 'ci-only' }`。
适用边界 · 注意事项
- 构建环境无法运行 Node.js 22.18.0 或更高版本时不适用,因为 tsdown 构建时需要该版本。
- 项目不需要打包 TypeScript/JavaScript 库、生成 .d.ts、多格式输出或从 tsup 迁移时,可不使用。
- 不能安装 tsdown(以及按需的 @tsdown/css、@tsdown/exe、rolldown-plugin-wasm 等)时不适用。