first push
This commit is contained in:
commit
2a81b4a502
845 files changed
+356635
No files matched your search
@@ -0,0 +1,144 @@
|
||||
// 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";
|
||||
|
||||
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",
|
||||
};
|
||||
|
||||
const hashOf = (file) => createHash("sha256").update(readFileSync(file)).digest("hex").slice(0, 12);
|
||||
|
||||
// Further languages: each lang/<code>.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` };
|
||||
// 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: { __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 mainConfig = (viewerHash, editorHash, cardEditorHash) => ({
|
||||
...common,
|
||||
entryPoints: ["src/main.ts"],
|
||||
outfile: `${out}/nextfloor.js`,
|
||||
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: { __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: {
|
||||
__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,
|
||||
},
|
||||
});
|
||||
|
||||
// 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.
|
||||
const BUDGET = { "nextfloor.js": 650 * 1024, "nextfloor-3d.js": 1000 * 1024, "nextfloor-editor.js": 720 * 1024, "nextfloor-card-editor.js": 240 * 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,
|
||||
editorConfig("dev"),
|
||||
cardEditorConfig,
|
||||
mainConfig("dev", "dev", "dev"),
|
||||
symconViewerConfig,
|
||||
symconEditorConfig("dev"),
|
||||
symconCardEditorConfig,
|
||||
symconHostConfig("dev", "dev", "dev"),
|
||||
builderConfig,
|
||||
]) await (await context(c)).watch();
|
||||
} else {
|
||||
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)));
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user