mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
feat(nuxt): allow experimental global: 'sync'
components (#22558)
This commit is contained in:
parent
396e538aad
commit
b2cea4927e
@ -36,20 +36,25 @@ export default defineNuxtPlugin({
|
||||
export const componentsPluginTemplate: NuxtPluginTemplate<ComponentsTemplateContext> = {
|
||||
filename: 'components.plugin.mjs',
|
||||
getContents ({ app }) {
|
||||
const globalComponents = new Set<string>()
|
||||
const lazyGlobalComponents = new Set<string>()
|
||||
const syncGlobalComponents = new Set<string>()
|
||||
for (const component of app.components) {
|
||||
if (component.global) {
|
||||
globalComponents.add(component.pascalName)
|
||||
if (component.global === 'sync') {
|
||||
syncGlobalComponents.add(component.pascalName)
|
||||
} else if (component.global) {
|
||||
lazyGlobalComponents.add(component.pascalName)
|
||||
}
|
||||
}
|
||||
if (!globalComponents.size) { return emptyComponentsPlugin }
|
||||
if (!lazyGlobalComponents.size && !syncGlobalComponents.size) { return emptyComponentsPlugin }
|
||||
|
||||
const components = [...globalComponents]
|
||||
const lazyComponents = [...lazyGlobalComponents]
|
||||
const syncComponents = [...syncGlobalComponents]
|
||||
|
||||
return `import { defineNuxtPlugin } from '#app/nuxt'
|
||||
import { ${components.map(c => 'Lazy' + c).join(', ')} } from '#components'
|
||||
import { ${[...lazyComponents.map(c => 'Lazy' + c), ...syncComponents].join(', ')} } from '#components'
|
||||
const lazyGlobalComponents = [
|
||||
${components.map(c => `["${c}", Lazy${c}]`).join(',\n')}
|
||||
${lazyComponents.map(c => `["${c}", Lazy${c}]`).join(',\n')},
|
||||
${syncComponents.map(c => `["${c}", ${c}]`).join(',\n')}
|
||||
]
|
||||
|
||||
export default defineNuxtPlugin({
|
||||
|
@ -7,7 +7,7 @@ export interface Component {
|
||||
chunkName: string
|
||||
prefetch: boolean
|
||||
preload: boolean
|
||||
global?: boolean
|
||||
global?: boolean | 'sync'
|
||||
island?: boolean
|
||||
mode?: 'client' | 'server' | 'all'
|
||||
/**
|
||||
|
@ -94,6 +94,7 @@ describe('pages', () => {
|
||||
// should register global components automatically
|
||||
expect(html).toContain('global component registered automatically')
|
||||
expect(html).toContain('global component via suffix')
|
||||
expect(html).toContain('This is a synchronously registered global component')
|
||||
|
||||
await expectNoClientErrors('/')
|
||||
})
|
||||
|
5
test/fixtures/basic/components/GlobalSync.vue
vendored
Normal file
5
test/fixtures/basic/components/GlobalSync.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
<template>
|
||||
<div>
|
||||
This is a synchronously registered global component
|
||||
</div>
|
||||
</template>
|
7
test/fixtures/basic/nuxt.config.ts
vendored
7
test/fixtures/basic/nuxt.config.ts
vendored
@ -146,6 +146,13 @@ export default defineNuxtConfig({
|
||||
filePath: '~/other-components-folder/named-export'
|
||||
})
|
||||
},
|
||||
'components:extend' (components) {
|
||||
for (const comp of components) {
|
||||
if (comp.pascalName === 'GlobalSync') {
|
||||
comp.global = 'sync'
|
||||
}
|
||||
}
|
||||
},
|
||||
'vite:extendConfig' (config) {
|
||||
config.plugins!.push({
|
||||
name: 'nuxt:server',
|
||||
|
1
test/fixtures/basic/pages/index.vue
vendored
1
test/fixtures/basic/pages/index.vue
vendored
@ -44,6 +44,7 @@
|
||||
</NuxtLink>
|
||||
<NestedSugarCounter :multiplier="2" />
|
||||
<CustomComponent />
|
||||
<component :is="`global${'-'.toString()}sync`" />
|
||||
<Spin>Test</Spin>
|
||||
<component :is="`test${'-'.toString()}global`" />
|
||||
<component :is="`with${'-'.toString()}suffix`" />
|
||||
|
Loading…
Reference in New Issue
Block a user