Skip to content

Commit e5c3517

Browse files
authored
fix(rsc): warn dual module of optimized and non-optimized client reference (#705)
1 parent 5b73cbe commit e5c3517

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,34 @@ test.describe('dev-non-optimized-cjs', () => {
7676
})
7777
})
7878

79+
test.describe('dev-inconsistent-client-optimization', () => {
80+
test.beforeAll(async () => {
81+
// remove explicitly added optimizeDeps.exclude
82+
const editor = f.createEditor('vite.config.ts')
83+
editor.edit((s) =>
84+
s.replace(`'@vitejs/test-dep-client-in-server2/client',`, ``),
85+
)
86+
})
87+
88+
const f = useFixture({
89+
root: 'examples/basic',
90+
mode: 'dev',
91+
})
92+
93+
test('show warning', async ({ page }) => {
94+
await page.goto(f.url())
95+
expect(f.proc().stderr()).toContain(
96+
'client component dependency is inconsistently optimized.',
97+
)
98+
})
99+
})
100+
79101
function defineTest(f: Fixture) {
80102
test('basic', async ({ page }) => {
81103
using _ = expectNoPageError(page)
82104
await page.goto(f.url())
83105
await waitForHydration(page)
106+
expect(f.proc().stderr()).toBe('')
84107
})
85108

86109
test('client component', async ({ page }) => {

packages/plugin-rsc/e2e/fixture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function useFixture(options: {
106106
await proc.done
107107
assert(proc.proc.exitCode === 0)
108108
}
109-
const proc = runCli({
109+
proc = runCli({
110110
command: options.command ?? `pnpm preview`,
111111
label: `${options.root}:preview`,
112112
cwd,

packages/plugin-rsc/src/plugin.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,24 @@ function vitePluginUseClient(
951951
const browserEnvironmentName =
952952
useClientPluginOptions.environment?.browser ?? 'client'
953953

954+
// TODO: warning for late optimizer discovery
955+
function warnInoncistentClientOptimization(
956+
ctx: Rollup.TransformPluginContext,
957+
id: string,
958+
) {
959+
const { depsOptimizer } = server.environments.client
960+
if (depsOptimizer) {
961+
for (const dep of Object.values(depsOptimizer.metadata.optimized)) {
962+
if (dep.src === id) {
963+
ctx.warn(
964+
`client component dependency is inconsistently optimized. ` +
965+
`It's recommended to add the dependency to 'optimizeDeps.exclude'.`,
966+
)
967+
}
968+
}
969+
}
970+
}
971+
954972
return [
955973
{
956974
name: 'rsc:use-client',
@@ -986,7 +1004,9 @@ function vitePluginUseClient(
9861004
`[vite-rsc] detected an internal client boundary created by a package imported on rsc environment`,
9871005
)
9881006
}
989-
importId = `/@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/${encodeURIComponent(cleanUrl(id))}`
1007+
id = cleanUrl(id)
1008+
warnInoncistentClientOptimization(this, id)
1009+
importId = `/@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/${encodeURIComponent(id)}`
9901010
referenceKey = importId
9911011
} else if (packageSource) {
9921012
if (this.environment.mode === 'dev') {

0 commit comments

Comments
 (0)