Skip to content

Commit a34f8c5

Browse files
authored
refactor(rsc): move writeManifest inside buildApp hook (#659)
1 parent 5df0070 commit a34f8c5

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

packages/plugin-rsc/examples/ssg/vite.config.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default defineConfig((env) => ({
2121
ssr: './src/framework/entry.ssr.tsx',
2222
},
2323
serverHandler: env.isPreview ? false : undefined,
24+
useBuildAppHook: true,
2425
}),
2526
rscSsgPlugin(),
2627
inspect(),
@@ -38,15 +39,9 @@ function rscSsgPlugin(): Plugin[] {
3839
}
3940
}
4041
},
41-
// Use post ssr writeBundle to wait for app is fully built.
42-
// On Vite 7, you can use `buildApp` hook instead.
43-
writeBundle: {
44-
order: 'post',
45-
async handler() {
46-
if (this.environment.name === 'ssr') {
47-
const config = this.environment.getTopLevelConfig()
48-
await renderStatic(config)
49-
}
42+
buildApp: {
43+
async handler(builder) {
44+
await renderStatic(builder.config)
5045
},
5146
},
5247
},

packages/plugin-rsc/src/plugin.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,7 @@ export default function vitePluginRsc(
202202
clientReferenceMetaMap = sortObject(clientReferenceMetaMap)
203203
serverResourcesMetaMap = sortObject(serverResourcesMetaMap)
204204
await builder.build(builder.environments.client!)
205-
206-
const assetsManifestCode = `export default ${serializeValueWithRuntime(
207-
buildAssetsManifest,
208-
)}`
209-
const manifestPath = path.join(
210-
builder.environments!.rsc!.config.build!.outDir!,
211-
BUILD_ASSETS_MANIFEST_NAME,
212-
)
213-
fs.writeFileSync(manifestPath, assetsManifestCode)
205+
writeAssetsManifest(['rsc'])
214206
return
215207
}
216208

@@ -229,6 +221,22 @@ export default function vitePluginRsc(
229221
serverResourcesMetaMap = sortObject(serverResourcesMetaMap)
230222
await builder.build(builder.environments.client!)
231223
await builder.build(builder.environments.ssr!)
224+
writeAssetsManifest(['ssr', 'rsc'])
225+
}
226+
227+
function writeAssetsManifest(environmentNames: string[]) {
228+
// output client manifest to non-client build directly.
229+
// this makes server build to be self-contained and deploy-able for cloudflare.
230+
const assetsManifestCode = `export default ${serializeValueWithRuntime(
231+
buildAssetsManifest,
232+
)}`
233+
for (const name of environmentNames) {
234+
const manifestPath = path.join(
235+
config.environments[name]!.build.outDir,
236+
BUILD_ASSETS_MANIFEST_NAME,
237+
)
238+
fs.writeFileSync(manifestPath, assetsManifestCode)
239+
}
232240
}
233241

234242
return [
@@ -752,24 +760,6 @@ export default function vitePluginRsc(
752760
}
753761
return
754762
},
755-
writeBundle() {
756-
// TODO: move this to `buildApp`.
757-
// note that we already do this in buildApp for no-ssr case.
758-
if (this.environment.name === 'ssr') {
759-
// output client manifest to non-client build directly.
760-
// this makes server build to be self-contained and deploy-able for cloudflare.
761-
const assetsManifestCode = `export default ${serializeValueWithRuntime(
762-
buildAssetsManifest,
763-
)}`
764-
for (const name of ['ssr', 'rsc']) {
765-
const manifestPath = path.join(
766-
config.environments[name]!.build.outDir,
767-
BUILD_ASSETS_MANIFEST_NAME,
768-
)
769-
fs.writeFileSync(manifestPath, assetsManifestCode)
770-
}
771-
}
772-
},
773763
},
774764
createVirtualPlugin('vite-rsc/bootstrap-script-content', function () {
775765
assert(this.environment.name !== 'client')

0 commit comments

Comments
 (0)