diff --git a/packages/nuxt/src/components/plugins/component-names.ts b/packages/nuxt/src/components/plugins/component-names.ts index af01adb5dc..47fec70203 100644 --- a/packages/nuxt/src/components/plugins/component-names.ts +++ b/packages/nuxt/src/components/plugins/component-names.ts @@ -1,6 +1,7 @@ import { createUnplugin } from 'unplugin' import MagicString from 'magic-string' import type { Component } from 'nuxt/schema' +import type { Program } from 'acorn' import { SX_RE, isVue } from '../../core/utils' interface NameDevPluginOptions { @@ -34,6 +35,16 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl const s = new MagicString(code) s.replace(NAME_RE, `__name: ${JSON.stringify(component.pascalName)}`) + // Without setup function, vue compiler does not generate __name + if (!s.hasChanged()) { + const ast = this.parse(code) as Program + const exportDefault = ast.body.find(node => node.type === 'ExportDefaultDeclaration') + if (exportDefault) { + const { start, end } = exportDefault.declaration + s.overwrite(start, end, `Object.assign(${code.slice(start, end)}, { __name: ${JSON.stringify(component.pascalName)} })`) + } + } + if (s.hasChanged()) { return { code: s.toString(),