Skip to content

Commit

Permalink
Merge pull request #349 from nksaraf/build-processes
Browse files Browse the repository at this point in the history
run each router build in separate node processes
  • Loading branch information
nksaraf authored Aug 26, 2024
2 parents 26078ae + 8707a79 commit 0cf5a1a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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

0 comments on commit 0cf5a1a

Please sign in to comment.