From a551b216eae6566afb5775a0e04fb99310babdee Mon Sep 17 00:00:00 2001 From: Becem Date: Sat, 20 Jan 2024 21:43:11 +0100 Subject: [PATCH] fix(nuxt): use default export for raw components (#25282) --- packages/nuxt/src/components/loader.ts | 2 +- test/basic.test.ts | 32 ++++++++++++ .../basic/modules/import-components/index.ts | 49 +++++++++++++++++++ .../import-components/runtime/components.ts | 13 +++++ .../basic/pages/import-components/index.vue | 10 ++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/basic/modules/import-components/index.ts create mode 100644 test/fixtures/basic/modules/import-components/runtime/components.ts create mode 100644 test/fixtures/basic/pages/import-components/index.vue diff --git a/packages/nuxt/src/components/loader.ts b/packages/nuxt/src/components/loader.ts index 9bda024d72..1f372adfb3 100644 --- a/packages/nuxt/src/components/loader.ts +++ b/packages/nuxt/src/components/loader.ts @@ -76,7 +76,7 @@ export const loaderPlugin = createUnplugin((options: LoaderOptions) => { identifier += '_lazy' imports.add(`const ${identifier} = __defineAsyncComponent(${genDynamicImport(component.filePath, { interopDefault: false })}.then(c => c.${component.export ?? 'default'} || c)${isClientOnly ? '.then(c => createClientOnly(c))' : ''})`) } else { - imports.add(genImport(component.filePath, [{ name: component.export, as: identifier }])) + imports.add(genImport(component.filePath, [{ name: component._raw ? 'default' : component.export, as: identifier }])) if (isClientOnly) { imports.add(`const ${identifier}_wrapped = createClientOnly(${identifier})`) diff --git a/test/basic.test.ts b/test/basic.test.ts index 25ee24d137..5dc3dc556f 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -2307,6 +2307,38 @@ function normaliseIslandResult (result: NuxtIslandResponse) { } } +describe('import components', () => { + let html = '' + + it.sequential('fetch import-components page', async () => { + html = await $fetch('/import-components') + }) + + it('load default component with mode all', () => { + expect(html).toContain('default-comp-all') + }) + + it('load default component with mode client', () => { + expect(html).toContain('default-comp-client') + }) + + it('load default component with mode server', () => { + expect(html).toContain('default-comp-server') + }) + + it('load named component with mode all', () => { + expect(html).toContain('named-comp-all') + }) + + it('load named component with mode client', () => { + expect(html).toContain('named-comp-client') + }) + + it('load named component with mode server', () => { + expect(html).toContain('named-comp-server') + }) +}) + describe('lazy import components', () => { let html = '' diff --git a/test/fixtures/basic/modules/import-components/index.ts b/test/fixtures/basic/modules/import-components/index.ts new file mode 100644 index 0000000000..532afe20c4 --- /dev/null +++ b/test/fixtures/basic/modules/import-components/index.ts @@ -0,0 +1,49 @@ +import { addComponent, createResolver, defineNuxtModule } from 'nuxt/kit' + +export default defineNuxtModule({ + meta: { + name: 'import-components' + }, + setup () { + const { resolve } = createResolver(import.meta.url) + + addComponent({ + name: 'DCompClient', + filePath: resolve('./runtime/components'), + mode: 'client', + }) + + addComponent({ + name: 'DCompServer', + filePath: resolve('./runtime/components'), + mode: 'server', + }) + + addComponent({ + name: 'DCompAll', + filePath: resolve('./runtime/components'), + mode: 'all', + }) + + addComponent({ + name: 'NCompClient', + export: 'NComp', + filePath: resolve('./runtime/components'), + mode: 'client', + }) + + addComponent({ + name: 'NCompServer', + export: 'NComp', + filePath: resolve('./runtime/components'), + mode: 'server', + }) + + addComponent({ + name: 'NCompAll', + export: 'NComp', + filePath: resolve('./runtime/components'), + mode: 'all', + }) + }, +}) diff --git a/test/fixtures/basic/modules/import-components/runtime/components.ts b/test/fixtures/basic/modules/import-components/runtime/components.ts new file mode 100644 index 0000000000..96f456c19f --- /dev/null +++ b/test/fixtures/basic/modules/import-components/runtime/components.ts @@ -0,0 +1,13 @@ +import { defineComponent, h } from 'vue' + +export default defineComponent({ + name: 'DComp', + props: { message: String }, + render: (props: any) => h('h1', props.message), +}) + +export const NComp = defineComponent({ + name: 'NComp', + props: { message: String }, + render: (props: any) => h('h1', props.message), +}) diff --git a/test/fixtures/basic/pages/import-components/index.vue b/test/fixtures/basic/pages/import-components/index.vue new file mode 100644 index 0000000000..8f1b927d3d --- /dev/null +++ b/test/fixtures/basic/pages/import-components/index.vue @@ -0,0 +1,10 @@ +