47 lines
2.3 KiB
JavaScript
47 lines
2.3 KiB
JavaScript
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
// Vite library mode inlines images regardless of assetsInlineLimit. Always
|
|
// regenerate the final remote AppHeader after Vite using its canonical source,
|
|
// and ship the original BI image as a separate file next to AppHeader.js.
|
|
const root = fileURLToPath(new URL("../", import.meta.url));
|
|
const sourcePath = join(root, "src/remote/ui/AppHeader/AppHeader.remote.jsx");
|
|
const utilityPath = join(root, "src/utils/images.js");
|
|
const originalLogoPath = join(root, "src/assets/bi-logo.webp");
|
|
const outputDir = join(root, "dist/remote");
|
|
const outputJsPath = join(outputDir, "AppHeader.js");
|
|
const outputLogoPath = join(outputDir, "bi-logo.webp");
|
|
|
|
const source = await readFile(sourcePath, "utf8");
|
|
const utility = await readFile(utilityPath, "utf8");
|
|
const utilityImport = 'import { processImage } from "../../../utils/images.js";';
|
|
const utilityExport = "export const processImage =";
|
|
const runtimeLogoPath = 'new URL(DEFAULT_BI_LOGO_FILE, import.meta.url).href';
|
|
|
|
if (source.split(utilityImport).length !== 2 || !utility.includes(utilityExport)) {
|
|
throw new Error("AppHeader or images utility changed: update its remote builder.");
|
|
}
|
|
if (!source.includes(runtimeLogoPath) || !source.includes('const DEFAULT_BI_LOGO_FILE = "bi-logo.webp";')) {
|
|
throw new Error("AppHeader must resolve the original BI logo beside its remote module.");
|
|
}
|
|
if (/\bimport\s+(?!\()/m.test(source.replace(utilityImport, ""))) {
|
|
throw new Error("AppHeader gained an additional import: update the remote builder.");
|
|
}
|
|
|
|
const js = `${utility.replace(utilityExport, "const processImage =").trim()}\n\n${source.replace(utilityImport, "").trim()}\n`;
|
|
if (/data:image\/[^;]+;base64/i.test(js)) {
|
|
throw new Error("AppHeader output unexpectedly contains an embedded image.");
|
|
}
|
|
|
|
await mkdir(outputDir, { recursive: true });
|
|
await copyFile(originalLogoPath, outputLogoPath);
|
|
await writeFile(outputJsPath, js, "utf8");
|
|
|
|
const original = await readFile(originalLogoPath);
|
|
const shipped = await readFile(outputLogoPath);
|
|
if (!original.equals(shipped)) {
|
|
throw new Error("Remote BI logo differs from the canonical source asset.");
|
|
}
|
|
console.log(`AppHeader Remote ready: ${Buffer.byteLength(js)} JS bytes, ${shipped.length} original logo bytes`);
|