Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>