From 6ef227e0090013a42f28443d76a6bb158dbf01c7 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 5 Jun 2024 15:46:15 +0900 Subject: [PATCH] wip: emitFile each other --- .../src/features/server-action/plugin.tsx | 16 +++++++++++--- .../src/features/use-client/plugin.ts | 9 ++++++++ packages/react-server/src/plugin/index.ts | 22 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/react-server/src/features/server-action/plugin.tsx b/packages/react-server/src/features/server-action/plugin.tsx index 083ebda36..f15426fb9 100644 --- a/packages/react-server/src/features/server-action/plugin.tsx +++ b/packages/react-server/src/features/server-action/plugin.tsx @@ -62,6 +62,13 @@ const $$proxy = (id, name) => createServerReference(id + "#" + name); id, outCode: output.toString(), }); + if (manager.buildType === "parallel") { + tinyassert(manager.buildContextServer); + manager.buildContextServer.emitFile({ + type: "chunk", + id, + }); + } return { code: output.toString(), map: output.generateMap(), @@ -143,6 +150,7 @@ export function vitePluginServerUseServer({ // } tinyassert(manager.buildType === "parallel"); await this.load({ id: "\0virtual:wait-for-idle" }); + console.log("[server-references]", manager.rscUseServerIds); let result = `export default {\n`; for (const id of manager.rscUseServerIds) { result += `"${hashString(id)}": () => import("${id}"),\n`; @@ -162,10 +170,12 @@ function waitForIdlePlugin(): Plugin[] { const idlePromise = createManualPromise(); let done = false; const notIdle = debounce((...args) => { - if (done) { - console.log("[wait-for-idle:done-again]"); + if (0) { + if (done) { + console.log("[wait-for-idle:done-again]"); + } + console.log("[wait-for-idle:done]", { args }); } - console.log("[wait-for-idle:done]", { args }); done = true; idlePromise.resolve(); }, 200); diff --git a/packages/react-server/src/features/use-client/plugin.ts b/packages/react-server/src/features/use-client/plugin.ts index a036ff204..de844d733 100644 --- a/packages/react-server/src/features/use-client/plugin.ts +++ b/packages/react-server/src/features/use-client/plugin.ts @@ -147,6 +147,13 @@ export function vitePluginServerUseClient({ `import { registerClientReference as $$proxy } from "${runtimePath}";\n`, ); manager.rscUseClientIds.add(id); + if (manager.buildType === "parallel") { + tinyassert(manager.buildContextBrowser); + manager.buildContextBrowser.emitFile({ + type: "chunk", + id, + }); + } // if (manager.buildType === "scan") { // // to discover server references imported only by client // // we keep code as is and continue crawling @@ -173,6 +180,8 @@ export function vitePluginServerUseClient({ */ closeBundle: { async handler() { + // TODO: move to client virual first... + console.log("[client-references]", manager.rscUseClientIds); let result = `export default {\n`; for (let id of manager.rscUseClientIds) { // virtual module needs to be mapped back to the original form diff --git a/packages/react-server/src/plugin/index.ts b/packages/react-server/src/plugin/index.ts index e2558628b..894dfff5d 100644 --- a/packages/react-server/src/plugin/index.ts +++ b/packages/react-server/src/plugin/index.ts @@ -184,6 +184,19 @@ export function vitePluginReactServer(options?: { }, }, + // TODO: + // ensure both `buildContextBrowser` and `buildContextServer` + // are ready during `buildStart` + { + name: "build-context-server", + apply: "build", + buildStart() { + if (manager.buildType === "parallel") { + manager.buildContextServer = this; + } + }, + }, + ...(options?.plugins ?? []), ], build: { @@ -333,6 +346,15 @@ export function vitePluginReactServer(options?: { return [ rscParentPlugin, buildOrchestrationPlugin, + { + name: "build-context-browser", + apply: "build", + buildStart() { + if (manager.buildType === "parallel") { + manager.buildContextBrowser = this; + } + }, + }, vitePluginSilenceDirectiveBuildWarning(), vitePluginClientUseServer({ manager,