Skip to content

Commit c1e53b8

Browse files
committed
fix(rsc): tree-shake unused client reference module and fix build error
1 parent 2dd0993 commit c1e53b8

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

packages/plugin-rsc/examples/starter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@types/react-dom": "^19.1.9",
1919
"@vitejs/plugin-react": "latest",
2020
"@vitejs/plugin-rsc": "latest",
21+
"react-tweet": "^3.2.2",
2122
"rsc-html-stream": "^0.0.7",
2223
"vite": "^7.1.4"
2324
}

packages/plugin-rsc/examples/starter/src/root.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import { getServerCounter, updateServerCounter } from './action.tsx'
44
import reactLogo from './assets/react.svg'
55
import { ClientCounter } from './client.tsx'
66

7+
// repro
8+
import { Tweet } from 'react-tweet'
9+
console.log(Tweet)
10+
// import { useTweet } from 'react-tweet';
11+
// console.log(useTweet)
12+
// repro
13+
714
export function Root(props: { url: URL }) {
815
return (
916
<html lang="en">

packages/plugin-rsc/src/plugin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,12 +1226,14 @@ function vitePluginUseClient(
12261226
// group client reference modules by `clientChunks` option
12271227
manager.clientReferenceGroups = {}
12281228
for (const meta of Object.values(manager.clientReferenceMetaMap)) {
1229+
// no server chunk associated when the module is tree-shaken
1230+
if (!meta.serverChunk) continue
12291231
let name =
12301232
useClientPluginOptions.clientChunks?.({
12311233
id: meta.importId,
12321234
normalizedId: manager.toRelativeId(meta.importId),
1233-
serverChunk: meta.serverChunk!,
1234-
}) ?? meta.serverChunk!
1235+
serverChunk: meta.serverChunk,
1236+
}) ?? meta.serverChunk
12351237
// ensure clean virtual id to avoid interfering with other plugins
12361238
name = cleanUrl(name.replaceAll('..', '__'))
12371239
const group = (manager.clientReferenceGroups[name] ??= [])
@@ -1333,6 +1335,7 @@ function vitePluginUseClient(
13331335
}
13341336
},
13351337
generateBundle(_options, bundle) {
1338+
if (manager.isScanBuild) return
13361339
if (this.environment.name !== serverEnvironmentName) return
13371340

13381341
// analyze rsc build to inform later client reference building.

pnpm-lock.yaml

Lines changed: 48 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)