tsdown 插件体系实战指南:融合 Rolldown、Unplugin、Rollup 与 Vite 四大生态并编写自定义插件
tsdown 插件体系实战指南:融合 Rolldown、Unplugin、Rollup 与 Vite 四大生态并编写自定义插件
tsdown 是基于 Rolldown 构建的 TypeScript/JavaScript 库打包器,其插件系统天然继承了多生态兼容能力:既支持 Rolldown 原生插件,也能接入 Unplugin 通用插件、绝大多数 Rollup 插件与部分 Vite 插件。本文以 .agents/skills/tsdown/references/advanced-plugins.md 文档为骨架,结合 moeru-ai/airi 仓库中 35 余份真实 tsdown.config.ts 配置,系统讲解插件选型、接入方式、框架插件组合、自定义插件开发与故障排查,读完即可为你的库工程搭建一套可靠、可复用的插件化构建配置。
概述:为什么 tsdown 能兼容多生态插件
tsdown 以 Rolldown(Rust 编写的 Rollup 兼容打包内核)为底层引擎,而 Rolldown 的插件 API 与 Rollup 高度一致,这为 tsdown 带来天然的多生态插件兼容性。你可以在一个配置文件中混用多种来源的插件,按需扩展打包流程:
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [/* 来自任意生态的插件 */],
})
需要说明的适用前提:插件兼容性是分层级的,并非所有生态插件都能开箱即用。Rolldown 原生插件获得完全支持,Unplugin 系列插件大多数可直接工作,Rollup 插件兼容度很高但可能伴随类型报错,Vite 插件则仅当不依赖 Vite 专属 API 时才能工作。下面逐一展开。
支持的插件生态与兼容性对照
| 生态 | 兼容性 | 说明 |
|---|---|---|
| Rolldown 插件 | ✅ 完全支持 | 专为 Rolldown 设计,直接传入 plugins 即可 |
| Unplugin | ✅ 大多数可用 | 跨打包器通用,需调用 .rolldown() 适配器 |
| Rollup 插件 | ✅ 高兼容 | 可能触发 TS 类型报错,需类型断言 |
| Vite 插件 | ⚠️ 有限支持 | 仅在不使用 Vite 专属 API 时可用,未来版本将改进 |
Rolldown 插件:完全支持
Rolldown 原生插件与 tsdown 同源,无需任何适配:
import RolldownPlugin from 'rolldown-plugin-something'
export default defineConfig({
plugins: [RolldownPlugin()],
})
Unplugin:跨打包器的通用插件
Unplugin 是专门为解决"一套插件代码跑通多个打包器"而设计的规范,插件作者会同时导出 vite、rollup、webpack、rolldown 等多个入口。在 tsdown 中应使用 .rolldown() 入口:
import UnpluginPlugin from 'unplugin-something'
export default defineConfig({
plugins: [UnpluginPlugin.rolldown()],
})
常见可用的 Unplugin 插件包括:unplugin-vue-components(Vue 组件自动注册)、unplugin-auto-import(API 自动导入)、unplugin-icons(图标按需加载)等。airi 仓库的 packages/i18n/tsdown.config.ts 就是一个真实案例——它通过 unplugin-yaml/rolldown 在打包阶段把 .yaml 语言包直接转换为可导入的模块:
import Yaml from 'unplugin-yaml/rolldown'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: {
'index': 'src/index.ts',
'locales/index': 'src/locales/index.ts',
'locales/en/index': 'src/locales/en/index.ts',
'locales/zh-Hans/index': 'src/locales/zh-Hans/index.ts',
},
copy: [
{ from: 'src/locales', to: 'dist/locales' },
],
unbundle: true,
plugins: [
Yaml(),
],
})
该配置同时配合 unbundle: true(保留目录结构)与 copy(同步复制语言文件),构建产物结构与该包 package.json 中声明的 ./locales/* 多个 exports 子路径一一对应,展示了插件与构建选项协同的完整链路。
Rollup 插件:高兼容 + 类型断言
绝大多数 Rollup 插件可直接在 tsdown 中运行,但官方 Rollup 插件(@rollup/*)与 Rolldown 插件的 TypeScript 类型定义并不完全一致,直接写入配置可能报类型错误。文档给出的两种处理方式:
import RollupPlugin from 'rollup-plugin-something'
export default defineConfig({
plugins: [
// 方式一:用 @ts-expect-error 标注已知的类型差异
// @ts-expect-error Rollup plugin type mismatch
RollupPlugin(),
// 方式二:直接断言为 any
RollupPlugin() as any,
],
})
Vite 插件:有限支持
部分 Vite 插件(如 @vitejs/plugin-react、@vitejs/plugin-vue)也能在 tsdown 中工作,但仅限不依赖 Vite 专属 API(defineConfig 环境、server.*、optimizeDeps 等)的插件:
import VitePlugin from 'vite-plugin-something'
export default defineConfig({
plugins: [
// @ts-expect-error Vite plugin type mismatch
VitePlugin(),
],
})
按文档说明,这是有限支持——Vite 插件支持度正在持续改进,若插件依赖 Vite 特有能力,优先寻找 Rollup 等价实现或 Unplugin 版本(详见下文"Vite 插件失败"排查小节)。airi 仓库中 packages/vite-plugin-sherpaw/src/index.ts 的 Sherpaw() 插件则展示了反面场景:它依赖 configResolved、apply、enforce: 'pre' 等 Vite 专属机制管理 Sherpaw 语音模型的下载与打包,这类插件就应保留在 Vite 侧而非 tsdown 侧使用。
插件接入实战:从单个到多个再到条件化
基础用法
import { defineConfig } from 'tsdown'
import SomePlugin from 'some-plugin'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [SomePlugin()],
})
多个插件
插件按数组顺序依次执行(后文"插件顺序"有更详细的讨论),每个插件都可传入自己的配置对象:
import PluginA from 'plugin-a'
import PluginB from 'plugin-b'
import PluginC from 'plugin-c'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [
PluginA(),
PluginB({ option: true }),
PluginC(),
],
})
条件插件
tsdown 的 defineConfig 支持函数形式配置,配合 options.watch 可以在开发(watch)与生产构建间切换插件,.filter(Boolean) 用于剔除 false 项:
export default defineConfig((options) => ({
entry: ['src/index.ts'],
plugins: [
SomePlugin(),
options.watch && DevPlugin(),
!options.watch && ProdPlugin(),
].filter(Boolean),
}))
这种模式与 advanced-hooks.md 中"推荐优先用插件做扩展、hooks 仅用于简单任务"的建议一致,条件化插件是区分开发/生产行为的推荐做法。
常见插件模式:六个高频场景
以下六个模式对应最常用的 Rollup 官方插件与 Unplugin 插件组合,均直接取自原文档,可复制进你的 tsdown.config.ts 按需使用。
1. JSON 导入
import json from '@rollup/plugin-json'
export default defineConfig({
plugins: [json()],
})
2. Node 模块解析
import { nodeResolve } from '@rollup/plugin-node-resolve'
export default defineConfig({
plugins: [nodeResolve()],
})
3. CommonJS 转换
import commonjs from '@rollup/plugin-commonjs'
export default defineConfig({
plugins: [commonjs()],
})
4. 变量替换(Replace)
常用于注入环境变量与版本号,替换值需用 JSON.stringify 包裹以生成合法的 JS 字面量:
import replace from '@rollup/plugin-replace'
export default defineConfig({
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
__VERSION__: JSON.stringify('1.0.0'),
}),
],
})
5. API 自动导入(Auto Import)
unplugin-auto-import 的 rolldown 入口可自动为 Vue 等框架注入 API 并生成类型声明文件:
import AutoImport from 'unplugin-auto-import/rolldown'
export default defineConfig({
plugins: [
AutoImport({
imports: ['vue', 'vue-router'],
dts: 'src/auto-imports.d.ts',
}),
],
})
6. Vue 组件自动注册
unplugin-vue-components 可让 .vue 组件在模板中免 import 直接使用,并自动生成组件类型声明:
import Components from 'unplugin-vue-components/rolldown'
export default defineConfig({
plugins: [
Components({
dts: 'src/components.d.ts',
}),
],
})
框架特定插件:React、Vue、Solid 与 Svelte
构建组件库时,通常需要框架插件将 JSX/SFC 编译进打包流程。下面四个配置均来自原文档,其中 Vue 场景在 recipe-vue.md 中有更完整的配方(含 unplugin-vue + dts: { vue: true } 类型生成),React 场景可参考 recipe-react.md(Rolldown 对 JSX/TSX 原生支持,基础组件无需额外插件)。
React
import react from '@vitejs/plugin-react'
export default defineConfig({
entry: ['src/index.tsx'],
plugins: [
// @ts-expect-error Vite plugin
react(),
],
})
Vue
import vue from '@vitejs/plugin-vue'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [
// @ts-expect-error Vite plugin
vue(),
],
})
Solid
import solid from 'vite-plugin-solid'
export default defineConfig({
entry: ['src/index.tsx'],
plugins: [
// @ts-expect-error Vite plugin
solid(),
],
})
Svelte
import { svelte } from '@sveltejs/vite-plugin-svelte'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [
// @ts-expect-error Vite plugin
svelte(),
],
})
注意这四个例子全部属于"Vite 插件有限支持"范畴,注释标注的 @ts-expect-error 是预期内的类型差异;Svelte 场景在 recipe-svelte.md 中还建议优先考虑源码分发。airi 仓库中的 Vue 组件库包(如 packages/stage-ui、packages/stage-pages)在 Vite 侧使用 unplugin-vue-components、unplugin-auto-import 等组合,是上述模式在真实工程中的对应体现。
编写自定义插件:跟随 Rolldown 插件规范
当现成插件无法满足需求时,可以编写自己的 Rolldown 插件。基础结构只需一个带名字的对象和若干生命周期钩子:
import type { Plugin } from 'rolldown'
function myPlugin(): Plugin {
return {
name: 'my-plugin',
// Transform hook:处理特定后缀文件的源码转换
transform(code, id) {
if (id.endsWith('.custom')) {
return {
code: transformCode(code),
map: null,
}
}
},
// 其他 hooks...
}
}
在配置中直接引用:
import { myPlugin } from './my-plugin'
export default defineConfig({
plugins: [myPlugin()],
})
从源码结构看,Rolldown 插件 API 与 Rollup 的 Plugin 接口对齐(name、transform、resolveId、load、buildStart 等),因此熟悉 Rollup 插件开发的开发者可以平滑迁移;Rolldown 还提供了比 Rollup 更丰富的钩子能力,具体可参考 advanced-rolldown-options.md 中通过 inputOptions / outputOptions 直接向底层引擎传参的细粒度控制方式。
插件配置与执行顺序
插件专属选项
每个插件的具体配置项应查阅该插件自身文档——tsdown 不修改插件选项语义,plugins: [PluginB({ option: true })] 中的对象原样传给插件构造函数。
插件顺序:数组即执行顺序
插件按 plugins 数组中定义的先后顺序执行:
export default defineConfig({
plugins: [
PluginA(), // 第一个执行
PluginB(), // 第二个执行
PluginC(), // 最后执行
],
})
顺序会影响结果:例如 replace 应早于消费被替换变量的插件执行,vue 插件应在 dts 类型生成前完成 SFC 编译。若插件依赖特定执行顺序,调整数组顺序即可。
故障排查
Rollup/Vite 插件类型报错
这是最常见的现象,根源是生态间插件类型定义不完全一致。两种标准解法:
plugins: [
// 方案一:@ts-expect-error 显式声明"此处类型不匹配是预期行为"
// @ts-expect-error Plugin type mismatch
SomePlugin(),
// 方案二:断言为 any
SomePlugin() as any,
]
插件不工作
按以下顺序排查(原文档给出的四个步骤):
- 检查兼容性 —— 确认插件支持你所用的打包器(Rolldown/Rollup);
- 阅读文档 —— 遵循插件的官方接入指引,确认是否需要
.rolldown()等适配入口; - 检查插件顺序 —— 某些插件依赖执行顺序(如需要在其他插件之前/之后运行);
- 开启调试模式 —— 使用
--debug标志查看详细日志。根据 reference-cli.md,还可细化为tsdown --debug rolldown仅调试 Rolldown 相关特性。
Vite 插件失败
Vite 插件常依赖 Vite 专属 API(server.*、optimizeDeps、configResolved 环境等),在 tsdown 中不可用时的三条出路:
- 寻找 Rollup 等价版本 —— 多数知名 Vite 插件都有 Rollup/Rolldown 实现;
- 改用 Unplugin 版本 —— 检查是否有
unplugin-*替代品,用.rolldown()入口接入; - 等待官方支持 —— Vite 插件兼容性正在逐步改进中。
仓库实践总结:airi 中的 tsdown 插件运用
moeru-ai/airi 仓库本身是 tsdown 的规模化使用者,根目录 pnpm-workspace.yaml 将 tsdown 固定为 ^0.22.14(Node.js 22.18.0+ 即可运行,产物可通过 target 降低运行版本要求,详见 SKILL.md),packages/ 与 integrations/ 下分布着 35 份以上 tsdown.config.ts。除前文详述的 i18n 包外,可对照观察:
- packages/audio/tsdown.config.ts:多入口 +
unbundle: true,并用external显式外部化./capture.worklet?worker&url这类特殊模块; - packages/server-runtime/tsdown.config.ts:Node 服务入口(
server、bin/run)+target: 'node18'+dts: true,体现"构建端 Node 22+、产物端 node18"的降级策略; - packages/motion-driver-magic/tsdown.config.ts 与 packages/model-driver-magic-live2d/tsdown.config.ts:浏览器端库的标准配置(
platform: 'browser'+treeshake: true+dts: true); - packages/vite-plugin-sherpaw/tsdown.config.ts:为 Vite 插件类包设置
deps: { neverBundle: ['vite'] },把vite保留为外部依赖、避免将宿主插件框架打进产物。
这些真实配置印证了原文档的核心建议:优先 Rolldown 原生与 Unplugin 插件(如 i18n 的 unplugin-yaml),对 Rollup/Vite 插件做好类型断言,按需编写自定义插件,并在配置文件中保持明确的插件顺序。
Tips:插件使用要诀
- 优先选择 Rolldown 原生插件,兼容性最佳、无类型负担;
- 跨打包器需求用 Unplugin,
.rolldown()入口即可通吃; - Rollup/Vite 插件记得做类型断言(
@ts-expect-error或as any); - 跨生态插件务必充分测试,覆盖 ESM/CJS 等目标格式;
- 每个插件查阅其专属文档,确认入口与配置项;
- 特殊需求就写自定义插件,遵循 Rolldown 插件 API 即可融入生态。
如需进一步了解插件之外的自定义手段,可继续阅读本技能库中的 advanced-hooks.md(生命周期钩子)、advanced-rolldown-options.md(底层选项透传)、recipe-react.md 与 recipe-vue.md(框架级完整配方)。