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

run each router build in separate node processes #349

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/polite-llamas-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

perf: run each router build in separate node processes
6 changes: 5 additions & 1 deletion packages/vinxi/bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ const command = defineCommand({
type: "string",
description: "Server preset (default: node-server)",
},
router: {
type: "string",
description: "Router to build (by default, vinxi builds all your routers in separate processed and then into an app bundle, use this option to just build a single router",
},
version: {
type: "boolean",
description: "Print the versions of Vinxi core dependencies",
Expand All @@ -224,7 +228,7 @@ const command = defineCommand({
}
process.env.NODE_ENV = "production";
const { createBuild } = await import("../lib/build.js");
await createBuild(app, { preset: args.preset });
await createBuild(app, { preset: args.preset, router: args.router });
},
},
start: {
Expand Down
36 changes: 32 additions & 4 deletions packages/vinxi/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,35 @@ const require = createRequire(import.meta.url);
* @param {BuildConfig} buildConfig
*/
export async function createBuild(app, buildConfig) {
console.log("\n");
console.log(`⚙ ${c.green("Building your app...")}`);
await app.hooks.callHook("app:build:start", { app, buildConfig });
const { existsSync, promises: fsPromises, readFileSync } = await import("fs");
const { join } = await import("./path.js");
const { fileURLToPath } = await import("url");

if (buildConfig.router) {
console.log("\n");
console.log(`⚙ ${c.green(`Building your router ${buildConfig.router}...`)}`);
let router = app.config.routers.find((r) => r.name === buildConfig.router);
if (router.build !== false) {
if (existsSync(router.outDir)) {
await withLogger({ router, requestId: "clean" }, async () => {
console.log(`removing ${router.outDir}`);
await fsPromises.rm(router.outDir, { recursive: true });
});
}

await withLogger({ router, requestId: "build" }, async () => {
await createRouterBuild(app, router);
});
}

console.log(`⚙ ${c.green(`Built your router ${buildConfig.router} successfully`)}`);
return
}

console.log("\n");
console.log(`⚙ ${c.green("Building your app...")}`);
await app.hooks.callHook("app:build:start", { app, buildConfig });

app.config.routers = app.config.routers.filter(
(router) => router.build !== false,
);
Expand All @@ -56,7 +79,7 @@ export async function createBuild(app, buildConfig) {
for (const router of app.config.routers) {
if (router.type !== "static" && router.build !== false) {
await withLogger({ router, requestId: "build" }, async () => {
await createRouterBuild(app, router);
await createRouterBuildInWorker(app, router);
});
}
}
Expand Down Expand Up @@ -323,6 +346,11 @@ async function createViteBuild(config) {
return output;
}

async function createRouterBuildInWorker(app, router) {
const sh = await import("../runtime/sh.js");
const { fileURLToPath } = await import("url");
await sh.default`node ${fileURLToPath(new URL("../bin/cli.mjs", import.meta.url).href)} build --router=${router.name}`;
}
/**
*
* @param {import("./app.js").App} app
Expand Down
Loading