🧪 Fail the build when the client bundle carries the server runtime
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.
This commit is contained in:
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
"build": "vite build",
|
"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",
|
"preview": "vite preview",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
"test:watch": "vitest",
|
"test:watch": "vitest",
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user