fix(nuxt): assign default name to component without setup (#29869)

This commit is contained in:
Julien Huang 2024-11-12 17:39:29 +01:00 committed by Daniel Roe
parent 7973f5ed46
commit 13109cf821
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -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(),