中文 | English
A Vite plugin that automatically injects/fills defineOptions({ name }) in Vue SFC <script setup>, generating component names from the file path so components are easier to identify in Vue DevTools and debugging logs.
In Vue 3.2.34 or later, SFCs using <script setup> can automatically derive the component name from the filename. However, for paths like button/index.vue, the generated component name becomes Index (or index).
This means that in scenarios that rely on component names—such as recursive components and <KeepAlive> include / exclude (in addition to Vue DevTools)—you may still need to manually set the name to Button. This plugin recognizes both button/index.vue and button.vue and sets a reasonable component name automatically.
- Only processes
.vuefiles (configurable viainclude/exclude) - Only works when
<script setup>exists - After generating a component name, it updates
<script setup>as follows:- If
defineOptions()exists with no arguments: fills it todefineOptions({ name: "Xxx" }) - If
defineOptions({ ... })exists but has noname: injectsname: "Xxx",into the object - If
defineOptions(...)does not exist: insertsdefineOptions({ name: "Xxx" });at the beginning of the<script setup>content
- If
- If
nameis already present, it will not override it
Note:
defineOptionsrequires Vue 3.3+ (the<script setup>macro).
pnpm add -D vite-plugin-vue-define-options-nameUse it in vite.config.ts (recommended to place it before @vitejs/plugin-vue):
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDefineOptionsName from "vite-plugin-vue-define-options-name";
export default defineConfig({
plugins: [
vueDefineOptionsName(),
vue(),
],
});export type AutoComponentNameOptions = {
include?: (string | RegExp)[];
exclude?: (string | RegExp)[];
nameCase?: "pascal" | "camel" | "kebab";
};- include: file match rules to include (default
[/\.vue$/i]) - exclude: file match rules to exclude (default
[/\/node_modules\//i]) - nameCase: output naming style (default
"pascal")"pascal":MyComponent"camel":myComponent"kebab":my-component
Given a file path .../FooBar.vue:
- Uses the filename (without
.vue) as the base name:FooBar - Transforms it using
nameCase, and uses it as the componentname
Special case for index.vue:
.../profile/index.vue→ base name uses the directory nameprofile→ e.g.Profilein pascal case