fix(nuxt): allow importing server components from #components (#23188)

This commit is contained in:
Daniel Roe 2023-09-13 23:35:53 +01:00 committed by GitHub
parent 95d1f9944d
commit a95fd28725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View File

@ -38,7 +38,7 @@ export default defineNuxtModule<ComponentsOptions>({
const getComponents: getComponentsT = (mode) => {
return (mode && mode !== 'all')
? context.components.filter(c => c.mode === mode || c.mode === 'all')
? context.components.filter(c => c.mode === mode || c.mode === 'all' || (c.mode === 'server' && !context.components.some(otherComponent => otherComponent.mode !== 'server' && otherComponent.pascalName === c.pascalName)))
: context.components
}

View File

@ -5,12 +5,14 @@ import { createUnimport } from 'unimport'
import { createUnplugin } from 'unplugin'
import { parseURL } from 'ufo'
import { parseQuery } from 'vue-router'
import { normalize } from 'pathe'
import { normalize, resolve } from 'pathe'
import { distDir } from '../dirs'
import type { getComponentsT } from './module'
const COMPONENT_QUERY_RE = /[?&]nuxt_component=/
export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT, mode: 'client' | 'server' | 'all') {
const serverComponentRuntime = resolve(distDir, 'components/runtime/server-component')
const componentUnimport = createUnimport({
imports: [
{
@ -25,18 +27,20 @@ export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT
const components = getComponents(mode)
return components.flatMap((c): Import[] => {
const withMode = (mode: string | undefined) => mode
? `${c.filePath}${c.filePath.includes('?') ? '&' : '?'}nuxt_component=${mode}`
? `${c.filePath}${c.filePath.includes('?') ? '&' : '?'}nuxt_component=${mode}&nuxt_component_name=${c.pascalName}`
: c.filePath
const mode = !c._raw && c.mode && ['client', 'server'].includes(c.mode) ? c.mode : undefined
return [
{
as: c.pascalName,
from: withMode(c.mode === 'client' ? 'client' : undefined),
from: withMode(mode),
name: 'default'
},
{
as: 'Lazy' + c.pascalName,
from: withMode([c.mode === 'client' ? 'client' : '', 'async'].filter(Boolean).join(',')),
from: withMode([mode, 'async'].filter(Boolean).join(',')),
name: 'default'
}
]
@ -82,6 +86,15 @@ export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT
].join('\n'),
map: null
}
} else if (mode === 'server' || mode === 'server,async') {
const name = query.nuxt_component_name
return {
code: [
`import { createServerComponent } from ${JSON.stringify(serverComponentRuntime)}`,
`export default createServerComponent(${JSON.stringify(name)})`
].join('\n'),
map: null
}
} else {
throw new Error(`Unknown component mode: ${mode}, this might be an internal bug of Nuxt.`)
}

View File

@ -0,0 +1,5 @@
<template>
<div>
Test globally-registered server component
</div>
</template>