diff --git a/packages/void/src/cli/init.ts b/packages/void/src/cli/init.ts index 877c0113c..fa7ee1bfc 100644 --- a/packages/void/src/cli/init.ts +++ b/packages/void/src/cli/init.ts @@ -61,6 +61,7 @@ import { SCAFFOLD_FRAMEWORKS, SCAFFOLD_FRAMEWORK_OPTIONS, SCAFFOLD_GITIGNORE_LINES, + SCAFFOLD_SVELTE_CONFIG, SCAFFOLD_TOOLCHAIN_OPTIONS, } from './template.ts'; import type { ScaffoldFramework, ScaffoldStarterMode, ScaffoldToolchain } from './template.ts'; @@ -582,6 +583,15 @@ export function isScaffoldableEmptyProject(root: string): boolean { if (entry.name === 'pnpm-workspace.yaml' && isEmptyProjectAllowedPnpmWorkspaceFile(root)) { continue; } + if (entry.name === SCAFFOLD_SVELTE_CONFIG.filename && entry.isFile()) { + try { + if (readFileSync(join(root, entry.name), 'utf8') === SCAFFOLD_SVELTE_CONFIG.content) { + continue; + } + } catch { + return false; + } + } if (entry.name.startsWith('README')) { continue; } diff --git a/packages/void/src/cli/template.ts b/packages/void/src/cli/template.ts index 5a6f95cae..8d5c1973f 100644 --- a/packages/void/src/cli/template.ts +++ b/packages/void/src/cli/template.ts @@ -19,6 +19,11 @@ export const SCAFFOLD_GITIGNORE_LINES = [ '.DS_Store', ] as const; +export const SCAFFOLD_SVELTE_CONFIG = { + filename: 'svelte.config.mjs', + content: 'export default {};\n', +} as const; + export type ScaffoldFramework = 'react' | 'vue' | 'svelte' | 'solid'; export type ScaffoldStarterMode = 'sqlite' | 'postgresql' | 'mysql' | 'static'; export type ScaffoldToolchain = 'vite-plus' | 'vite'; @@ -221,6 +226,10 @@ export function createPagesStarterTemplate(options: { 'public/icons.svg': readFileSync(join(assets, 'icons.svg'), 'utf8'), }; + if (framework === 'svelte') { + files[SCAFFOLD_SVELTE_CONFIG.filename] = SCAFFOLD_SVELTE_CONFIG.content; + } + if (starterMode !== 'static') { files['pages/index.server.ts'] = getIndexServer(); files['routes/api/hello.ts'] = getHelloRoute(config.label, starterMode); diff --git a/packages/void/test/unit/init.test.ts b/packages/void/test/unit/init.test.ts index f05daa0d8..543d210a1 100644 --- a/packages/void/test/unit/init.test.ts +++ b/packages/void/test/unit/init.test.ts @@ -2256,7 +2256,7 @@ describe('isScaffoldableEmptyProject', () => { }); }); - it('replaces stale vp starter scripts when plain Vite is selected', async () => { + it('replaces Vite+ starter scripts when plain Vite is selected', async () => { const root = mkdtempSync(join(tmpdir(), 'void-init-')); try { writeFileSync( @@ -2291,6 +2291,7 @@ describe('isScaffoldableEmptyProject', () => { expect(pkg.devDependencies.vite).toBe('^8.0.10'); expect(pkg.devDependencies['vite-plus']).toBeUndefined(); expect(pkg.devDependencies['@cloudflare/oxlint-config']).toBeUndefined(); + expect(readFileSync(join(root, 'svelte.config.mjs'), 'utf8')).toBe('export default {};\n'); const viteConfig = readFileSync(join(root, 'vite.config.ts'), 'utf8'); expect(viteConfig).toContain('from "vite"'); expect(viteConfig).not.toContain('lint:'); @@ -2300,6 +2301,52 @@ describe('isScaffoldableEmptyProject', () => { } }); + it('re-scaffolds a Svelte workspace starter when its generated config remains', async () => { + await withTempDirAsync(async (workspace) => { + writeFileSync(join(workspace, 'pnpm-workspace.yaml'), "packages:\n - 'apps/*'\n"); + const root = join(workspace, 'apps', 'site'); + mkdirSync(root, { recursive: true }); + writeFileSync(join(root, 'package.json'), JSON.stringify({ name: 'site', type: 'commonjs' })); + const args = parseInitArgs([ + '--toolchain', + 'vite-plus', + '--framework', + 'svelte', + '--starter', + 'static', + '--deploy', + 'none', + ]); + await runInitCommand(root, args); + + for (const entry of [ + 'pages', + 'public', + 'vite.config.mts', + 'void.config.ts', + '.void-wrangler.jsonc', + ]) { + rmSync(join(root, entry), { recursive: true, force: true }); + } + expect(isScaffoldableEmptyProject(root)).toBe(true); + await runInitCommand(root, args); + + expect(existsSync(join(root, 'pages', 'index.svelte'))).toBe(true); + expect(readFileSync(join(root, 'vite.config.mts'), 'utf8')).toContain('@void/svelte/plugin'); + }); + }); + + it('returns false when the Svelte config has user content', () => { + withTempDir((root) => { + writeFileSync( + join(root, 'svelte.config.mjs'), + 'export default { compilerOptions: { dev: true } };\n', + ); + + expect(isScaffoldableEmptyProject(root)).toBe(false); + }); + }); + it('returns false when app files already exist', () => { withTempDir((root) => { mkdirSync(join(root, 'src'), { recursive: true });