Skip to content

Commit ff44ae4

Browse files
authored
refactor(rsc): split encryption runtime exports (#660)
1 parent c1383f8 commit ff44ae4

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -938,17 +938,6 @@ function hashString(v: string) {
938938
return createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
939939
}
940940

941-
function normalizeReferenceId(id: string, name: 'client' | 'rsc') {
942-
if (!server) {
943-
return hashString(path.relative(config.root, id))
944-
}
945-
946-
// align with how Vite import analysis would rewrite id
947-
// to avoid double modules on browser and ssr.
948-
const environment = server.environments[name]!
949-
return normalizeViteImportAnalysisUrl(environment, id)
950-
}
951-
952941
function vitePluginUseClient(
953942
useClientPluginOptions: Pick<
954943
RscPluginOptions,
@@ -1243,7 +1232,14 @@ function vitePluginUseServer(
12431232
// module identity of `import(id)` like browser, so we simply strip it off.
12441233
id = id.split('?v=')[0]!
12451234
}
1246-
normalizedId_ = normalizeReferenceId(id, 'rsc')
1235+
if (config.command === 'build') {
1236+
normalizedId_ = hashString(path.relative(config.root, id))
1237+
} else {
1238+
normalizedId_ = normalizeViteImportAnalysisUrl(
1239+
server.environments.rsc!,
1240+
id,
1241+
)
1242+
}
12471243
}
12481244
return normalizedId_
12491245
}
@@ -1262,17 +1258,26 @@ function vitePluginUseServer(
12621258
)}, ${JSON.stringify(name)})`,
12631259
rejectNonAsyncFunction: true,
12641260
encode: enableEncryption
1265-
? (value) => `$$ReactServer.encryptActionBoundArgs(${value})`
1261+
? (value) =>
1262+
`__vite_rsc_encryption_runtime.encryptActionBoundArgs(${value})`
12661263
: undefined,
12671264
decode: enableEncryption
12681265
? (value) =>
1269-
`await $$ReactServer.decryptActionBoundArgs(${value})`
1266+
`await __vite_rsc_encryption_runtime.decryptActionBoundArgs(${value})`
12701267
: undefined,
12711268
})
12721269
if (!output.hasChanged()) return
12731270
serverReferences[getNormalizedId()] = id
1274-
const importSource = resolvePackage(`${PKG_NAME}/rsc`)
1271+
const importSource = resolvePackage(`${PKG_NAME}/react/rsc`)
12751272
output.prepend(`import * as $$ReactServer from "${importSource}";\n`)
1273+
if (enableEncryption) {
1274+
const importSource = resolvePackage(
1275+
`${PKG_NAME}/utils/encryption-runtime`,
1276+
)
1277+
output.prepend(
1278+
`import * as __vite_rsc_encryption_runtime from ${JSON.stringify(importSource)};\n`,
1279+
)
1280+
}
12761281
return {
12771282
code: output.toString(),
12781283
map: output.generateMap({ hires: 'boundary' }),

packages/plugin-rsc/tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default defineConfig({
2222
'src/rsc-html-stream/ssr.ts',
2323
'src/rsc-html-stream/browser.ts',
2424
'src/utils/rpc.ts',
25+
'src/utils/encryption-runtime.ts',
2526
],
2627
format: ['esm'],
2728
external: [/^virtual:/, /^@vitejs\/plugin-rsc\/vendor\//],

0 commit comments

Comments
 (0)