RezinaLMS-CustomerEnd/tools/artifact-inline.mjs
kirukib 118d26d158 feat: Phase 0 Claude-hosted demo of the e-learning platform
Spec v1.2, Logosmental-based design direction and tokens, pnpm monorepo
(apps/demo, packages/data|features|i18n|db/seed), student + admin screens
in 4 locales, simulated ArifPay checkout on the real order state machine,
artifact inline/check tools, domain rule checks and UI audit fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 23:57:11 +03:00

31 lines
2.3 KiB
JavaScript

// 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(/<link[^>]+rel="preload"[^>]*>/g, ''); // preloads would hit the network
html = html.replace(/<link[^>]+rel="stylesheet"[^>]*href="(\/_next[^"]+)"[^>]*>/g, (_, h) => `<style>${read(h)}</style>`);
html = html.replace(/<script([^>]*?)src="(\/_next[^"]+)"([^>]*)><\/script>/g, (_, a, src, b) => {
const attrs = (a + b).replace(/\s(async|defer|noModule|nomodule)(="")?/g, '');
return `<script${attrs}>${read(src).replace(/<\/script/gi, '<\\/script')}</script>`;
});
// The artifact host only scans the first 8 KB for <title>, 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)`);