26 lines
1.2 KiB
TypeScript
26 lines
1.2 KiB
TypeScript
import { readFile, writeFile } from 'node:fs/promises';
|
|
import { dirname, join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const dist = resolve(dirname(fileURLToPath(import.meta.url)), '../dist');
|
|
let html = await readFile(join(dist, 'index.html'), 'utf8');
|
|
|
|
const scriptTag = /<script type="module"(?: [^>]*)? src="(\.\/assets\/[^"<>]+\.js)"><\/script>/;
|
|
const scriptPath = html.match(scriptTag)?.[1];
|
|
if (!scriptPath) throw new Error('Expected a Vite JavaScript asset in dist/index.html');
|
|
|
|
const [javascript, icon] = await Promise.all([
|
|
readFile(join(dist, scriptPath)),
|
|
readFile(join(dist, 'HellNet_alt.png')),
|
|
]);
|
|
const js = javascript.toString('utf8');
|
|
if (/<\/script/i.test(js)) throw new Error('Built JavaScript contains an unsafe inline closing tag');
|
|
|
|
html = html.replace(scriptTag, '');
|
|
html = html.replace('href="./HellNet_alt.png"', `href="data:image/png;base64,${icon.toString('base64')}"`);
|
|
html = html.replace('</body>', () => ` <script>${js}</script>\n </body>`);
|
|
if (/src="\.\/assets\/|href="\.\/assets\/|href="\.\/HellNet_alt\.png"/.test(html)) {
|
|
throw new Error('Standalone HTML still references external build assets');
|
|
}
|
|
await writeFile(join(dist, 'hellcon-local.html'), html);
|