// Turns a Next.js static export (output: 'export') into ONE self-contained HTML file // that can be published as a Claude artifact. Usage: node tools/artifact-inline.mjs apps/demo/out dist/demo.html import fs from 'fs'; import path from 'path'; const [outDir = 'apps/demo/out', dest = 'dist/demo.html'] = process.argv.slice(2); let html = fs.readFileSync(path.join(outDir, 'index.html'), 'utf8'); const read = (p) => fs.readFileSync(path.join(outDir, p.replace(/^\//, '')), 'utf8'); html = html.replace(/]+rel="preload"[^>]*>/g, ''); // preloads would hit the network html = html.replace(/]+rel="stylesheet"[^>]*href="(\/_next[^"]+)"[^>]*>/g, (_, h) => ``); html = html.replace(/]*?)src="(\/_next[^"]+)"([^>]*)><\/script>/g, (_, a, src, b) => { const attrs = (a + b).replace(/\s(async|defer|noModule|nomodule)(="")?/g, ''); return `${read(src).replace(/<\/script/gi, '<\\/script')}`; }); // The artifact host only scans the first 8 KB for , and wants viewport-fit=cover for safe-area insets. const title = html.match(/<title>[^<]*<\/title>/)?.[0]; if (title) html = html.replace(title, '').replace(/<head>/, `<head>${title}`); html = html.replace(/(<meta name="viewport" content="[^"]*?)"/, (m, a) => (a.includes('viewport-fit') ? m : `${a}, viewport-fit=cover"`)); // The publisher rejects raw U+FFFD as a sign of lost text; in our bundles it only appears inside JS string literals // (URI-decoding fallbacks in dependencies), where the \uFFFD escape means the same thing. html = html.replace(/<script([^>]*)>([\s\S]*?)<\/script>/g, (m, a, body) => (body.includes('\uFFFD') ? `<script${a}>${body.replace(/\uFFFD/g, '\\uFFFD')}</script>` : m)); if (html.includes('\uFFFD')) { console.error('U+FFFD outside scripts — find the lost character'); process.exit(1); } const leftovers = (html.match(/(src|href)="\/_next\/[^"]+"/g) || []); if (leftovers.length) { console.error('Un-inlined Next assets:', leftovers); process.exit(1); } const mb = Buffer.byteLength(html) / 1024 / 1024; if (mb > 15) { console.error(`Too large for an artifact: ${mb.toFixed(1)} MB (limit 16 MB)`); process.exit(1); } fs.mkdirSync(path.dirname(dest), { recursive: true }); fs.writeFileSync(dest, html); console.log(`Wrote ${dest} (${mb.toFixed(2)} MB)`);