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

wip: wait for idle during build #349

Draft
wants to merge 8 commits into
base: refactor-build-server-client-reference
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
78 changes: 63 additions & 15 deletions packages/react-server/src/features/server-action/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
transformDirectiveProxyExport,
transformServerActionServer,
} from "@hiogawa/transforms";
import { createDebug, tinyassert } from "@hiogawa/utils";
import {
createDebug,
createManualPromise,
debounce,
tinyassert,
} from "@hiogawa/utils";
import { type Plugin, type PluginOption, parseAstAsync } from "vite";
import type { PluginStateManager } from "../../plugin";
import {
Expand Down Expand Up @@ -109,6 +114,12 @@
id,
outCode: output.toString(),
});
if (manager.buildType === "rsc") {
this.emitFile({
type: "chunk",
id,
});
}
return {
code: output.toString(),
map: output.generateMap(),
Expand All @@ -124,19 +135,56 @@
};

// expose server references for RSC build via virtual module
const virtualPlugin = createVirtualPlugin("server-references", async () => {
if (manager.buildType === "scan") {
return `export default {}`;
}
tinyassert(manager.buildType === "rsc");
let result = `export default {\n`;
for (const id of manager.rscUseServerIds) {
result += `"${hashString(id)}": () => import("${id}"),\n`;
}
result += "};\n";
debug("[virtual:server-references]", result);
return result;
});
const virtualPlugin = createVirtualPlugin(
"server-references",
async function () {
if (manager.buildType === "scan") {
return `export default {}`;
}
tinyassert(manager.buildType === "rsc");
await this.load({ id: "\0virtual:wait-for-idle" });
let result = `export default {\n`;
for (const id of manager.rscUseServerIds) {
result += `"${hashString(id)}": () => import("${id}"),\n`;
}
result += "};\n";
debug("[virtual:server-references]", result);
return result;
},
);

return [transformPlugin, virtualPlugin];
return [transformPlugin, virtualPlugin, waitForIdlePlugin()];
}

// https://github.com/rollup/rollup/issues/4985#issuecomment-1936333388
// https://github.com/ArnaudBarre/downwind/blob/1d47b6a3f1e7bc271d0bb5bd96cfbbea68445510/src/vitePlugin.ts#L164
function waitForIdlePlugin(): Plugin[] {
const idlePromise = createManualPromise<void>();
let done = false;

Check failure on line 163 in packages/react-server/src/features/server-action/plugin.tsx

View workflow job for this annotation

GitHub Actions / test

'done' is declared but its value is never read.
const notIdle = debounce((...args) => {
console.log("[wait-for-idle:done]", { args });
done = true;
idlePromise.resolve();
}, 200);

return [
{
name: waitForIdlePlugin.name,
apply: "build",
enforce: "pre",
buildStart() {
this.emitFile({
type: "chunk",
id: "virtual:wait-for-idle",
});
},
resolveId: notIdle,
load: notIdle,
transform: notIdle,
},
createVirtualPlugin("wait-for-idle", async () => {
await idlePromise;
return `export default "** wait-for-idle **"`;
}),
];
}
10 changes: 6 additions & 4 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type Plugin,
type PluginOption,
type ResolvedConfig,
Rollup,
type ViteDevServer,
build,
createLogger,
Expand Down Expand Up @@ -62,6 +63,8 @@ class PluginStateManager {
configEnv!: ConfigEnv;

buildType?: "scan" | "rsc" | "client" | "ssr";
buildContextServer?: Rollup.PluginContext;
buildContextBrowser?: Rollup.PluginContext;

routeToClientReferences: Record<string, string[]> = {};
routeManifest?: RouteManifest;
Expand Down Expand Up @@ -303,13 +306,12 @@ export function vitePluginReactServer(options?: {
apply: "build",
async buildStart(_options) {
if (!manager.buildType) {
console.log("▶▶▶ REACT SERVER BUILD (scan) [1/4]");
manager.buildType = "scan";
await build(reactServerViteConfig);
// 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]");
Expand Down
Loading