vite.config.ts

vite.config.ts is a file in Bane's Lab Site. 78 lines of code and 12 definitions.

import { DIAGRAM_WALK_ATTRIBUTE } from "#configuration/constants/diagram.constants";
import { VOCABULARY } from "#core/generated/vocabulary.generated";
import { anatomyRecordsOf } from "#domain/converters/anatomy.reference.converter";
import { codeReferencesOf } from "#core/converters/code.converter";
import { walkAttributeOf } from "#core/analyzers/diagram.analyzer";
import { definitionIndexOf, specifierResolverOf } from "#core/converters/definition.converter";
import { qualifiedPath, treeOf } from "#core/converters/folder.converter";
import { selfGovernedPathOf } from "@banes-lab/build-scripts/core/loaders/manifest.loader.ts";
import { ROOT, absolutePath } from "@ssot/paths";
import { catalogRuleRecord } from "@banes-lab/build-scripts/core/resolvers/rule.resolver.ts";
import { ANATOMY_TREE_DECLARATIONS } from "#core/registries/anatomy.tree.registry";
import { bindingResolverOf } from "@banes-lab/build-scripts/core/resolvers/binding.resolver.ts";
import { codeFilesOf } from "#core/converters/binding.converter";
import { compression } from "vite-plugin-compression2";
import { defineConfig } from "vite";
import { devCertificate } from "@banes-lab/build-scripts/core/factories/certificate.factory.ts";
import { deriveAnatomy } from "@banes-lab/build-scripts/core/coordinators/anatomy.coordinator.ts";
import { devPortPlugin } from "@banes-lab/build-scripts/core/plugins/server.plugin.ts";
import { join } from "node:path";
import { sitePlugin } from "@banes-lab/build-scripts/core/plugins/site.plugin.ts";

const COMPRESSION_THRESHOLD = 256;
const CSP_NONCE_PLACEHOLDER = "87ade4db5f7b6fe6e46ad2134cb9f615";
const BUILD_COMMAND = "build";

const treeRootOf = function treeRootOf(tab: string): string | null {
    const declaration = ANATOMY_TREE_DECLARATIONS.find((held) => held.tab === tab);
    return declaration === undefined ? null : join(ROOT, declaration.folder);
};

const slotDeclaration = function slotDeclaration(path: string): string | null {
    const root = treeRootOf(treeOf(path));
    return root === null ? null : selfGovernedPathOf(root);
};

const buildAnatomy = async function buildAnatomy(): Promise<void> {
    await deriveAnatomy({
        qualifyPath: qualifiedPath,
        record: anatomyRecordsOf,
        refer: (roots) => {
            const index = definitionIndexOf(roots);
            const records = new Set(VOCABULARY.map((entry) => entry.ref));
            const bindings = bindingResolverOf(codeFilesOf(roots, treeRootOf), absolutePath("app.member"));
            const lookups = { bindings, records, ruleRecord: catalogRuleRecord(records), slotDeclaration };
            return (path, text, document) => codeReferencesOf(index, path, text, document, lookups);
        },
        specifiers: specifierResolverOf,
    });
};

export default defineConfig(async ({ command, mode }) => {
    if (command === BUILD_COMMAND) {
        await buildAnatomy();
    }
    return {
        base: "/",
        cacheDir: absolutePath("viteCache.web"),
        build: {
            assetsInlineLimit: 0,
            emptyOutDir: true,
            outDir: absolutePath("builds.web"),
            rollupOptions: {
                input: { main: join(absolutePath("app.member"), "index.html") },
                output: {
                    assetFileNames: "assets/[hash][extname]",
                    chunkFileNames: "assets/[hash].js",
                    entryFileNames: "assets/[hash].js",
                },
            },
            target: "esnext",
        },
        html: { cspNonce: CSP_NONCE_PLACEHOLDER },
        plugins: [
            sitePlugin({ attribute: DIAGRAM_WALK_ATTRIBUTE, orderOf: walkAttributeOf }),
            devPortPlugin("SITE_DEV_PORT"),
            mode === "production" &&
                compression({ algorithms: ["brotliCompress", "gzip"], threshold: COMPRESSION_THRESHOLD }),
        ],
        publicDir: absolutePath("app.public"),
        root: absolutePath("app.member"),
        server: { https: await devCertificate(), strictPort: true },
    };
});