main init
This commit is contained in:
54
Library/web/template/build-web.mjs
Normal file
54
Library/web/template/build-web.mjs
Normal file
@@ -0,0 +1,54 @@
|
||||
// Builds release/{{SLUG}}.html: the whole game in ONE file. Local <script src> and
|
||||
// <link rel="stylesheet"> are inlined, every file under game/assets is embedded as a data: URI
|
||||
// (window.KIT_ASSETS, read by Kit.asset). Works by double-click (file://) and on any host
|
||||
// (itch.io, webspace). From _Tools\Library\web\template.
|
||||
// node build-web.mjs
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const root = path.dirname(fileURLToPath(import.meta.url));
|
||||
const game = path.join(root, 'game');
|
||||
const MIME = {
|
||||
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif',
|
||||
'.svg': 'image/svg+xml', '.mp3': 'audio/mpeg', '.ogg': 'audio/ogg', '.wav': 'audio/wav',
|
||||
'.ttf': 'font/ttf', '.otf': 'font/otf', '.woff2': 'font/woff2', '.json': 'application/json',
|
||||
};
|
||||
|
||||
function walk(dir) {
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
return fs.readdirSync(dir, { withFileTypes: true }).flatMap(e =>
|
||||
e.isDirectory() ? walk(path.join(dir, e.name)) : [path.join(dir, e.name)]);
|
||||
}
|
||||
|
||||
const assets = {};
|
||||
let bytes = 0;
|
||||
for (const file of walk(path.join(game, 'assets'))) {
|
||||
const mime = MIME[path.extname(file).toLowerCase()];
|
||||
if (!mime) continue;
|
||||
const rel = path.relative(game, file).split(path.sep).join('/');
|
||||
const buf = fs.readFileSync(file);
|
||||
bytes += buf.length;
|
||||
assets[rel] = `data:${mime};base64,${buf.toString('base64')}`;
|
||||
}
|
||||
|
||||
let html = fs.readFileSync(path.join(game, 'index.html'), 'utf8');
|
||||
const local = p => !/^(https?:)?\/\//.test(p) && !p.startsWith('data:');
|
||||
// "</script" inside an inlined file would end the tag early
|
||||
const safe = s => s.replace(/<\/script/gi, '<\\/script');
|
||||
|
||||
html = html.replace(/<link\s+rel="stylesheet"\s+href="([^"]+)"\s*\/?>/g, (m, p) =>
|
||||
local(p) ? `<style>\n${fs.readFileSync(path.join(game, p), 'utf8')}\n</style>` : m);
|
||||
html = html.replace(/<script\s+src="([^"]+)"\s*><\/script>/g, (m, p) =>
|
||||
local(p) ? `<script>\n${safe(fs.readFileSync(path.join(game, p), 'utf8'))}\n</script>` : m);
|
||||
// static references in CSS / HTML attributes
|
||||
html = html.replace(/url\((['"]?)(assets\/[^'")]+)\1\)/g, (m, q, p) => (assets[p] ? `url('${assets[p]}')` : m));
|
||||
html = html.replace(/(src|href)="(assets\/[^"]+)"/g, (m, a, p) => (assets[p] ? `${a}="${assets[p]}"` : m));
|
||||
// everything the scripts load at runtime: must come before the first other <script>
|
||||
html = html.replace('<script>', () => `<script>window.KIT_ASSETS = ${JSON.stringify(assets)};</script>\n<script>`);
|
||||
|
||||
const out = path.join(root, 'release', '{{SLUG}}.html');
|
||||
fs.mkdirSync(path.dirname(out), { recursive: true });
|
||||
fs.writeFileSync(out, html);
|
||||
const mb = n => (n / 1024 / 1024).toFixed(1);
|
||||
console.log(`Fertig: ${out} (${mb(fs.statSync(out).size)} MB, ${Object.keys(assets).length} Assets, ${mb(bytes)} MB roh)`);
|
||||
Reference in New Issue
Block a user