import { readFile, writeFile } from 'node:fs/promises' import path from 'node:path' import { createRequire } from 'node:module' import * as esbuild from 'esbuild' import { TERMINAL_BUNDLED_FONTS, terminalBundledFontCss, terminalNerdFontCss, terminalSymbolsFontCss } from '../src/terminal/fonts/terminal-font-face-css.mjs' const require = createRequire(import.meta.url) const scriptDir = import.meta.dirname const mobileRoot = path.resolve(scriptDir, '..') const outputPath = path.join( mobileRoot, 'src', 'terminal', 'document', 'terminal-webview-engine.generated.ts' ) // Emit stylesheet separately: native WebView needs both strings; page needs CSS without pulling in ~612 KiB engine string forbidden by script-src self. const cssOutputPath = path.join( mobileRoot, 'src', 'terminal', 'document', 'terminal-webview-engine-css.generated.ts' ) // Native-only: the settings preview and native terminal embed just the picked font; the page imports the files as assets. const bundledFontCssOutputPath = path.join( mobileRoot, 'src', 'terminal', 'document', 'terminal-bundled-font-css.generated.ts' ) const target = 'chrome74' const packages = ['@xterm/xterm', '@xterm/addon-unicode11', '@xterm/addon-webgl'] async function readPackageVersion(packageName) { // Use / in package specifier; path.join emits unresolvable backslashes on Windows. const packageJsonPath = require.resolve(`${packageName}/package.json`) const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf8')) return `${packageName}@${packageJson.version}` } function htmlText(value, closingTag) { return value.replace(new RegExp(` { const [latin, latinExt] = await Promise.all([ fontDataUrl(`${file}-Latin.woff2`), fontDataUrl(`${file}-LatinExt.woff2`) ]) return [id, htmlText(terminalBundledFontCss(id, { latin, latinExt }), 'style')] }) ) return Object.fromEntries(entries) } async function main() { const [engineJs, bundledFontCss, rawEngineCss, nerdFont, symbolsFont, ...versions] = await Promise.all([ buildEngineJs(), buildBundledFontCss(), readFile(require.resolve('@xterm/xterm/css/xterm.css'), 'utf8'), readFile(path.join(mobileRoot, 'assets', 'fonts', 'SymbolsNerdFontMono-Regular.woff2')), readFile(path.join(mobileRoot, 'assets', 'fonts', 'NotoSansSymbols2-TerminalSubset.woff2')), ...packages.map(readPackageVersion) ]) // Percent-encode xmlns scheme colons inside data URLs: no fetch occurs, but literal http(s) trips document URL gate; SVG parsing decodes them. const engineCss = rawEngineCss .replace(/\/\*[\s\S]*?\*\//g, '') .replace(/http:\/\/www\.w3\.org\/2000\/svg/g, 'http%3A//www.w3.org/2000/svg') const nerdFontCss = terminalNerdFontCss(`data:font/woff2;base64,${nerdFont.toString('base64')}`) // Inline in both hosts: under 1 KiB, and the page bundle's font allowance admits only the Nerd font file. const symbolsFontCss = terminalSymbolsFontCss( `data:font/woff2;base64,${symbolsFont.toString('base64')}` ) const header = [ '// Generated by scripts/build-terminal-webview-engine.mjs.', `// Packages: ${versions.join(', ')}.`, `// Target: ${target}. Do not edit by hand; regenerate via pnpm postinstall.` ] await Promise.all([ writeFile( outputPath, [ ...header, `export const XTERM_ENGINE_JS = ${JSON.stringify(htmlText(engineJs, 'script'))}`, '' ].join('\n') ), writeFile( cssOutputPath, [ ...header, `export const XTERM_ENGINE_CSS = ${JSON.stringify(htmlText(engineCss, 'style'))}`, `export const TERMINAL_NERD_FONT_CSS = ${JSON.stringify(htmlText(nerdFontCss, 'style'))}`, `export const TERMINAL_SYMBOLS_FONT_CSS = ${JSON.stringify(htmlText(symbolsFontCss, 'style'))}`, '' ].join('\n') ), writeFile( bundledFontCssOutputPath, [ '// Generated by scripts/build-terminal-webview-engine.mjs from mobile/assets/fonts.', '// Do not edit by hand; regenerate via pnpm postinstall.', `export const TERMINAL_BUNDLED_FONT_CSS = ${JSON.stringify(bundledFontCss)} as const`, '' ].join('\n') ) ]) } await main()