import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import { extname, relative, resolve } from "node:path"; import { build } from "esbuild"; import { createHash } from "node:crypto"; import { writeWebLicenseNotices } from "./package-licenses.mjs"; import { meta as filmMeta } from "../site/films/external/films/A/film.js"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { tsImport } from "tsx/esm/api"; import { buildDocumentation } from "../site/docs/build.mjs"; import { buildFilmControls } from "./build-film-controls.mjs"; const source = new URL("../site/", import.meta.url); const output = new URL("../build/site/", import.meta.url); await rm(output, { recursive: true, force: true }); await mkdir(output, { recursive: true }); await cp(source, output, { recursive: true, filter: (path) => path !== fileURLToPath(new URL("docs", source)) && path !== fileURLToPath(new URL("films", source)) && !path.endsWith(".js") && !path.endsWith(".css") && !path.endsWith(".ts") && !path.endsWith(".tsx") && !path.endsWith("tsconfig.json") && !path.endsWith("README.md"), }); await cp(new URL("../LICENSE", import.meta.url), new URL("LICENSE", output)); // App, website and reader guides share the same brand artwork and raster exports. for (const name of ["favicon.svg", "favicon.png", "favicon.ico"]) { await cp(new URL(`../public/${name}`, import.meta.url), new URL(name.endsWith(".ico") ? name : `assets/${name}`, output)); } const [{ locales }, { Glyph }] = await Promise.all([ "../src/client/locales/index.ts", "../src/client/ui/icons.tsx", ].map(path => tsImport(path, { parentURL: import.meta.url, tsconfig: fileURLToPath(new URL("../src/tsconfig.json", import.meta.url)), }))); // Independent locale pools stay inert HTML; the first entry is also the no-JS fallback. const welcomePools = [locales.en, locales.zh].map(locale => locale.playful.welcome.map(({ text }) => renderToStaticMarkup(createElement("span", { lang: locale.tag }, `“${text}”`)))); const homepage = (await readFile(new URL("index.html", source), "utf8")) .replace(/.*?<\/span>/, `${Math.round(filmMeta.duration)}s`) .replace(/

.*?<\/p>/, `

${welcomePools.map(pool => pool[0] ?? "").join("")}

`) // Reuse the product's icon and copy owners without a React runtime on the homepage. .replace(/