Skip to content

Commit 377a273

Browse files
authored
feat(rsc): enable server-chunk-based client chunks (#794)
1 parent 95e4091 commit 377a273

File tree

14 files changed

+106
-23
lines changed

14 files changed

+106
-23
lines changed

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,48 @@ test.describe('build-default', () => {
4848
const f = useFixture({ root: 'examples/basic', mode: 'build' })
4949
defineTest(f)
5050

51-
test('custom client chunk', async () => {
51+
test('server-chunk-based client chunks', async () => {
5252
const { chunks }: { chunks: Rollup.OutputChunk[] } = JSON.parse(
5353
f.createEditor('dist/client/.vite/test.json').read(),
5454
)
55-
const chunk = chunks.find((c) => c.name === 'custom-chunk')
56-
const expected = [1, 2, 3].map((i) =>
57-
normalizePath(path.join(f.root, `src/routes/chunk/client${i}.tsx`)),
58-
)
59-
expect(chunk?.moduleIds).toEqual(expect.arrayContaining(expected))
55+
const expectedGroups = {
56+
'facade:src/routes/chunk2/client1.tsx': ['src/routes/chunk2/client1.tsx'],
57+
'facade:src/routes/chunk2/server2.tsx': [
58+
'src/routes/chunk2/client2.tsx',
59+
'src/routes/chunk2/client2b.tsx',
60+
],
61+
'shared:src/routes/chunk2/client3.tsx': ['src/routes/chunk2/client3.tsx'],
62+
}
63+
const actualGroups: Record<string, string[]> = {}
64+
for (const key in expectedGroups) {
65+
const groupId = `\0virtual:vite-rsc/client-references/group/${key}`
66+
const groupChunk = chunks.find((c) => c.facadeModuleId === groupId)
67+
if (groupChunk) {
68+
actualGroups[key] = groupChunk.moduleIds
69+
.filter((id) => id !== groupId)
70+
.map((id) => normalizePath(path.relative(f.root, id)))
71+
}
72+
}
73+
expect(actualGroups).toEqual(expectedGroups)
6074
})
6175
})
6276

63-
test.describe('build-server-client-chunks', () => {
77+
test.describe('custom-client-chunks', () => {
6478
const f = useFixture({
6579
root: 'examples/basic',
6680
mode: 'build',
6781
cliOptions: {
6882
env: {
69-
TEST_SERVER_CLIENT_CHUNKS: 'true',
83+
TEST_CUSTOM_CLIENT_CHUNKS: 'true',
7084
},
7185
},
7286
})
7387

74-
defineTest(f)
75-
76-
test('custom client chunk', async () => {
88+
test('basic', async () => {
7789
const { chunks }: { chunks: Rollup.OutputChunk[] } = JSON.parse(
7890
f.createEditor('dist/client/.vite/test.json').read(),
7991
)
80-
const chunk = chunks.find((c) => c.name === 'root')
92+
const chunk = chunks.find((c) => c.name === 'custom-chunk')
8193
const expected = [1, 2, 3].map((i) =>
8294
normalizePath(path.join(f.root, `src/routes/chunk/client${i}.tsx`)),
8395
)
@@ -181,7 +193,7 @@ test.describe('build-stable-chunks', () => {
181193
.sort()
182194
expect(newChunks).toEqual([
183195
'src/framework/entry.browser.tsx',
184-
'virtual:vite-rsc/client-references/group/src/routes/client.tsx',
196+
'virtual:vite-rsc/client-references/group/facade:src/routes/root.tsx',
185197
])
186198
expect(oldChunks).toEqual(newChunks)
187199
})
@@ -1434,4 +1446,12 @@ function defineTest(f: Fixture) {
14341446
await testBackgroundImage('.test-assets-server-css')
14351447
await testBackgroundImage('.test-assets-client-css')
14361448
})
1449+
1450+
test('lazy', async ({ page }) => {
1451+
await page.goto(f.url())
1452+
await waitForHydration(page)
1453+
await expect(page.getByTestId('test-chunk2')).toHaveText(
1454+
'test-chunk1|test-chunk2|test-chunk2b|test-chunk3|test-chunk3',
1455+
)
1456+
})
14371457
}

packages/plugin-rsc/e2e/render-built-url.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ test.describe(() => {
104104
f.root + '/dist/ssr/__vite_rsc_assets_manifest.js',
105105
'utf-8',
106106
)
107-
expect(manifestFileContent).toContain(`__dynamicBase + "assets/client-`)
107+
expect(manifestFileContent).toContain(
108+
`__dynamicBase + "assets/entry.rsc-`,
109+
)
108110
})
109111
})
110112
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test.describe('build-development', () => {
4545
test('verify development', async ({ page }) => {
4646
let output!: string
4747
page.on('response', async (response) => {
48-
if (response.url().match(/\/assets\/client-[\w-]+\.js$/)) {
48+
if (response.url().match(/\/assets\/entry.rsc-[\w-]+\.js$/)) {
4949
output = await response.text()
5050
}
5151
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use client'
2+
3+
export default function TestChunkClient1() {
4+
return <span>test-chunk1</span>
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use client'
2+
3+
export default function TestChunkClient2() {
4+
return <span>test-chunk2</span>
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use client'
2+
3+
export default function TestChunkClient2b() {
4+
return <span>test-chunk2b</span>
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use client'
2+
3+
export default function TestChunkClient3() {
4+
return <span>test-chunk3</span>
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
3+
const TestChunkClient1 = React.lazy(() => import('./client1'))
4+
const TestChunkServer2 = React.lazy(() => import('./server2'))
5+
const TestChunkServer3 = React.lazy(() => import('./server3'))
6+
const TestChunkServer4 = React.lazy(() => import('./server4'))
7+
8+
export function TestChunk2() {
9+
return (
10+
<div data-testid="test-chunk2">
11+
<TestChunkClient1 />|
12+
<TestChunkServer2 />|
13+
<TestChunkServer3 />|
14+
<TestChunkServer4 />
15+
</div>
16+
)
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import TestChunkClient2 from './client2'
2+
import TestChunkClient2b from './client2b'
3+
4+
export default function TestChunkServer2() {
5+
return (
6+
<>
7+
<TestChunkClient2 />|
8+
<TestChunkClient2b />
9+
</>
10+
)
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import TestChunkClient3 from './client3'
2+
3+
export default function TestChunkServer3() {
4+
return <TestChunkClient3 />
5+
}

0 commit comments

Comments
 (0)