From 2c2c7a97c644ecedbe73ca8310e1970db77e6ece Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Wed, 2 Dec 2026 19:05:47 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Fail=20the=20build=20when=20the?= =?UTF-8?q?=20client=20bundle=20carries=20the=20server=20runtime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The postbuild step now recreates the tracked build placeholder and checks that the client bundle holds Svelte's browser runtime and the service worker registration, so a wrong export condition cannot ship unnoticed. --- web/package.json | 2 +- web/scripts/postbuild.mjs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 web/scripts/postbuild.mjs 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); +}