Skip to content

Commit 715864b

Browse files
committed
fix(rsc): preserve server manifest export names
1 parent a0b2af7 commit 715864b

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { vitePluginRscMinimal } from './plugin'
3+
4+
describe('server reference manifest', () => {
5+
it('preserves and deduplicates existing export names', async () => {
6+
const plugins = vitePluginRscMinimal({ enableActionEncryption: false })
7+
const minimalPlugin = plugins.find(
8+
(plugin) => plugin.name === 'rsc:minimal',
9+
)!
10+
const manager = (minimalPlugin.api as any).manager
11+
manager.config = {
12+
command: 'build',
13+
root: '/root',
14+
}
15+
16+
const id = '/root/actions.ts'
17+
manager.serverReferenceMetaMap[id] = {
18+
importId: id,
19+
referenceKey: 'existing',
20+
exportNames: ['cached', 'action', 'cached'],
21+
}
22+
23+
const useServerPlugin = plugins.find(
24+
(plugin) => plugin.name === 'rsc:use-server',
25+
)!
26+
const transformHandler = (useServerPlugin.transform as any).handler
27+
await transformHandler.call(
28+
{
29+
environment: { name: 'rsc', mode: 'build' },
30+
error(error: unknown) {
31+
throw error
32+
},
33+
},
34+
`"use server"; export async function action() {}`,
35+
id,
36+
)
37+
38+
expect(manager.serverReferenceMetaMap[id].exportNames).toEqual([
39+
'cached',
40+
'action',
41+
])
42+
43+
const manifestPlugin = plugins.find(
44+
(plugin) => plugin.name === 'rsc:virtual-vite-rsc/server-references',
45+
)!
46+
const loadHandler = (manifestPlugin.load as any).handler
47+
const manifest = await loadHandler.call(
48+
{ environment: { mode: 'build' } },
49+
'\0virtual:vite-rsc/server-references',
50+
{},
51+
)
52+
53+
expect(manifest.code.match(/\bcached\b/g)).toHaveLength(2)
54+
expect(manifest.code.match(/\baction\b/g)).toHaveLength(2)
55+
})
56+
})

packages/plugin-rsc/src/plugin.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,11 +2017,16 @@ function vitePluginUseServer(
20172017
delete manager.serverReferenceMetaMap[id]
20182018
return
20192019
}
2020+
const existingExportNames =
2021+
manager.serverReferenceMetaMap[id]?.exportNames ?? []
2022+
const exportNames =
2023+
'names' in result ? result.names : result.exportNames
20202024
manager.serverReferenceMetaMap[id] = {
20212025
importId: id,
20222026
referenceKey: getNormalizedId(),
2023-
exportNames:
2024-
'names' in result ? result.names : result.exportNames,
2027+
exportNames: [
2028+
...new Set([...existingExportNames, ...exportNames]),
2029+
],
20252030
}
20262031
const importSource = resolvePackage(`${PKG_NAME}/react/rsc`)
20272032
output.prepend(
@@ -2094,7 +2099,7 @@ function vitePluginUseServer(
20942099
for (const meta of Object.values(manager.serverReferenceMetaMap)) {
20952100
const key = JSON.stringify(meta.referenceKey)
20962101
const id = JSON.stringify(meta.importId)
2097-
const exports = meta.exportNames
2102+
const exports = [...new Set(meta.exportNames)]
20982103
.map((name) => (name === 'default' ? 'default: _default' : name))
20992104
.sort()
21002105
code += `

0 commit comments

Comments
 (0)