Skip to content

Commit

Permalink
fix(nuxt): deduplicate global components before registration (#20743)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed May 10, 2023
1 parent 8cca5cc commit 53fef72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/nuxt/src/components/templates.ts
Expand Up @@ -36,13 +36,20 @@ export default defineNuxtPlugin({
export const componentsPluginTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = {
filename: 'components.plugin.mjs',
getContents ({ app }) {
const globalComponents = app.components.filter(c => c.global)
if (!globalComponents.length) { return emptyComponentsPlugin }
const globalComponents = new Set<string>()
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'
import { ${globalComponents.map(c => 'Lazy' + c.pascalName).join(', ')} } from '#components'
import { ${components.map(c => 'Lazy' + c).join(', ')} } from '#components'
const lazyGlobalComponents = [
${globalComponents.map(c => `["${c.pascalName}", Lazy${c.pascalName}]`).join(',\n')}
${components.map(c => `["${c}", Lazy${c}]`).join(',\n')}
]
export default defineNuxtPlugin({
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic/components/global/ClientGlobal.client.vue
@@ -0,0 +1,5 @@
<template>
<div>
global component (client-only) registered automatically
</div>
</template>

0 comments on commit 53fef72

Please sign in to comment.