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
13 changes: 4 additions & 9 deletions packages/plugin-rsc/examples/ssg/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig((env) => ({
ssr: './src/framework/entry.ssr.tsx',
},
serverHandler: env.isPreview ? false : undefined,
useBuildAppHook: true,
}),
rscSsgPlugin(),
inspect(),
Expand All @@ -38,15 +39,9 @@ function rscSsgPlugin(): Plugin[] {
}
}
},
// Use post ssr writeBundle to wait for app is fully built.
// On Vite 7, you can use `buildApp` hook instead.
writeBundle: {
order: 'post',
async handler() {
if (this.environment.name === 'ssr') {
const config = this.environment.getTopLevelConfig()
await renderStatic(config)
}
buildApp: {
async handler(builder) {
await renderStatic(builder.config)
},
},
},
Expand Down
44 changes: 17 additions & 27 deletions packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,7 @@ export default function vitePluginRsc(
clientReferenceMetaMap = sortObject(clientReferenceMetaMap)
serverResourcesMetaMap = sortObject(serverResourcesMetaMap)
await builder.build(builder.environments.client!)

const assetsManifestCode = `export default ${serializeValueWithRuntime(
buildAssetsManifest,
)}`
const manifestPath = path.join(
builder.environments!.rsc!.config.build!.outDir!,
BUILD_ASSETS_MANIFEST_NAME,
)
fs.writeFileSync(manifestPath, assetsManifestCode)
writeAssetsManifest(['rsc'])
return
}

Expand All @@ -229,6 +221,22 @@ export default function vitePluginRsc(
serverResourcesMetaMap = sortObject(serverResourcesMetaMap)
await builder.build(builder.environments.client!)
await builder.build(builder.environments.ssr!)
writeAssetsManifest(['ssr', 'rsc'])
}

function writeAssetsManifest(environmentNames: string[]) {
// output client manifest to non-client build directly.
// this makes server build to be self-contained and deploy-able for cloudflare.
const assetsManifestCode = `export default ${serializeValueWithRuntime(
buildAssetsManifest,
)}`
for (const name of environmentNames) {
const manifestPath = path.join(
config.environments[name]!.build.outDir,
BUILD_ASSETS_MANIFEST_NAME,
)
fs.writeFileSync(manifestPath, assetsManifestCode)
}
}

return [
Expand Down Expand Up @@ -752,24 +760,6 @@ export default function vitePluginRsc(
}
return
},
writeBundle() {
// TODO: move this to `buildApp`.
// note that we already do this in buildApp for no-ssr case.
if (this.environment.name === 'ssr') {
// output client manifest to non-client build directly.
// this makes server build to be self-contained and deploy-able for cloudflare.
const assetsManifestCode = `export default ${serializeValueWithRuntime(
buildAssetsManifest,
)}`
for (const name of ['ssr', 'rsc']) {
const manifestPath = path.join(
config.environments[name]!.build.outDir,
BUILD_ASSETS_MANIFEST_NAME,
)
fs.writeFileSync(manifestPath, assetsManifestCode)
}
}
},
},
createVirtualPlugin('vite-rsc/bootstrap-script-content', function () {
assert(this.environment.name !== 'client')
Expand Down
Loading