Skip to content
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
2 changes: 1 addition & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Custom names are used in the deploy manifest and in wrangler binding sync for lo

#### `inference.build`

Override the build command. Useful for frameworks with their own CLIs (Nuxt, Astro) or custom build scripts. If omitted, the CLI uses the framework's default build command.
Override the build command. Useful for frameworks with their own CLIs (Nuxt, Astro) or static apps with custom build scripts. If omitted, the CLI uses the detected default build command for the current app type.

```json
{ "inference": { "build": "nuxt build --preset cloudflare-module" } }
Expand Down
17 changes: 17 additions & 0 deletions packages/void/src/cli/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ export async function runDeploy(root: string, options?: RunDeployOptions): Promi
const gitCommit = options?.skipBuild ? null : getGitCommit(root);

if (preset) {
preset = {
...preset,
buildCommand: resolveStaticBuildCommand(
preset,
voidConfig.inference?.build,
Boolean(options?.dir),
),
};
await runStaticDeploy(
root,
config,
Expand All @@ -220,6 +228,15 @@ export async function runDeploy(root: string, options?: RunDeployOptions): Promi
}
}

export function resolveStaticBuildCommand(
preset: StaticPreset,
buildOverride?: string,
hasExplicitDir = false,
): string | null {
if (hasExplicitDir) return null;
return buildOverride ?? preset.buildCommand;
}

async function streamDeploy(
client: PlatformClient,
projectId: string,
Expand Down
28 changes: 27 additions & 1 deletion packages/void/src/cli/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ export function detectPreset(

// 4. Vite/Vite+ in deps → static SPA
if (pkg && hasViteLike(pkg)) {
const buildCommand = resolveProjectScriptCommand(root, "build");
return {
buildCommand: resolveProjectCommand(root, "vite build"),
buildCommand: buildCommand ?? resolveProjectCommand(root, "vite build"),
outputDir: resolve(root, "dist"),
appType: "spa",
};
Expand Down Expand Up @@ -247,6 +248,31 @@ function getAllDeps(pkg: Record<string, unknown>): Record<string, string> {
};
}

function detectPackageManager(root: string): "pnpm" | "yarn" | "bun" | "npm" {
if (existsSync(join(root, "pnpm-lock.yaml"))) return "pnpm";
if (existsSync(join(root, "yarn.lock"))) return "yarn";
if (existsSync(join(root, "bun.lock")) || existsSync(join(root, "bun.lockb"))) return "bun";
return "npm";
}

function resolveProjectScriptCommand(root: string, scriptName: string): string | null {
const pkg = readPackageJson(root);
const script = pkg?.scripts;
if (!script || typeof script !== "object" || Array.isArray(script)) return null;
if (typeof (script as Record<string, unknown>)[scriptName] !== "string") return null;

switch (detectPackageManager(root)) {
case "pnpm":
return `pnpm ${scriptName}`;
case "yarn":
return `yarn ${scriptName}`;
case "bun":
return `bun run ${scriptName}`;
default:
return `npm run ${scriptName}`;
}
}

function hasViteLike(pkg: Record<string, unknown>): boolean {
const deps = getAllDeps(pkg);
return "vite" in deps || "vite-plus" in deps;
Expand Down
35 changes: 34 additions & 1 deletion packages/void/test/unit/deploy-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { resolveDistDir, resolveWorkerDirName } from "../../src/cli/deploy";
import {
resolveDistDir,
resolveStaticBuildCommand,
resolveWorkerDirName,
} from "../../src/cli/deploy";
import { FRAMEWORK_PRESETS } from "../../src/cli/preset";
import {
getRequestedProjectSlug,
Expand Down Expand Up @@ -144,6 +148,35 @@ describe("resolveDistDir", () => {
});
});

describe("resolveStaticBuildCommand", () => {
it("uses inference.build when provided", () => {
expect(
resolveStaticBuildCommand(
{
buildCommand: "vite build",
outputDir: "/tmp/dist",
appType: "static",
},
"pnpm build",
),
).toBe("pnpm build");
});

it("keeps explicit --dir deploys buildless even when inference.build is set", () => {
expect(
resolveStaticBuildCommand(
{
buildCommand: null,
outputDir: "/tmp/dist",
appType: "static",
},
"pnpm build",
true,
),
).toBeNull();
});
});

describe("resolveWorkerDirName", () => {
afterEach(() => {
while (tempDirs.length > 0) {
Expand Down
16 changes: 16 additions & 0 deletions packages/void/test/unit/preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ describe("detectPreset", () => {
});
});

it("prefers the project build script for plain Vite SPAs", () => {
const root = setupProject({
pkg: {
dependencies: { vite: "^6.0.0" },
scripts: { build: "vite build && node scripts/postbuild.js" },
},
files: { "pnpm-lock.yaml": "lockfileVersion: '9.0'\n" },
});
const result = detectPreset(root);
expect(result).toEqual({
buildCommand: "pnpm build",
outputDir: resolve(root, "dist"),
appType: "spa",
});
});

it("detects Vite+ SPA (no backend files)", () => {
const root = setupProject({
pkg: { devDependencies: { "vite-plus": "^1.0.0" } },
Expand Down
Loading