Skip to content

Commit

Permalink
wip: build browser and server in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jun 5, 2024
1 parent 6f5c84e commit b657376
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/react-server/src/features/assets/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function vitePluginServerAssets({
// ensure hmr boundary since css module doesn't have `import.meta.hot.accept`
return code + `if (import.meta.hot) { import.meta.hot.accept() }`;
}
if (manager.buildType === "client") {
if (manager.buildType) {
// TODO: probe manifest to collect css?
const files = await fs.promises.readdir("./dist/rsc/assets", {
withFileTypes: true,
Expand Down
7 changes: 5 additions & 2 deletions packages/react-server/src/features/router/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function routeManifestPluginServer({
name: "server-route-manifest",
apply: "build",
async buildEnd() {
if (manager.buildType === "rsc") {
if (manager.buildType === "parallel") {
const routeFiles = await FastGlob(
"./src/routes/**/(page|layout|error).(js|jsx|ts|tsx)",
);
Expand All @@ -45,6 +45,9 @@ export function routeManifestPluginServer({
];
}

// TODO: route manifest also requires
// - routeManifestPluginServer.buildEnd finishes before
// - routeManifestPluginClient.generateBundle
export function routeManifestPluginClient({
manager,
}: { manager: PluginStateManager }): Plugin[] {
Expand All @@ -53,7 +56,7 @@ export function routeManifestPluginClient({
name: routeManifestPluginClient.name + ":bundle",
apply: "build",
generateBundle(_options, bundle) {
if (manager.buildType === "client") {
if (manager.buildType === "parallel") {
const facadeModuleDeps: Record<string, AssetDeps> = {};
for (const [k, v] of Object.entries(bundle)) {
if (v.type === "chunk" && v.facadeModuleId) {
Expand Down
13 changes: 8 additions & 5 deletions packages/react-server/src/features/server-action/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function vitePluginServerUseServer({
id,
outCode: output.toString(),
});
if (manager.buildType === "rsc") {
if (manager.buildType) {
this.emitFile({
type: "chunk",
id,
Expand All @@ -138,10 +138,10 @@ export function vitePluginServerUseServer({
const virtualPlugin = createVirtualPlugin(
"server-references",
async function () {
if (manager.buildType === "scan") {
return `export default {}`;
}
tinyassert(manager.buildType === "rsc");
// if (manager.buildType === "scan") {
// return `export default {}`;
// }
tinyassert(manager.buildType === "parallel");
await this.load({ id: "\0virtual:wait-for-idle" });
let result = `export default {\n`;
for (const id of manager.rscUseServerIds) {
Expand All @@ -162,6 +162,9 @@ function waitForIdlePlugin(): Plugin[] {
const idlePromise = createManualPromise<void>();
let done = false;
const notIdle = debounce((...args) => {
if (done) {
console.log("[wait-for-idle:done-again]");
}
console.log("[wait-for-idle:done]", { args });
done = true;
idlePromise.resolve();
Expand Down
12 changes: 6 additions & 6 deletions packages/react-server/src/features/use-client/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ export function vitePluginServerUseClient({
`import { registerClientReference as $$proxy } from "${runtimePath}";\n`,
);
manager.rscUseClientIds.add(id);
if (manager.buildType === "scan") {
// to discover server references imported only by client
// we keep code as is and continue crawling
return;
}
// if (manager.buildType === "scan") {
// // to discover server references imported only by client
// // we keep code as is and continue crawling
// return;
// }
return {
code: output.toString(),
map: output.generateMap(),
Expand Down Expand Up @@ -249,7 +249,7 @@ export function vitePluginClientUseClient({
return [
devExternalPlugin,
createVirtualPlugin("client-references", () => {
tinyassert(manager.buildType && manager.buildType !== "rsc");
tinyassert(manager.buildType);
return fs.promises.readFile("dist/rsc/client-references.js", "utf-8");
}),
];
Expand Down
21 changes: 13 additions & 8 deletions packages/react-server/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class PluginStateManager {
config!: ResolvedConfig;
configEnv!: ConfigEnv;

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

Expand Down Expand Up @@ -310,13 +311,17 @@ export function vitePluginReactServer(options?: {
// manager.buildType = "scan";
// await build(reactServerViteConfig);

console.log("▶▶▶ REACT SERVER BUILD (server) [2/4]");
manager.buildType = "rsc";
await build(reactServerViteConfig);
console.log("▶▶▶ REACT SERVER BUILD (server, browser) [(2,3)/4]");
manager.buildType = "parallel";
await Promise.all([build(reactServerViteConfig), build()]);

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

// console.log("▶▶▶ REACT SERVER BUILD (browser) [3/4]");
// manager.buildType = "client";
// await build();

console.log("▶▶▶ REACT SERVER BUILD (ssr) [4/4]");
manager.buildType = "ssr";
Expand Down Expand Up @@ -350,7 +355,7 @@ export function vitePluginReactServer(options?: {
`;
}
// build
if (manager.buildType === "client") {
if (manager.buildType === "parallel") {
// import "runtime-client" for preload
return /* js */ `
import "${SERVER_CSS_PROXY}";
Expand Down

0 comments on commit b657376

Please sign in to comment.