From 154d3ca016619d722d7482b50c91027e9cc4513e Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 24 Nov 2026 06:12:43 +0800 Subject: [PATCH 1/2] fix(deploy): upload dist/client for a Vite app built with void A Vite app that depends on `void` builds with voidPlugin(), which writes browser files to dist/client and the server bundle, its source map and wrangler.json to dist/ssr. With no backend files, detectPreset step 4 picked `dist` as the static output, so `void deploy` published the server files and `/` returned 404 (index.html sat at /client/index.html). Pick dist/client in that case. A plain Vite app keeps `dist`. Co-Authored-By: Claude Opus 5.5 --- docs/guide/app-types.md | 2 +- docs/reference/config.md | 2 +- packages/void/src/cli/preset.ts | 14 +++++++++++++- packages/void/test/unit/preset.test.ts | 25 +++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/guide/app-types.md b/docs/guide/app-types.md index 5dfa403e6..60269c39e 100644 --- a/docs/guide/app-types.md +++ b/docs/guide/app-types.md @@ -116,4 +116,4 @@ To lock the app type and skip auto-detection, set `inference.appType` in [`void. ``` - `inference.appType`: `"void"`, `"framework"`, `"spa"`, or `"static"` -- `inference.outputDir`: output directory for static app types (relative to project root, defaults to `"dist"`) +- `inference.outputDir`: output directory for static app types (relative to project root). Defaults to `"dist"`, or `"dist/client"` when the project depends on `void`, because `voidPlugin()` writes browser files there diff --git a/docs/reference/config.md b/docs/reference/config.md index 2b3c012ce..4786a50bb 100644 --- a/docs/reference/config.md +++ b/docs/reference/config.md @@ -622,4 +622,4 @@ App type. If omitted, auto-detected on first deploy. See [Supported App Types](. #### `inference.outputDir` -Output directory for static deploys, relative to the project root. Only relevant when `inference.appType` is `"spa"` or `"static"`. Defaults to `"dist"`. +Output directory for static deploys, relative to the project root. Only relevant when `inference.appType` is `"spa"` or `"static"`. Defaults to `"dist"`, or `"dist/client"` when the project depends on `void`, because `voidPlugin()` writes browser files there and server files to `dist/ssr`. diff --git a/packages/void/src/cli/preset.ts b/packages/void/src/cli/preset.ts index 6b6b4a471..29c692a9e 100644 --- a/packages/void/src/cli/preset.ts +++ b/packages/void/src/cli/preset.ts @@ -279,7 +279,7 @@ export function detectPreset( const buildCommand = resolveProjectScriptCommand(root, 'build'); return { buildCommand: buildCommand ?? resolveProjectCommand(root, 'vite build'), - outputDir: resolve(root, 'dist'), + outputDir: resolve(root, viteSpaOutputDir(pkg)), appType: 'spa', }; } @@ -379,6 +379,18 @@ function hasViteLike(pkg: Record): boolean { return hasPackageDependency(pkg, 'vite') || hasPackageDependency(pkg, 'vite-plus'); } +/** + * Where a Vite SPA build puts its browser files. A project that depends on + * `void` builds with `voidPlugin()` (`void init` adds it), which splits the + * output into `dist/client` (browser files) and `dist/ssr` (the server + * bundle, its source map and `wrangler.json`). Only `dist/client` may be + * uploaded: uploading `dist` publishes the server files and leaves `/` + * without an `index.html`. + */ +function viteSpaOutputDir(pkg: Record): string { + return hasPackageDependency(pkg, 'void') ? 'dist/client' : 'dist'; +} + function detectViteBinary(root: string): 'vite' | 'vp' { const pkg = readPackageJson(root); if (pkg && hasPackageDependency(pkg, 'vite-plus')) { diff --git a/packages/void/test/unit/preset.test.ts b/packages/void/test/unit/preset.test.ts index 588ceffc7..76705dd47 100644 --- a/packages/void/test/unit/preset.test.ts +++ b/packages/void/test/unit/preset.test.ts @@ -153,6 +153,31 @@ describe('detectPreset', () => { }); }); + // voidPlugin() splits the build into dist/client and dist/ssr. Uploading dist + // published the server bundle, its source map and wrangler.json, and left / + // without an index.html. + it('uploads only dist/client for a Vite app built with void and no backend', () => { + const root = setupProject({ + pkg: { devDependencies: { 'vite-plus': '^1.0.0', void: '^0.5.0' } }, + files: { 'index.html': '', 'main.js': '' }, + }); + expect(detectPreset(root)).toEqual({ + buildCommand: 'vp build', + outputDir: toSlash(resolve(root, 'dist/client')), + appType: 'spa', + }); + }); + + it('uses dist/client for a configured static app built with void', () => { + const root = setupProject({ + pkg: { dependencies: { vite: '^6.0.0', void: '^0.5.0' } }, + }); + const config = { inference: { appType: 'spa' } } as VoidConfig; + expect(resolveCloudflareStaticPreset(root, config, {})?.outputDir).toBe( + toSlash(resolve(root, 'dist/client')), + ); + }); + it('detects pre-built dist/index.html as static', () => { const root = setupProject({ files: { 'dist/index.html': '' }, From 74a423435440b409e72e8d8377f0b25ad6da4a1f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 24 Nov 2026 11:11:20 +0800 Subject: [PATCH 2/2] fix(deploy): pick dist/client only when the Vite config uses voidPlugin() A project can install void for its CLI alone and keep Vite's plain dist output. Choosing dist/client from the dependency made those deploys fail with a missing output directory. Read the Vite config and use dist/client only when it imports voidPlugin from void. Co-Authored-By: Claude Opus 5.5 --- packages/void/src/cli/preset.ts | 36 ++++++++++++++++------ packages/void/src/cli/vite-config-patch.ts | 23 ++++++++++++++ packages/void/test/unit/preset.test.ts | 25 ++++++++++++++- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/packages/void/src/cli/preset.ts b/packages/void/src/cli/preset.ts index 29c692a9e..ccb01b3bf 100644 --- a/packages/void/src/cli/preset.ts +++ b/packages/void/src/cli/preset.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'node:fs'; +import { existsSync, readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join, relative, resolve } from 'pathslash'; import { readProjectPaths, type ProjectPaths } from '../project-paths.ts'; @@ -8,6 +8,7 @@ import { readPackageJson, } from '../shared/package-json.ts'; import { getSsrServerEntryPath, hasSsrEntry } from '../ssr/entry.ts'; +import { viteConfigImportsVoidPlugin } from './vite-config-patch.ts'; /** Build output conventions for a known SSG (static site generator). */ export type SsgPreset = { @@ -279,7 +280,7 @@ export function detectPreset( const buildCommand = resolveProjectScriptCommand(root, 'build'); return { buildCommand: buildCommand ?? resolveProjectCommand(root, 'vite build'), - outputDir: resolve(root, viteSpaOutputDir(pkg)), + outputDir: resolve(root, viteSpaOutputDir(root, pkg)), appType: 'spa', }; } @@ -379,16 +380,31 @@ function hasViteLike(pkg: Record): boolean { return hasPackageDependency(pkg, 'vite') || hasPackageDependency(pkg, 'vite-plus'); } +const VITE_CONFIG_FILES = [ + 'vite.config.ts', + 'vite.config.mts', + 'vite.config.js', + 'vite.config.mjs', + 'vite.config.cts', + 'vite.config.cjs', +]; + /** - * Where a Vite SPA build puts its browser files. A project that depends on - * `void` builds with `voidPlugin()` (`void init` adds it), which splits the - * output into `dist/client` (browser files) and `dist/ssr` (the server - * bundle, its source map and `wrangler.json`). Only `dist/client` may be - * uploaded: uploading `dist` publishes the server files and leaves `/` - * without an `index.html`. + * Where a Vite SPA build puts its browser files. A Vite config that uses + * `voidPlugin()` (`void init` adds it) splits the output into `dist/client` + * (browser files) and `dist/ssr` (the server bundle, its source map and + * `wrangler.json`). Only `dist/client` may be uploaded: uploading `dist` + * publishes the server files and leaves `/` without an `index.html`. A + * project that installs `void` only for its CLI keeps Vite's plain `dist`. */ -function viteSpaOutputDir(pkg: Record): string { - return hasPackageDependency(pkg, 'void') ? 'dist/client' : 'dist'; +function viteSpaOutputDir(root: string, pkg: Record): string { + if (!hasPackageDependency(pkg, 'void')) { + return 'dist'; + } + const config = VITE_CONFIG_FILES.map((name) => join(root, name)).find((path) => existsSync(path)); + return config && viteConfigImportsVoidPlugin(config, readFileSync(config, 'utf8')) + ? 'dist/client' + : 'dist'; } function detectViteBinary(root: string): 'vite' | 'vp' { diff --git a/packages/void/src/cli/vite-config-patch.ts b/packages/void/src/cli/vite-config-patch.ts index 1ebb6bfd9..edc82e233 100644 --- a/packages/void/src/cli/vite-config-patch.ts +++ b/packages/void/src/cli/vite-config-patch.ts @@ -77,6 +77,29 @@ function lineIndent(code: string, index: number): string { } /** Add the plugin only when the exported Vite configuration has an editable object and array. */ +/** + * Whether a Vite config imports `voidPlugin` from `void`. Only then does the + * build split into `dist/client` and `dist/ssr`; a project can install `void` + * for its CLI alone and keep Vite's plain `dist` output. + */ +export function viteConfigImportsVoidPlugin(filePath: string, code: string): boolean { + let program: Node; + try { + program = parseSync(filePath, code).program as unknown as Node; + } catch { + return false; + } + return (program.body ?? []).some( + (node) => + node.type === 'ImportDeclaration' && + node.source?.value === 'void' && + (node.specifiers ?? []).some( + (specifier) => + specifier.type === 'ImportSpecifier' && specifier.imported?.name === 'voidPlugin', + ), + ); +} + export function patchViteConfigWithVoidPlugin(filePath: string, code: string): string | undefined { if (filePath.endsWith('.cjs') || filePath.endsWith('.cts')) { return undefined; diff --git a/packages/void/test/unit/preset.test.ts b/packages/void/test/unit/preset.test.ts index 76705dd47..d25be03f5 100644 --- a/packages/void/test/unit/preset.test.ts +++ b/packages/void/test/unit/preset.test.ts @@ -159,7 +159,12 @@ describe('detectPreset', () => { it('uploads only dist/client for a Vite app built with void and no backend', () => { const root = setupProject({ pkg: { devDependencies: { 'vite-plus': '^1.0.0', void: '^0.5.0' } }, - files: { 'index.html': '', 'main.js': '' }, + files: { + 'index.html': '', + 'main.js': '', + 'vite.config.ts': + "import { defineConfig } from 'vite-plus';\nimport { voidPlugin } from 'void';\nexport default defineConfig({ plugins: [voidPlugin()] });\n", + }, }); expect(detectPreset(root)).toEqual({ buildCommand: 'vp build', @@ -168,9 +173,27 @@ describe('detectPreset', () => { }); }); + // `void` installed only for its CLI: the build is plain Vite and writes dist. + it('keeps dist for a Vite app that installs void without voidPlugin()', () => { + for (const config of [ + null, + "import { defineConfig } from 'vite';\nexport default defineConfig({});\n", + ]) { + const root = setupProject({ + pkg: { devDependencies: { vite: '^6.0.0', void: '^0.5.0' } }, + files: { 'index.html': '', ...(config ? { 'vite.config.ts': config } : {}) }, + }); + expect(detectPreset(root)?.outputDir).toBe(toSlash(resolve(root, 'dist'))); + } + }); + it('uses dist/client for a configured static app built with void', () => { const root = setupProject({ pkg: { dependencies: { vite: '^6.0.0', void: '^0.5.0' } }, + files: { + 'vite.config.ts': + "import { voidPlugin } from 'void';\nexport default { plugins: [voidPlugin()] };\n", + }, }); const config = { inference: { appType: 'spa' } } as VoidConfig; expect(resolveCloudflareStaticPreset(root, config, {})?.outputDir).toBe(