Skip to content

Commit

Permalink
Fix conflict when re-exporting multiple Client References (#49468)
Browse files Browse the repository at this point in the history
Closes #49324. Default export should only be added in the loader
generated boundary file when it actually exists.

fix NEXT-1112
  • Loading branch information
shuding committed May 16, 2023
1 parent 2dade5d commit 05a9e0a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/next-swc/crates/core/src/react_server_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ impl<C: Comments> ReactServerComponents<C> {
})) => {
self.export_names.push("default".to_string());
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(ExportDefaultExpr {
expr: _,
..
})) => {
self.export_names.push("default".to_string());
}
ModuleItem::ModuleDecl(ModuleDecl::ExportAll(_)) => {
self.export_names.push("*".to_string());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ const proxy = createProxy(String.raw\`${this.resourcePath}\`)
// The __esModule getter forces the proxy target to create the default export
// and the $$typeof value is for rendering logic to determine if the module
// is a client boundary.
export const { __esModule, $$typeof } = proxy;
export default proxy.default;
const { __esModule, $$typeof } = proxy;
const __default__ = proxy.default;
`
let cnt = 0
for (const ref of clientRefs) {
if (ref !== '' && ref !== 'default') {
if (ref === '') {
esmSource += `\nexports[''] = proxy[''];`
} else if (ref === 'default') {
esmSource += `
export { __esModule, $$typeof };
export default __default__;`
} else {
esmSource += `
const e${cnt} = proxy["${ref}"];
export { e${cnt++} as ${ref} };`
} else if (ref === '') {
esmSource += `\nexports[''] = proxy[''];`
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-external/app-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ createNextDescribe(
expect(html).toContain('hello')
})

it('should support exporting multiple star re-exports', async () => {
const html = await next.render('/wildcard')
expect(html).toContain('Foo')
})

if ((global as any).isNextDev) {
it('should error for wildcard exports of client module references in esm', async () => {
const page = 'app/esm-client-ref/page.js'
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-external/app/wildcard/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Foo } from 'client-esm-module-wildcard-2'

export default function Page() {
return <Foo />
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use client'

export function Bar() {
return 'Bar'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use client'

export function Foo() {
return 'Foo'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './foo'
export * from './bar'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "client-esm-module-wildcard-2",
"type": "module",
"exports": {
"default": "./index.js"
}
}

0 comments on commit 05a9e0a

Please sign in to comment.