From 311cfc7ab2950297cc541fdb56198932c2ec62d1 Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Tue, 12 Nov 2024 17:39:29 +0100 Subject: [PATCH] fix(nuxt): assign default name to component without setup (#29869) --- .../nuxt/src/components/plugins/component-names.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) 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(),