← 返回基因库
其他

用 tsdown 打包 TypeScript/JavaScr…

构建 npm 库、生成 .d.ts、打包 ESM/CJS/IIFE/UMD、优化包体、迁移自 tsup 或构建 React/Vue/Solid/Svelte 组件库时使用。

被复用0 次
复用有效—
置信度55%
沉淀时间2026-09-23
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
来自 shane.jiang 的小呱沉淀
GDI C 35/100

进化评分(GDI)

综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」

内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 39/100 ×0.15

适用场景

用 tsdown 打包 TypeScri特定

解决思路

  1. 构建时使用 Node.js 22.18.0 或更高版本运行 tsdown;通过 `target` 选项让打包输出支持更低的 Node.js 版本。
  2. 若包需要支持 Node.js 18/20,在 CI 中用 Node.js 22+ 构建,例如设置 `target: 'node18'` 或 `target: 'node20'`。
  3. 在打算支持的低版本 Node.js 上测试构建输出或 packed tarball,例如用矩阵任务在 Node.js 18/20/22 上运行已发布包的测试。
  4. 安装:`pnpm add -D tsdown`。
  5. 基本用法:`npx tsdown`。
  6. 使用配置文件:`npx tsdown --config tsdown.config.ts`。
  7. 监听模式:`npx tsdown --watch`。
  8. 从 tsup 迁移:`npx tsdown-migrate`。
  9. 基础配置:`defineConfig({ entry: ['./src/index.ts'], format: ['esm', 'cjs'], dts: true, clean: true })`。
  10. 需要完整迁移辅助和完整选项映射时,安装专用 tsdown-migrate skill:`npx skills add rolldown/tsdown --skill tsdown-migrate`。
  11. 入口点:`entry: ['src/*.ts', '!**/*.test.ts']`。
  12. 输出格式:`format: ['esm', 'cjs', 'iife', 'umd']`。
  13. 输出目录:`outDir: 'dist'`,`outExtensions`。
  14. 类型声明:`dts: true`,`dts: { sourcemap, compilerOptions, vue }`。
  15. 目标环境:`target: 'es2020'`,`target: 'esnext'`。
  16. 平台:`platform: 'node'`,`platform: 'browser'`。
  17. Tree shaking:`treeshake: true`,自定义选项。
  18. 压缩:`minify: true`,`minify: 'dce-only'`。
  19. Source maps:`sourcemap: true`,`'inline'`,`'hidden'`。
  20. Watch 模式:`watch: true`,watch 选项。
  21. 清理:`clean: true`,clean patterns。
  22. 日志级别:`logLevel: 'silent'`,`failOnWarn: false`。
  23. 依赖从不打包:`deps: { neverBundle: ['react', /^@myorg\//] }`。
  24. 依赖总是打包:`deps: { alwaysBundle: ['dep-to-bundle'] }`。
  25. 仅打包白名单:`deps: { onlyBundle: ['cac', 'bumpp'] }`。
  26. 跳过 node_modules 打包:`deps: { skipNodeModulesBundle: true }`。
  27. 自动 external:自动 externalize dependency/peer/optional。
  28. Shims:`shims: true`,添加 ESM/CJS 兼容。
  29. CJS default:`cjsDefault: true`(默认)/ `false`。
  30. Package exports:`exports: true`,生成 exports 字段。
  31. CSS 处理:实验性 `css: { ... }`,使用完整 pipeline(预处理器、Lightning CSS、PostCSS、CSS modules、代码分割);需要 `@tsdown/css`。
  32. CSS modules:`css: { modules: { localsConvention: 'camelCase' } }`,为 `.module.css` 文件生成作用域类名。
  33. CSS inject:`css: { inject: true }`,在 JS 输出中保留 CSS imports。
  34. Unbundle 模式:`unbundle: true`,保留目录结构。
  35. Root 目录:`root: 'src'`,控制输出目录映射。
  36. 可执行文件:实验性 `exe: true`,通过 `@tsdown/exe` 跨平台打包为独立可执行文件。
  37. 包验证:`publint: true`,`attw: true`。
  38. 基础库打包配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, clean: true }`。
  39. 多入口配置:`{ entry: { index: 'src/index.ts', utils: 'src/utils.ts', cli: 'src/cli.ts' }, format: ['esm', 'cjs'], dts: true }`。
  40. 浏览器库(IIFE/UMD)配置:`{ entry: ['src/index.ts'], format: ['iife'], globalName: 'MyLib', platform: 'browser', minify: true }`。
  41. React 组件库配置:`{ entry: ['src/index.tsx'], format: ['esm', 'cjs'], dts: true, deps: { neverBundle: ['react', 'react-dom'] }, inputOptions: { jsx: { runtime: 'automatic' } } }`。
  42. 保留目录结构配置:`{ entry: ['src/**/*.ts', '!**/*.test.ts'], unbundle: true, format: ['esm'], dts: true }`。
  43. CI 感知配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, failOnWarn: 'ci-only', publint: 'ci-only', attw: 'ci-only' }`。
  44. WASM 支持配置:`plugins: [wasm()]`,从 `rolldown-plugin-wasm` 导入 `wasm`。
  45. 带 CSS 和 Sass 的库配置:`{ entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true, target: 'chrome100', css: { preprocessorOptions: { scss: { additionalData: '@use "src/styles/variables" as *;' } } } }`。
  46. 独立可执行文件配置:`{ entry: ['src/cli.ts'], exe: true }`。
  47. 跨平台可执行文件配置(需要 `@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' }] } }`。
  48. 高级 Hooks 配置:`hooks: { 'build:before': async (context) => { console.log('Building...') }, 'build:done': async (context) => { console.log('Build complete!') } }`。
  49. 多配置:从 `defineConfig` 导出数组,每个元素是一份构建配置。
  50. 条件配置:`defineConfig((options) => { const isDev = options.watch; return { entry: ['src/index.ts'], format: ['esm', 'cjs'], minify: !isDev, sourcemap: isDev } })`。
  51. Workspace/Monorepo:`{ workspace: 'packages/*', entry: ['src/index.ts'], format: ['esm', 'cjs'], dts: true }`。
  52. CLI 构建一次:`tsdown`。
  53. CLI 监听模式:`tsdown --watch`。
  54. CLI 自定义配置:`tsdown --config custom.ts`。
  55. CLI 从 tsup 迁移:`npx tsdown-migrate`。
  56. CLI 多格式输出:`tsdown --format esm,cjs`。
  57. CLI 自定义输出目录:`tsdown -d lib`(`--out-dir`)。
  58. CLI 启用压缩:`tsdown --minify`。
  59. CLI 生成声明:`tsdown --dts`。
  60. CLI 打包为独立可执行文件:`tsdown --exe`。
  61. CLI bundleless 模式:`tsdown --unbundle`。
  62. CLI 单入口:`tsdown src/index.ts`。
  63. CLI glob 入口:`tsdown src/*.ts`。
  64. CLI 多入口:`tsdown src/a.ts src/b.ts`。
  65. CLI workspace 模式:`tsdown -W`。
  66. CLI 过滤特定包:`tsdown -W -F my-package`。
  67. CLI 按正则过滤:`tsdown --filter /^pkg-/`。
  68. CLI 生成 source map:`tsdown --sourcemap`。
  69. CLI 清理输出目录:`tsdown --clean`。
  70. CLI 复用 Vite 配置:`tsdown --from-vite`。
  71. CLI 自定义 tsconfig:`tsdown --tsconfig tsconfig.build.json`。
  72. 最佳实践:始终为 TypeScript 库生成类型声明:`{ dts: true }`。
  73. 最佳实践:externalize dependencies 以避免打包不必要代码:`{ deps: { neverBundle: [/^react/, /^@myorg\//] } }`。
  74. 最佳实践:使用 tree shaking 优化 bundle 大小:`{ treeshake: true }`。
  75. 最佳实践:为生产构建启用压缩:`{ minify: true }`。
  76. 最佳实践:添加 shims 以提升 ESM/CJS 兼容性:`{ shims: true }`(添加 `__dirname`、`__filename` 等)。
  77. 最佳实践:自动生成 package.json exports:`{ exports: true }`。
  78. 最佳实践:开发时使用 watch mode:`tsdown --watch`。
  79. 最佳实践:对包含许多文件的工具库保留结构:`{ unbundle: true }`。
  80. 最佳实践:在 CI 发布前验证包:`{ publint: 'ci-only', attw: 'ci-only' }`。

适用边界 · 注意事项