bimr-studio/vite.config.ts
2026-09-21 15:33:30 +02:00

62 lines
1.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Milovann Yanatchkov
// SPDX-License-Identifier: MIT
import { execSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { defineConfig } from "vite";
import type { Plugin } from "vite";
import react from "@vitejs/plugin-react";
import { handleNode } from "./proxy/node.mjs";
// Build identity, shown at the bottom of Settings. The hash is the current
// commit; the version comes from package.json.
const pkg = JSON.parse(
readFileSync(new URL("./package.json", import.meta.url), "utf8"),
) as { version: string };
const gitHash = (() => {
try {
return execSync("git rev-parse --short HEAD", {
stdio: ["ignore", "pipe", "ignore"],
})
.toString()
.trim();
} catch {
return "dev";
}
})();
// Studio — Vite + React SPA.
// The BIMR MicroPython runtime (public/mp) and samples (public/samples) are
// synced from the @bimr/web package (scripts/sync-bimr.mjs). The IFCX viewer
// bundle (src/bimr/viewer.mjs) is bundled here, resolving `three` from
// node_modules.
//
// Dev-only: mount the portable forge proxy (proxy/proxy.mjs) at /forge-proxy
// so development and production share one implementation. In dev any host is
// allowed unless FORGE_ALLOWED_HOSTS is set.
function forgeProxy(): Plugin {
return {
name: "studio-forge-proxy",
configureServer(server) {
if (!process.env.FORGE_ALLOWED_HOSTS && !process.env.FORGE_ALLOW_ANY) {
process.env.FORGE_ALLOW_ANY = "1";
}
server.middlewares.use((req, res, next) => {
if (!req.url || !req.url.startsWith("/forge-proxy/")) return next();
void handleNode(req, res);
});
},
};
}
export default defineConfig({
base: "./",
plugins: [react(), forgeProxy()],
define: {
"import.meta.env.VITE_APP_VERSION": JSON.stringify(pkg.version),
"import.meta.env.VITE_GIT_HASH": JSON.stringify(gitHash),
},
build: {
target: "esnext",
},
});