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 072ea6071..6ccea2f5c 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 c3447e4d2..e5bbcfce7 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 { join, relative, resolve } from 'pathslash'; import { readProjectPaths } from '../project-paths.ts'; import 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, 'dist'), + outputDir: resolve(root, viteSpaOutputDir(root, pkg)), appType: 'spa', }; } @@ -379,6 +380,33 @@ 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 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(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' { const pkg = readPackageJson(root); if (pkg && hasPackageDependency(pkg, 'vite-plus')) { diff --git a/packages/void/src/cli/vite-config-patch.ts b/packages/void/src/cli/vite-config-patch.ts index 48072740d..f922531a2 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 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 eed8135a2..f427361ad 100644 --- a/packages/void/test/unit/preset.test.ts +++ b/packages/void/test/unit/preset.test.ts @@ -153,6 +153,54 @@ 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': '', + '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', + outputDir: toSlash(resolve(root, 'dist/client')), + appType: 'spa', + }); + }); + + // `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( + toSlash(resolve(root, 'dist/client')), + ); + }); + it('detects pre-built dist/index.html as static', () => { const root = setupProject({ files: { 'dist/index.html': '' },