/** * Move SvelteKit's inline bootstrap script to an external file for CSP (no unsafe-inline). * Run after vite build; reads/writes build/. * Finds containing __sveltekit_, minifies it, writes to _app/immutable/entry/bootstrap.js, * and replaces the inline script with that contains __sveltekit_ function findInlineBootstrap(html) { const scriptOpen = html.indexOf('', scriptOpen); if (scriptClose === -1) return null; const content = html.slice(scriptOpen + ''.length }; } const SCRIPT_TAG = ``; async function main() { const htmlFiles = getFiles(buildDir, '.html'); let bootstrapWritten = false; let count = 0; for (const htmlFile of htmlFiles) { let html = readFileSync(htmlFile, 'utf8'); const found = findInlineBootstrap(html); if (!found) continue; if (!bootstrapWritten) { // Imports relative to script location when in _app/immutable/entry/ let scriptContent = found.content.replace( /import\("\.\/_app\/immutable\/entry\/([^"]+)"\)/g, 'import("./$1")' ); const result = await minify(scriptContent, { format: { comments: false }, compress: { passes: 1 } }); if (result.code) scriptContent = result.code; mkdirSync(entryDir, { recursive: true }); writeFileSync(bootstrapPath, scriptContent, 'utf8'); bootstrapWritten = true; } html = html.slice(0, found.start) + SCRIPT_TAG + html.slice(found.end); writeFileSync(htmlFile, html, 'utf8'); count++; } if (count > 0) { console.log('Bootstrap script externalized (minified):', scriptSrc, `(${count} HTML file(s))`); } else if (htmlFiles.length > 0) { console.log('No SvelteKit inline script found in HTML (bootstrap already external?)'); } } main().catch((err) => { console.error(err); process.exit(1); });