import type { Plugin } from "vite"; // The design lab (`src/design-lab/`) is a dev-only page: main.tsx reaches it behind // `import.meta.env.DEV`, which Vite folds to `false` in a production build, so the dynamic import // is dead code and the chunk is never emitted. That rule used to be a sentence; this makes it a // build failure. If a future edit imports the lab unguarded (a static import, a shared helper, a // re-export), the production build refuses instead of shipping the lab to every visitor. const DEV_ONLY_DIR = "/src/design-lab/"; export function devOnlyGuard(): Plugin { return { name: "siftlode-dev-only-guard", apply: "build", generateBundle(_options, bundle) { for (const [fileName, output] of Object.entries(bundle)) { if (output.type !== "chunk") continue; const leaked = Object.keys(output.modules).find((id) => id.replaceAll("\\", "/").includes(DEV_ONLY_DIR), ); if (leaked) { this.error( `dev-only module ${leaked} reached the production bundle (${fileName}). ` + "Import src/design-lab only behind `import.meta.env.DEV`.", ); } } }, }; }