Skip to content
Draft
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
24 changes: 22 additions & 2 deletions packages/plugin-rsc/examples/browser/lib/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import assert from 'node:assert'
import { rmSync } from 'node:fs'
import path from 'node:path'
import { normalizePath, type Plugin } from 'vite'
import { normalizePath, type Plugin, type Rollup } from 'vite'

export default function vitePluginRscBrowser(): Plugin[] {
let rscBundle: Rollup.OutputBundle

return [
{
name: 'rsc-browser',
Expand All @@ -19,6 +22,11 @@ export default function vitePluginRscBrowser(): Plugin[] {
rsc: {
build: {
outDir: 'dist/client/__server',
rollupOptions: {
output: {
entryFileNames: '[name]-[hash].js',
},
},
},
keepProcessEnv: false,
resolve: {
Expand Down Expand Up @@ -75,8 +83,17 @@ export default function vitePluginRscBrowser(): Plugin[] {
return { id: source, external: true }
}
},
generateBundle(_options, bundle) {
if (this.environment.name === 'rsc') {
rscBundle = bundle
}
},
renderChunk(code, chunk) {
if (code.includes('virtual:vite-rsc-browser/load-rsc')) {
assert(this.environment.name === 'client')
const rscEntry = Object.values(rscBundle).find(
(v) => v.type === 'chunk' && v.isEntry,
)!
const config = this.environment.getTopLevelConfig()
const replacement = normalizeRelativePath(
path.relative(
Expand All @@ -85,7 +102,10 @@ export default function vitePluginRscBrowser(): Plugin[] {
chunk.fileName,
'..',
),
path.join(config.environments.rsc.build.outDir, 'index.js'),
path.join(
config.environments.rsc.build.outDir,
rscEntry.fileName,
),
),
)
code = code.replaceAll(
Expand Down
Loading