Skip to content

Commit

Permalink
wip: virtual:ssr-assets
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 18, 2024
1 parent d0d0d1c commit f623f26
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion react-server-dom-vite-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"build": "vite build --app",
"preview": "vite preview",
"tsc": "tsc -b",
"lint": "biome check --write .",
Expand Down
4 changes: 4 additions & 0 deletions react-server-dom-vite-example/src/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ async function main() {
{},
);
ReactDomClient.hydrateRoot(document, payload.root);

const el = document.createElement("div");
el.textContent = "hydrated";
document.body.appendChild(el);
}

main();
9 changes: 4 additions & 5 deletions react-server-dom-vite-example/src/entry.ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export default async function handler(
{},
);

const ssrAssets = await import("virtual:ssr-assets");

const htmlStream = fromPipeableToWebReadable(
ReactDomServer.renderToPipeableStream(payload.root, {
bootstrapModules: [
import.meta.env.DEV ? "/src/entry.client.tsx" : "/todo",
],
bootstrapModules: ssrAssets.bootstrapModules,
}),
);

Expand All @@ -64,7 +64,6 @@ async function importRscEntry(): Promise<typeof import("./entry.rsc")> {
if (import.meta.env.DEV) {
return await __rscRunner.import("/src/entry.rsc.tsx");
} else {
// @ts-ignore
return await import("virtual:build-rsc-entry");
return (await import("virtual:build-rsc-entry")) as any;
}
}
8 changes: 8 additions & 0 deletions react-server-dom-vite-example/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ declare module "@jacob-ebey/react-server-dom-vite/client" {
manifest: unknown,
): Promise<T>;
}

declare module "virtual:ssr-assets" {
export const bootstrapModules: string[];
}

declare module "virtual:build-rsc-entry" {
export {};
}
43 changes: 37 additions & 6 deletions react-server-dom-vite-example/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import assert from "node:assert";
import path from "node:path";
import {
type Manifest,
type Plugin,
type RunnableDevEnvironment,
createRunnableDevEnvironment,
defineConfig,
} from "vite";

let clientManifest: Manifest;

export default defineConfig({
appType: "custom",
environments: {
client: {
optimizeDeps: {
entries: [],
},
build: {
manifest: true,
outDir: "dist/client",
rollupOptions: {
input: { index: "/src/entry.client.tsx" },
Expand Down Expand Up @@ -87,12 +89,41 @@ export default defineConfig({
};
},
},
createVirtualPlugin("build-rsc-entry", function () {
// TODO
return `export {}`;
{
name: "virtual:build-rsc-entry",
resolveId(source) {
if (source === "virtual:build-rsc-entry") {
// externalize rsc entry in ssr entry as relative path
return { id: "../rsc/index.js", external: true };
}
},
},
createVirtualPlugin("ssr-assets", function () {
assert(this.environment.name === "ssr");
let bootstrapModules: string[] = [];
if (this.environment.mode === "dev") {
bootstrapModules = ["/src/entry.client.tsx"];
}
if (this.environment.mode === "build") {
bootstrapModules = [clientManifest["src/entry.client.tsx"].file];
}
return `export const bootstrapModules = ${JSON.stringify(bootstrapModules)}`;
}),
{
name: "misc",
writeBundle(_options, bundle) {
if (this.environment.name === "client") {
const output = bundle[".vite/manifest.json"];
assert(output.type === "asset");
assert(typeof output.source === "string");
clientManifest = JSON.parse(output.source);
}
},
},
],
builder: {
sharedPlugins: true,
sharedConfigBuild: true,
async buildApp(builder) {
await builder.build(builder.environments.rsc);
await builder.build(builder.environments.client);
Expand Down

0 comments on commit f623f26

Please sign in to comment.