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/guide/app-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
32 changes: 30 additions & 2 deletions packages/void/src/cli/preset.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = {
Expand Down Expand Up @@ -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',
};
}
Expand Down Expand Up @@ -379,6 +380,33 @@ function hasViteLike(pkg: Record<string, unknown>): 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, unknown>): 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')) {
Expand Down
23 changes: 23 additions & 0 deletions packages/void/src/cli/vite-config-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions packages/void/test/unit/preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<html></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': '<html></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': '<html></html>' },
Expand Down