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): island components with number prefix #24469

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/components/island-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createVNode, defineComponent, onErrorCaptured } from 'vue'
import { createError } from '../composables/error'

// @ts-expect-error virtual file
import * as islandComponents from '#build/components.islands.mjs'
import { islandComponents } from '#build/components.islands.mjs'

export default defineComponent({
props: {
Expand All @@ -14,7 +14,7 @@ export default defineComponent({
}
},
setup (props) {
const component = islandComponents[props.context.name] as ReturnType<typeof defineAsyncComponent>
const component = (islandComponents as ([string, ReturnType<typeof defineAsyncComponent>])[]).find(([c]) => c === props.context.name)

if (!component) {
throw createError({
Expand All @@ -27,6 +27,6 @@ export default defineComponent({
console.log(e)
})

return () => createVNode(component || 'span', { ...props.context.props, 'nuxt-ssr-component-uid': '' })
return () => createVNode(component[1] || 'span', { ...props.context.props, 'nuxt-ssr-component-uid': '' })
}
})
2 changes: 1 addition & 1 deletion packages/nuxt/src/components/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default defineNuxtModule<ComponentsOptions>({
if (nuxt.options.experimental.componentIslands) {
addTemplate({ ...componentsIslandsTemplate, filename: 'components.islands.mjs' })
} else {
addTemplate({ filename: 'components.islands.mjs', getContents: () => 'export default {}' })
addTemplate({ filename: 'components.islands.mjs', getContents: () => 'export const islandComponents = []' })
}

const unpluginServer = createTransformPlugin(nuxt, getComponents, 'server')
Expand Down
20 changes: 13 additions & 7 deletions packages/nuxt/src/components/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,19 @@ export const componentsIslandsTemplate: NuxtTemplate<ComponentsTemplateContext>
// .server components without a corresponding .client component will need to be rendered as an island
(component.mode === 'server' && !components.some(c => c.pascalName === component.pascalName && c.mode === 'client'))
)
return ['import { defineAsyncComponent } from \'vue\'', ...islands.map(
(c) => {
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
const comment = createImportMagicComments(c)
return `export const ${c.pascalName} = defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))`
}
)].join('\n')

return [
'import { defineAsyncComponent } from \'vue\'',
'export const islandComponents = [',
islands.map(
(c) => {
const exp = c.export === 'default' ? 'c.default || c' : `c['${c.export}']`
const comment = createImportMagicComments(c)
return ` ["${c.pascalName}", defineAsyncComponent(${genDynamicImport(c.filePath, { comment })}.then(c => ${exp}))]`
}
).join(',\n'),
']'
].join('\n')
}
}

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/basic/components/1thing.island.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- eslint-disable vue/multi-word-component-names -->

<template>
<div>
Meant to detect syntax error when `components.islands.mjs` is created
</div>
</template>