Skip to content

Commit

Permalink
fix(nuxt): use default export for raw components (#25282)
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Jan 20, 2024
1 parent 9d1ca7c commit a551b21
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/nuxt/src/components/loader.ts
Expand Up @@ -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})`)
Expand Down
32 changes: 32 additions & 0 deletions test/basic.test.ts
Expand Up @@ -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 = ''

Expand Down
49 changes: 49 additions & 0 deletions 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',
})
},
})
@@ -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),
})
10 changes: 10 additions & 0 deletions test/fixtures/basic/pages/import-components/index.vue
@@ -0,0 +1,10 @@
<template>
<div>
<DCompAll message="default-comp-all" />
<DCompClient message="default-comp-client" />
<DCompServer message="default-comp-server" />
<NCompAll message="named-comp-all" />
<NCompClient message="named-comp-client" />
<NCompServer message="named-comp-server" />
</div>
</template>

0 comments on commit a551b21

Please sign in to comment.