import { svelte } from '@sveltejs/vite-plugin-svelte';
import { readdirSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { defineConfig, type Plugin } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
// A relative path, not '@rosea/schema', on purpose. Vite bundles a config's
// relative imports but externalises anything under node_modules, and Node cannot
// load a workspace package's .ts source, so `vite build` and `svelte-check` both
// failed to load this file. The constants module is pure and dependency-free.
import { PRODUCT_NAME, PRODUCT_SHORT_NAME } from '../../packages/schema/src/constants.js';
import { CSP } from './csp.js';
/**
* Two builds of one game (CLAUDE.md §2, *Four versions*; tauri-platforms.md D2).
* - the PWA (default mode): base `/Rosea/`, a service worker, the web manifest, the CSP
* in a tag, into `dist/`;
* - the Tauri shell's copy (`--mode tauri`): base `/`, because Tauri serves the game at the
* root of its own origin; no worker and no manifest, because the files are already on
* the device; no CSP, because Tauri delivers the same policy and a page that
* carries both obeys both; into `dist-tauri/`, so the two can never be mixed up.
*/
type Shell = 'web' | 'tauri';
/**
* GitHub Pages serves a project site at `//`, not `/` (CLAUDE.md §8). Vite
* `base`, the manifest's `scope`/`start_url` and the service worker's scope all
* derive from this one value, so they cannot disagree. The e2e and offline
* suites serve the build under the same prefix.
*/
function baseFor(shell: Shell): string {
const base = process.env['ROSEA_BASE'] ?? (shell === 'tauri' ? '/' : '/Rosea/');
if (!base.startsWith('/') || !base.endsWith('/')) {
throw new Error(`ROSEA_BASE must start and end with '/', got '${base}'`);
}
return base;
}
/**
* The content-security policy (`csp.ts`). GitHub Pages cannot send headers, so the PWA
* carries it in a tag (CLAUDE.md §8). It is injected into the BUILT page only,
* because the dev server injects inline