mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-15 02:14:44 +00:00
fix(nuxt): assign default name to component without setup (#29869)
This commit is contained in:
parent
e9ec575183
commit
311cfc7ab2
@ -1,6 +1,7 @@
|
|||||||
import { createUnplugin } from 'unplugin'
|
import { createUnplugin } from 'unplugin'
|
||||||
import MagicString from 'magic-string'
|
import MagicString from 'magic-string'
|
||||||
import type { Component } from 'nuxt/schema'
|
import type { Component } from 'nuxt/schema'
|
||||||
|
import type { Program } from 'acorn'
|
||||||
import { SX_RE, isVue } from '../../core/utils'
|
import { SX_RE, isVue } from '../../core/utils'
|
||||||
|
|
||||||
interface NameDevPluginOptions {
|
interface NameDevPluginOptions {
|
||||||
@ -34,6 +35,16 @@ export const ComponentNamePlugin = (options: NameDevPluginOptions) => createUnpl
|
|||||||
const s = new MagicString(code)
|
const s = new MagicString(code)
|
||||||
s.replace(NAME_RE, `__name: ${JSON.stringify(component.pascalName)}`)
|
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()) {
|
if (s.hasChanged()) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
|
Loading…
Reference in New Issue
Block a user