fix(nuxt): deduplicate global components before registration (#20743)

This commit is contained in:
Daniel Roe 2023-05-10 13:57:27 +01:00 committed by GitHub
parent 8cca5cc9d8
commit 53fef72031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -36,13 +36,20 @@ export default defineNuxtPlugin({
export const componentsPluginTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = { export const componentsPluginTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = {
filename: 'components.plugin.mjs', filename: 'components.plugin.mjs',
getContents ({ app }) { getContents ({ app }) {
const globalComponents = app.components.filter(c => c.global) const globalComponents = new Set<string>()
if (!globalComponents.length) { return emptyComponentsPlugin } for (const component of app.components) {
if (component.global) {
globalComponents.add(component.pascalName)
}
}
if (!globalComponents.size) { return emptyComponentsPlugin }
const components = [...globalComponents]
return `import { defineNuxtPlugin } from '#app/nuxt' return `import { defineNuxtPlugin } from '#app/nuxt'
import { ${globalComponents.map(c => 'Lazy' + c.pascalName).join(', ')} } from '#components' import { ${components.map(c => 'Lazy' + c).join(', ')} } from '#components'
const lazyGlobalComponents = [ const lazyGlobalComponents = [
${globalComponents.map(c => `["${c.pascalName}", Lazy${c.pascalName}]`).join(',\n')} ${components.map(c => `["${c}", Lazy${c}]`).join(',\n')}
] ]
export default defineNuxtPlugin({ export default defineNuxtPlugin({

View File

@ -0,0 +1,5 @@
<template>
<div>
global component (client-only) registered automatically
</div>
</template>