Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(react-server)!: parallel client/server reference build #344

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion packages/react-server/examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"build": "vite build --ssr",
"preview": "vite preview",
"test-e2e": "playwright test",
"test-e2e-preview": "E2E_PREVIEW=1 playwright test",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-server/examples/starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"build": "vite build --ssr",
"preview": "vite preview",
"cf-build": "SSR_ENTRY=/src/adapters/cloudflare-workers.ts pnpm build && bash misc/cloudflare-workers/build.sh",
"cf-preview": "cd misc/cloudflare-workers && wrangler dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-server/src/features/router/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function routeManifestPluginClient({
}
const routeToAssetDeps = objectMapValues(
manager.routeToClientReferences,
// facade module might not exist when dynamic import is also imported statically
// facade module might not exist when dynamic import is also imported statically?
(ids) =>
mergeAssetDeps(
ids.map((id) => facadeModuleDeps[id]).filter(typedBoolean),
Expand Down
21 changes: 19 additions & 2 deletions packages/react-server/src/features/server-action/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createDebug, tinyassert } from "@hiogawa/utils";
import { type Plugin, type PluginOption, parseAstAsync } from "vite";
import type { PluginStateManager } from "../../plugin";
import {
type CustomModuleMeta,
USE_SERVER,
createVirtualPlugin,
hashString,
Expand Down Expand Up @@ -56,7 +57,15 @@ const $$proxy = (id, name) => createServerReference(id + "#" + name);
id,
outCode: output.toString(),
});
return { code: output.toString(), map: output.generateMap() };
return {
code: output.toString(),
map: output.generateMap(),
meta: {
$$rsc: {
type: "server",
},
} satisfies CustomModuleMeta,
};
},
};
}
Expand Down Expand Up @@ -100,7 +109,15 @@ export function vitePluginServerUseServer({
id,
outCode: output.toString(),
});
return { code: output.toString(), map: output.generateMap() };
return {
code: output.toString(),
map: output.generateMap(),
meta: {
$$rsc: {
type: "server",
},
} satisfies CustomModuleMeta,
};
}
return;
},
Expand Down
16 changes: 5 additions & 11 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export function vitePluginReactServer(options?: {
},
};

// orchestrate four builds from a single vite (browser) build
// orchestrate four builds from a single vite (ssr) build
const buildOrchestrationPlugin: Plugin = {
name: vitePluginReactServer.name + ":build",
apply: "build",
Expand All @@ -306,24 +306,18 @@ export function vitePluginReactServer(options?: {
console.log("▶▶▶ REACT SERVER BUILD (scan) [1/4]");
manager.buildType = "scan";
await build(reactServerViteConfig);

console.log("▶▶▶ REACT SERVER BUILD (server) [2/4]");
manager.buildType = "rsc";
manager.rscUseClientIds.clear();
await build(reactServerViteConfig);

console.log("▶▶▶ REACT SERVER BUILD (browser) [3/4]");
manager.buildType = "client";
}
},
async closeBundle() {
// TODO: build ssr only when client build succeeds
if (manager.buildType === "client") {
await build();

console.log("▶▶▶ REACT SERVER BUILD (ssr) [4/4]");
manager.buildType = "ssr";
await build({
build: {
ssr: true,
},
});
}
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-server/src/plugin/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function invalidateModule(server: ViteDevServer, id: string) {
// cf. https://github.com/vercel/next.js/blob/5ae286ffd664e5c76841ed64f6e2da85a0835922/packages/next/src/build/webpack/loaders/get-module-build-info.ts#L8
export type CustomModuleMeta = {
$$rsc?: {
type: "client";
type: "client" | "server";
};
};

Expand Down