76 lines
2.9 KiB
JavaScript
76 lines
2.9 KiB
JavaScript
// Builds dist/index.html: one self-contained page (ttyd --index serves a single
|
|||
|
|
// file and nothing else), with CSS, JS, fonts and the favicon all inlined.
|
||
|
|
import { build, transform } from 'esbuild';
|
||
|
|
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
||
|
|
import { dirname, join } from 'node:path';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
|
||
|
|
const root = join(dirname(fileURLToPath(import.meta.url)), '..');
|
||
|
|
const at = (...parts) => join(root, ...parts);
|
||
|
|
const read = (...parts) => readFile(at(...parts), 'utf8');
|
||
|
|
|
||
|
|
// Version: $DEVDESK_VERSION (CI) or the top entry of debian/changelog.
|
||
|
|
async function version() {
|
||
|
|
if (process.env.DEVDESK_VERSION) return process.env.DEVDESK_VERSION;
|
||
|
|
const changelog = await read('debian', 'changelog');
|
||
|
|
return /^devdesk \(([^)]+)\)/.exec(changelog)?.[1] ?? 'dev';
|
||
|
|
}
|
||
|
|
|
||
|
|
// JetBrains Mono (OFL-1.1) from @fontsource: keep the latin + latin-ext faces
|
||
|
|
// of the given weights and inline their woff2 files.
|
||
|
|
async function fontFaces(weights) {
|
||
|
|
const pkg = at('node_modules', '@fontsource', 'jetbrains-mono');
|
||
|
|
const faces = [];
|
||
|
|
for (const weight of weights) {
|
||
|
|
const css = await readFile(join(pkg, `${weight}.css`), 'utf8');
|
||
|
|
for (const block of css.match(/\/\* jetbrains-mono-latin(-ext)?-\d+-normal \*\/\s*@font-face\s*{[^}]*}/g) ?? []) {
|
||
|
|
const file = /url\(\.\/files\/([^)]+\.woff2)\)/.exec(block)[1];
|
||
|
|
const data = (await readFile(join(pkg, 'files', file))).toString('base64');
|
||
|
|
faces.push(block.replace(/src:[^;]*;/, `src: url(data:font/woff2;base64,${data}) format('woff2');`));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!faces.length) throw new Error('no JetBrains Mono faces found');
|
||
|
|
return faces.join('\n');
|
||
|
|
}
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
const ver = await version();
|
||
|
|
|
||
|
|
const js = await build({
|
||
|
|
entryPoints: [at('web', 'src', 'main.js')],
|
||
|
|
bundle: true,
|
||
|
|
minify: true,
|
||
|
|
format: 'iife',
|
||
|
|
target: ['es2020'],
|
||
|
|
write: false,
|
||
|
|
legalComments: 'eof',
|
||
|
|
define: { __DEVDESK_VERSION__: JSON.stringify(ver) },
|
||
|
|
});
|
||
|
|
|
||
|
|
const css = await transform([
|
||
|
|
await read('vendor', 'web_template', 'base.css'),
|
||
|
|
await read('node_modules', '@xterm', 'xterm', 'css', 'xterm.css'),
|
||
|
|
await fontFaces([400, 700]),
|
||
|
|
await read('web', 'styles.css'),
|
||
|
|
].join('\n'), { loader: 'css', minify: true, legalComments: 'eof' });
|
||
|
|
|
||
|
|
const { FAVICON: favicon } = await import('./src/icon.js');
|
||
|
|
|
||
|
|
const script = js.outputFiles[0].text.replace(/<\/script/gi, '<\\/script');
|
||
|
|
const style = css.code.replace(/<\/style/gi, '<\\/style');
|
||
|
|
const template = await read('web', 'index.template.html');
|
||
|
|
const html = template
|
||
|
|
.replace('__FAVICON__', () => favicon)
|
||
|
|
.replace('<!--STYLES-->', () => `<style>${style}</style>`)
|
||
|
|
.replace('<!--SCRIPT-->', () => `<script>${script}</script>`);
|
||
|
|
|
||
|
|
await mkdir(at('dist'), { recursive: true });
|
||
|
|
await writeFile(at('dist', 'index.html'), html);
|
||
|
|
console.log(`dist/index.html ${(html.length / 1024).toFixed(0)} KiB (devdesk ${ver})`);
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch((err) => {
|
||
|
|
console.error(err);
|
||
|
|
process.exit(1);
|
||
|
|
});
|