fix(nuxt): properly handle query for component wrapper (#20591)

This commit is contained in:
Anthony Fu 2023-04-30 11:08:08 +02:00 committed by GitHub
parent a13b18eddf
commit d2fc6ae614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -21,15 +21,19 @@ export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT
function getComponentsImports (): Import[] { function getComponentsImports (): Import[] {
const components = getComponents(mode) const components = getComponents(mode)
return components.flatMap((c): Import[] => { return components.flatMap((c): Import[] => {
const withMode = (mode: string | undefined) => mode
? `${c.filePath}${c.filePath.includes('?') ? '&' : '?'}nuxt_component=${mode}`
: c.filePath
return [ return [
{ {
as: c.pascalName, as: c.pascalName,
from: c.filePath + (c.mode === 'client' ? '?component=client' : ''), from: withMode(c.mode === 'client' ? 'client' : undefined),
name: 'default' name: 'default'
}, },
{ {
as: 'Lazy' + c.pascalName, as: 'Lazy' + c.pascalName,
from: c.filePath + '?component=' + [c.mode === 'client' ? 'client' : '', 'async'].filter(Boolean).join(','), from: withMode([c.mode === 'client' ? 'client' : '', 'async'].filter(Boolean).join(',')),
name: 'default' name: 'default'
} }
] ]
@ -43,10 +47,10 @@ export function createTransformPlugin (nuxt: Nuxt, getComponents: getComponentsT
}, },
async transform (code, id) { async transform (code, id) {
// Virtual component wrapper // Virtual component wrapper
if (id.includes('?component')) { if (id.match(/[?&]nuxt_component=/)) {
const { search } = parseURL(id) const { search } = parseURL(id)
const query = parseQuery(search) const query = parseQuery(search)
const mode = query.component const mode = query.nuxt_component
const bare = id.replace(/\?.*/, '') const bare = id.replace(/\?.*/, '')
if (mode === 'async') { if (mode === 'async') {
return [ return [