// Builds the two bundles into custom_components/nextfloor/frontend and checks the size budgets. import { build, context } from "esbuild"; import { createHash } from "node:crypto"; import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "node:fs"; import { validateSymconPackage } from "../symcon/tests/validate-package.mjs"; const out = "../custom_components/nextfloor/frontend"; // IP-Symcon ships the same browser UI through the module's protected WebHook. Its designer and // tile iframe use their own copies so installing the Symcon module never needs Home Assistant files. const symconOut = "../symcon/NextFloor/assets"; const watch = process.argv.includes("--watch"); const common = { bundle: true, format: "esm", // older iPads stay on iOS 15/16: Safari 15 is the oldest with WebGL 2 (three.js needs it); newer syntax such as // static class blocks (Safari 16.4) is turned into older code, otherwise the 3D view fails to load there target: ["safari15", "chrome94", "firefox93", "edge94"], minify: !watch, sourcemap: false, legalComments: "eof", banner: { js: "/*! NextFloor, MIT licence. Includes three.js (MIT) and Lit (BSD-3-Clause); see LICENSE and THIRD_PARTY_NOTICES.md */" }, logLevel: "info", define: { __NF_TREND_HASH__: JSON.stringify("dev"), __NF_HYDRONIC_HASH__: JSON.stringify("dev") }, }; const hashOf = (file) => createHash("sha256").update(readFileSync(file)).digest("hex").slice(0, 12); // Further languages: each lang/.json ships beside the bundles, and the bundles that show texts know // which ones there are, with a hash of each (so a changed file is never taken from the browser cache). const langFiles = readdirSync("lang").filter((f) => /^[a-z]{2}\.json$/.test(f)); const langs = JSON.stringify(Object.fromEntries(langFiles.map((f) => [f.slice(0, 2), hashOf(`lang/${f}`)]))); const viewerConfig = { ...common, entryPoints: ["src/viewer/viewer3d.ts"], outfile: `${out}/nextfloor-3d.js` }; const trendConfig = { ...common, entryPoints: ["src/components/trend-dialog.ts"], outfile: `${out}/nextfloor-trend.js` }; const hydronicConfig = { ...common, entryPoints: ["src/components/hydronic-card.ts"], outfile: `${out}/nextfloor-hydronics.js` }; // the card's visual editor only loads in the dashboard's card dialog const cardEditorConfig = { ...common, entryPoints: ["src/card-editor.ts"], outfile: `${out}/nextfloor-card-editor.js`, define: { __NF_LANGS__: langs, "globalThis.__NF_SHARED_TEXTS__": "true" } }; // the editor is only needed by admins who open it, so it is a bundle of its own as well // (it draws furniture previews with the 3D bundle, so it knows that bundle's hash too) const editorConfig = (viewerHash) => ({ ...common, entryPoints: ["src/components/editor.ts"], outfile: `${out}/nextfloor-editor.js`, define: { ...common.define, __NF_VIEWER_HASH__: JSON.stringify(viewerHash), __NF_LANGS__: langs, "globalThis.__NF_SHARED_TEXTS__": "true" }, }); // The main bundle loads the 3D bundle with a hash of its content in the URL, so a new 3D bundle is // never taken from the browser cache (the integration version only changes after a restart). // the frontend knows its own version, to notice a backend that still runs an older one const version = JSON.parse(readFileSync("../custom_components/nextfloor/manifest.json", "utf8")).version; const symconVersion = JSON.parse(readFileSync("../symcon/library.json", "utf8")).version; const mainConfig = (viewerHash, editorHash, cardEditorHash) => ({ ...common, entryPoints: ["src/main.ts"], outfile: `${out}/nextfloor.js`, define: { ...common.define, __NF_VIEWER_HASH__: JSON.stringify(viewerHash), __NF_EDITOR_HASH__: JSON.stringify(editorHash), __NF_CARD_EDITOR_HASH__: JSON.stringify(cardEditorHash), __NF_VERSION__: JSON.stringify(version), __NF_LANGS__: langs, }, }); const symconViewerConfig = { ...common, entryPoints: ["src/viewer/viewer3d.ts"], outfile: `${symconOut}/nextfloor-3d.js` }; const symconCardEditorConfig = { ...common, entryPoints: ["src/card-editor.ts"], outfile: `${symconOut}/nextfloor-card-editor.js`, define: { __NF_LANGS__: langs, "globalThis.__NF_SHARED_TEXTS__": "true" } }; const symconEditorConfig = (viewerHash) => ({ ...common, entryPoints: ["src/components/editor.ts"], outfile: `${symconOut}/nextfloor-editor.js`, define: { ...common.define, __NF_VIEWER_HASH__: JSON.stringify(viewerHash), __NF_LANGS__: langs, "globalThis.__NF_SHARED_TEXTS__": "true" }, }); const symconHostConfig = (viewerHash, editorHash, cardEditorHash) => ({ ...common, entryPoints: ["src/symcon-host.ts"], outfile: `${symconOut}/nextfloor-designer.js`, define: { ...common.define, __NF_VIEWER_HASH__: JSON.stringify(viewerHash), __NF_EDITOR_HASH__: JSON.stringify(editorHash), __NF_CARD_EDITOR_HASH__: JSON.stringify(cardEditorHash), __NF_VERSION__: JSON.stringify(symconVersion), __NF_LANGS__: langs, }, }); // the furniture builder: a page of its own on the project's site (site/builder), not part of the integration const builderConfig = { ...common, entryPoints: ["src/builder/app.ts"], outfile: "../site/builder/builder.js" }; function copyAssets() { // the integration folder is what HACS ships as a zip, so the licences have to be inside it for (const f of ["LICENSE", "THIRD_PARTY_NOTICES.md"]) copyFileSync(`../${f}`, `../custom_components/nextfloor/${f}`); mkdirSync(`${out}/lang`, { recursive: true }); for (const f of langFiles) copyFileSync(`lang/${f}`, `${out}/lang/${f}`); } function copySymconAssets() { mkdirSync(`${symconOut}/lang`, { recursive: true }); for (const f of langFiles) copyFileSync(`lang/${f}`, `${symconOut}/lang/${f}`); // Packs are data rather than JavaScript, but the existing editor loads them through the same // backend API. Ship a complete local copy so the Symcon module has no HA dependency. const symconPacks = "../symcon/NextFloor/packs"; mkdirSync(symconPacks, { recursive: true }); for (const f of readdirSync("../custom_components/nextfloor/packs").filter((name) => name.endsWith(".json"))) { copyFileSync(`../custom_components/nextfloor/packs/${f}`, `${symconPacks}/${f}`); } for (const f of ["LICENSE", "THIRD_PARTY_NOTICES.md"]) copyFileSync(`../${f}`, `../symcon/NextFloor/${f}`); } // Size budgets, small enough for old wall tablets. The editor bundles only load for admins and the card's settings, so they // may grow more than the view; German and English texts ride in every bundle. // Native utility discovery/reports plus the building/floor overview need about 14 KB in the shared runtime. // Hydronic geometry/readouts load separately; only model references, anchors and the loader join the host. // Metered-device detail discovery adds about 2 KB; keep a small, explicit margin. // Charging telemetry, native time/duration formatting and labels add another 5 KB. // Zoom detail levels and bounded device-card layouts add about 1.5 KB. const BUDGET = { "nextfloor.js": 682 * 1024, "nextfloor-3d.js": 1000 * 1024, "nextfloor-editor.js": 720 * 1024, "nextfloor-card-editor.js": 240 * 1024, "nextfloor-trend.js": 100 * 1024, "nextfloor-hydronics.js": 100 * 1024 }; copyAssets(); copySymconAssets(); if (watch) { // in watch mode the hash is not tracked; a dev reload fetches the bundle anyway for (const c of [ viewerConfig, trendConfig, hydronicConfig, { ...hydronicConfig, outfile: `${symconOut}/nextfloor-hydronics.js` }, { ...trendConfig, outfile: `${symconOut}/nextfloor-trend.js` }, editorConfig("dev"), cardEditorConfig, mainConfig("dev", "dev", "dev"), symconViewerConfig, symconEditorConfig("dev"), symconCardEditorConfig, symconHostConfig("dev", "dev", "dev"), builderConfig, ]) await (await context(c)).watch(); } else { await build(trendConfig); copyFileSync(trendConfig.outfile, `${symconOut}/nextfloor-trend.js`); common.define.__NF_TREND_HASH__ = JSON.stringify(hashOf(trendConfig.outfile)); await build(hydronicConfig); copyFileSync(hydronicConfig.outfile, `${symconOut}/nextfloor-hydronics.js`); common.define.__NF_HYDRONIC_HASH__ = JSON.stringify(hashOf(hydronicConfig.outfile)); await build(viewerConfig); const editor = editorConfig(hashOf(viewerConfig.outfile)); await build(editor); await build(cardEditorConfig); await build(mainConfig(hashOf(viewerConfig.outfile), hashOf(editor.outfile), hashOf(cardEditorConfig.outfile))); await build(symconViewerConfig); const symconEditor = symconEditorConfig(hashOf(symconViewerConfig.outfile)); await build(symconEditor); await build(symconCardEditorConfig); await build(symconHostConfig(hashOf(symconViewerConfig.outfile), hashOf(symconEditor.outfile), hashOf(symconCardEditorConfig.outfile))); validateSymconPackage(); await build(builderConfig); let over = false; for (const [file, limit] of Object.entries(BUDGET)) { const size = statSync(`${out}/${file}`).size; const ok = size <= limit; over ||= !ok; console.log(`${ok ? "ok " : "OVER"} ${file}: ${(size / 1024).toFixed(1)} KB (budget ${limit / 1024} KB)`); } if (over) process.exit(1); }