Skip to content

Commit

Permalink
wip: emitFile each other
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jun 5, 2024
1 parent b657376 commit 6ef227e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/react-server/src/features/server-action/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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`;
Expand All @@ -162,10 +170,12 @@ function waitForIdlePlugin(): Plugin[] {
const idlePromise = createManualPromise<void>();
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);
Expand Down
9 changes: 9 additions & 0 deletions packages/react-server/src/features/use-client/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6ef227e

Please sign in to comment.