componentRegistry(options?)
扫描 .vue、.tsx、.jsx 组件,生成命名导出、实例类型、全局注册方法与 Vue 全局组件声明。
ts
componentRegistry({
dirs: ["src/components", "src/features"],
output: "src/components/index.ts",
dts: "types/components.d.ts",
name: ({ defaultName, relativePath }) => (relativePath.startsWith("admin/") ? `Admin${defaultName}` : defaultName),
});| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
dirs | string | readonly string[] | "src/components" | 扫描目录 |
output | string | false | "src/components/index.ts" | 组件入口;false 关闭 |
dts | string | false | "types/components.d.ts" | 全局类型;false 关闭 |
deep | boolean | true | 是否递归扫描 |
extensions | readonly string[] | vue, tsx, jsx | 可带或不带点号的扩展名 |
include | (context) => boolean | - | 返回 false 排除文件 |
name | (context) => string | - | 自定义导出标识符 |
conflict | "error" | "warn" | "overwrite" | "error" | 重名策略 |
debounce | number | 80 | 开发监听防抖毫秒数 |
生成模块默认为每个扫描到的组件导出 组件名Instance = InstanceType<typeof 组件名> 实例类型。
registerComponents(app) 直接调用 app.component(Component.name, Component),不会生成回退注册名称,也不会在注册方法中检查或警告。插件能够静态确认组件未声明运行时 name 时会在生成阶段警告,但仍保留组件并继续生成;无法可靠判断时默认按已声明处理。
index.vue 默认使用父目录名称。生成名称必须是唯一且合法的 ECMAScript 标识符。output 和 dts 不能同时关闭。
componentRegistry 组件注册
扫描 Vue/TSX/JSX 组件,并生成命名导出、实例类型、全局注册方法和全局组件类型。
签名
ts
export function componentRegistry(options: ComponentRegistryPluginOptions = {}): Plugin;示例
ts
import { componentRegistry } from "fast-vite-plugins";
const plugin = componentRegistry({ dirs: ["src/components"], output: "src/components/index.ts" });
export default { plugins: [plugin] };输入
| 输入值 | 输入值类型 | 必填/默认值 | 输入值说明 |
|---|---|---|---|
options | ComponentRegistryPluginOptions | 否,默认 {} | 扫描目录、输出文件、过滤、命名和冲突策略。 |
返回
| 返回值 | 返回值类型 | 返回值说明 |
|---|---|---|
plugin | Plugin | 可直接加入 Vite plugins 的组件入口生成插件。 |
