-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mixing namespace import and named import client components (#64809)
### What Reported by @MaxLeiter, when you mixing named import and wildcard import to a client component, and if you clone the module it will missed others exports except the named ones. This lead to an issue that rendered React element type is `undefined`. ### Why We're using a tree-shaking strategy that collects the imported identifiers from client components on server components side. But in our code `connection.dependency.ids` can be undefined when you're using `import *`. So for that case we include all the exports. In the flight client entry plugin, if we found there's named imports that collected later, and the module is already being marked as namespace import `*`, we merge the ids into "*", so the whole module and all exports are respected. Now there're few possible cases for a client component import: During webpack build, in the outout going connections, there're connection with empty imported ids (`[]`), cannot unable to detect the imported ids (`['*']`) and detected named imported ids (`['a', 'b', ..]`). First two represnt to include the whole module and all exports, but we might collect the named imports could come later than the whole module. So if we found the existing collection already has `['*']` then we keep using that regardless the collected named imports. This can avoid the collected named imports cover "exports all" case, where we will expose less exports for that collection module lead to the undefined component error. Closes NEXT-3177
- Loading branch information
Showing
9 changed files
with
131 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...client-components-tree-shaking/app/client-import-namespace/client-module/client-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
|
||
export function ClientModExportA() { | ||
return 'client-mod:export-a' | ||
} | ||
|
||
export function ClientModExportB() { | ||
return 'client-mod:export-b' | ||
} | ||
|
||
export function ClientModExportC() { | ||
return 'client-mod:export-c' | ||
} |
7 changes: 7 additions & 0 deletions
7
...app-dir/client-components-tree-shaking/app/client-import-namespace/client-module/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use client' | ||
|
||
export { | ||
ClientModExportA, | ||
ClientModExportB, | ||
ClientModExportC, | ||
} from './client-module' |
9 changes: 9 additions & 0 deletions
9
...lient-components-tree-shaking/app/client-import-namespace/client-module2/client-module.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use client' | ||
|
||
export function ClientMod2ExportA() { | ||
return 'client-mod2:export-a' | ||
} | ||
|
||
export function ClientMod2ExportB() { | ||
return 'client-mod2:export-b' | ||
} |
3 changes: 3 additions & 0 deletions
3
...pp-dir/client-components-tree-shaking/app/client-import-namespace/client-module2/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use client' | ||
|
||
export { ClientMod2ExportA, ClientMod2ExportB } from './client-module' |
42 changes: 42 additions & 0 deletions
42
test/production/app-dir/client-components-tree-shaking/app/client-import-namespace/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import * as clientMod from './client-module' | ||
import { ClientModExportC } from './client-module' | ||
import * as clientMod2 from './client-module2' | ||
|
||
const mod1Map = { | ||
...clientMod, | ||
} | ||
|
||
const mod2Map = { | ||
...clientMod2, | ||
} | ||
|
||
const A = mod1Map.ClientModExportA | ||
const B = mod1Map.ClientModExportB | ||
const C = mod1Map.ClientModExportC | ||
const A2 = mod2Map.ClientMod2ExportA | ||
const B2 = mod2Map.ClientMod2ExportB | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<p id="a"> | ||
<A /> | ||
</p> | ||
<p id="b"> | ||
<B /> | ||
</p> | ||
<p id="c"> | ||
<C /> | ||
</p> | ||
<p id="named-c"> | ||
<ClientModExportC /> | ||
</p> | ||
<p id="a2"> | ||
<A2 /> | ||
</p> | ||
<p id="b2"> | ||
<B2 /> | ||
</p> | ||
</div> | ||
) | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
test/production/app-dir/client-components-tree-shaking/app/relative-dep/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters