Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,17 +938,6 @@ function hashString(v: string) {
return createHash('sha256').update(v).digest().toString('hex').slice(0, 12)
}

function normalizeReferenceId(id: string, name: 'client' | 'rsc') {
if (!server) {
return hashString(path.relative(config.root, id))
}

// align with how Vite import analysis would rewrite id
// to avoid double modules on browser and ssr.
const environment = server.environments[name]!
return normalizeViteImportAnalysisUrl(environment, id)
}

function vitePluginUseClient(
useClientPluginOptions: Pick<
RscPluginOptions,
Expand Down Expand Up @@ -1243,7 +1232,14 @@ function vitePluginUseServer(
// module identity of `import(id)` like browser, so we simply strip it off.
id = id.split('?v=')[0]!
}
normalizedId_ = normalizeReferenceId(id, 'rsc')
if (config.command === 'build') {
normalizedId_ = hashString(path.relative(config.root, id))
} else {
normalizedId_ = normalizeViteImportAnalysisUrl(
server.environments.rsc!,
id,
)
}
}
return normalizedId_
}
Expand All @@ -1262,17 +1258,26 @@ function vitePluginUseServer(
)}, ${JSON.stringify(name)})`,
rejectNonAsyncFunction: true,
encode: enableEncryption
? (value) => `$$ReactServer.encryptActionBoundArgs(${value})`
? (value) =>
`__vite_rsc_encryption_runtime.encryptActionBoundArgs(${value})`
: undefined,
decode: enableEncryption
? (value) =>
`await $$ReactServer.decryptActionBoundArgs(${value})`
`await __vite_rsc_encryption_runtime.decryptActionBoundArgs(${value})`
: undefined,
})
if (!output.hasChanged()) return
serverReferences[getNormalizedId()] = id
const importSource = resolvePackage(`${PKG_NAME}/rsc`)
const importSource = resolvePackage(`${PKG_NAME}/react/rsc`)
output.prepend(`import * as $$ReactServer from "${importSource}";\n`)
if (enableEncryption) {
const importSource = resolvePackage(
`${PKG_NAME}/utils/encryption-runtime`,
)
output.prepend(
`import * as __vite_rsc_encryption_runtime from ${JSON.stringify(importSource)};\n`,
)
}
return {
code: output.toString(),
map: output.generateMap({ hires: 'boundary' }),
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-rsc/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
'src/rsc-html-stream/ssr.ts',
'src/rsc-html-stream/browser.ts',
'src/utils/rpc.ts',
'src/utils/encryption-runtime.ts',
],
format: ['esm'],
external: [/^virtual:/, /^@vitejs\/plugin-rsc\/vendor\//],
Expand Down
Loading