import esbuild from "esbuild"; import fs from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const appRoot = path.resolve(__dirname, ".."); const repoRoot = path.resolve(appRoot, "../.."); const entry = path.join(appRoot, "src/terminal/webview/terminal-emulator-webview-entry.ts"); const output = path.join(appRoot, "src/terminal/webview/terminal-emulator-webview-html.ts"); async function resolveTsPath(basePath) { const candidates = [ basePath, `${basePath}.ts`, `${basePath}.tsx`, `${basePath}.js`, `${basePath}.jsx`, path.join(basePath, "index.ts"), path.join(basePath, "index.tsx"), path.join(basePath, "index.js"), path.join(basePath, "index.jsx"), ]; for (const candidate of candidates) { try { const stat = await fs.stat(candidate); if (stat.isFile()) return candidate; } catch { // try next candidate } } return basePath; } const aliasPlugin = { name: "paseo-alias", setup(build) { build.onResolve({ filter: /^@\// }, async (args) => ({ path: await resolveTsPath(path.join(appRoot, "src", args.path.slice(2))), })); build.onResolve({ filter: /^@server\// }, async (args) => ({ path: await resolveTsPath( path.join(repoRoot, "packages/server/src", args.path.slice("@server/".length)), ), })); }, }; const result = await esbuild.build({ entryPoints: [entry], bundle: true, write: false, format: "iife", platform: "browser", target: ["ios15", "chrome100"], loader: { ".css": "text", }, plugins: [aliasPlugin], logLevel: "info", }); const js = result.outputFiles[0]?.text; if (!js) { throw new Error("terminal webview bundle produced no JavaScript"); } const html = `
`; const contents = `// Generated by packages/app/scripts/build-terminal-webview-html.mjs. // Do not edit by hand. export const terminalEmulatorWebViewHtml = ${JSON.stringify(html)}; `; await fs.writeFile(output, contents); console.log(`Wrote ${path.relative(repoRoot, output)} (${html.length} bytes)`);