diff --git a/web/package.json b/web/package.json index d1b491b..9345805 100644 --- a/web/package.json +++ b/web/package.json @@ -6,7 +6,7 @@ "scripts": { "dev": "vite dev", "build": "vite build", - "postbuild": "node -e \"require('fs').mkdirSync('build',{recursive:true});require('fs').writeFileSync('build/.gitkeep','')\"", + "postbuild": "node scripts/postbuild.mjs", "preview": "vite preview", "test": "vitest run", "test:watch": "vitest", diff --git a/web/scripts/postbuild.mjs b/web/scripts/postbuild.mjs new file mode 100644 index 0000000..827bcd3 --- /dev/null +++ b/web/scripts/postbuild.mjs @@ -0,0 +1,26 @@ +import { mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; + +mkdirSync('build', { recursive: true }); +writeFileSync('build/.gitkeep', ''); + +// The client bundle must carry Svelte's browser runtime. When the bundler resolves the +// server build instead, onMount is a no-op that gets tree-shaken away, the app stays on +// its loading screen and every test still passes. +const filesIn = (dir) => + readdirSync(dir).flatMap((name) => { + const path = join(dir, name); + return statSync(path).isDirectory() ? filesIn(path) : path; + }); +const scripts = filesIn('build/_app/immutable') + .filter((path) => path.endsWith('.js')) + .map((path) => readFileSync(path, 'utf8')); + +if (scripts.some((code) => code.includes('lifecycle_function_unavailable'))) { + console.error('Build check failed: the client bundle contains Svelte\'s server runtime.'); + process.exit(1); +} +if (!scripts.some((code) => code.includes('serviceWorker'))) { + console.error('Build check failed: the service worker registration is missing from the client bundle.'); + process.exit(1); +}