Skip to content

Build your personal knowledge base with Trilium Notes

23.1 KB
Raw Blame
import fs from "fs";
import { join, resolve, sep } from "path";

import { codecovVitePlugin } from "@codecov/vite-plugin";
import prefresh from "@prefresh/vite";
import { defaultClientConditions, defineConfig, type Plugin } from "vite";
import { viteStaticCopy } from "vite-plugin-static-copy";

import {
    narrowThirdPartySelectors, shareTheme, stripUniverEmojiData, stripUniverHyphenation
} from "../client/vite-plugins.mjs";

const clientAssets = ["assets", "stylesheets", "fonts", "translations"];

const isDev = process.env.NODE_ENV === "development";
const FULL_COVERAGE = { lines: 100, functions: 100, branches: 100, statements: 100 };

// The share pages resolve built-in assets against `assets/v<version>`, the same prefix the server
// serves them under. Read from trilium-core because that is the version `assetUrlFragment` is
// built from; `chore:update-version` keeps every package.json in step.
const coreVersion = JSON.parse(
    fs.readFileSync(join(__dirname, "../../packages/trilium-core/package.json"), "utf-8")
).version;

// Watch client files and trigger reload in development
const clientWatchPlugin = () => ({
    name: "client-watch",
    configureServer(server: any) {
        if (isDev) {
            // Watch client source files (adjusted for new root)
            server.watcher.add("../../client/src/**/*");
            server.watcher.on("change", (file: string) => {
                if (file.includes("../../client/src/")) {
                    server.ws.send({
                        type: "full-reload"
                    });
                }
            });
        }
    }
});

// Serve PDF.js files directly in dev mode to bypass SPA fallback
const pdfjsServePlugin = (): Plugin => ({
    name: "pdfjs-serve",
    configureServer(server) {
        const pdfjsRoot = join(__dirname, "../../packages/pdfjs-viewer/dist");

        server.middlewares.use((req, res, next) => {
            if (!req.url?.startsWith("/pdfjs/")) {
                return next();
            }

            // Map /pdfjs/web/... to dist/web/...
            // Map /pdfjs/build/... to dist/build/...
            // Strip query string (e.g., ?v=0.102.2) before resolving path
            const urlWithoutQuery = req.url.split("?")[0];
            const relativePath = urlWithoutQuery.replace(/^\/pdfjs\//, "");
            const filePath = join(pdfjsRoot, relativePath);

            // Security: resolve both paths to prevent prefix-collision attacks
            // (e.g. pdfjsRoot="/foo/bar" matching "/foo/bar2/evil.js")
            const resolvedRoot = resolve(pdfjsRoot);
            const resolvedFilePath = resolve(filePath);
            if (!resolvedFilePath.startsWith(resolvedRoot + sep)) {
                return next();
            }

            if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
                const ext = filePath.split(".").pop() || "";
                const mimeTypes: Record<string, string> = {
                    html: "text/html",
                    css: "text/css",
                    js: "application/javascript",
                    mjs: "application/javascript",
                    wasm: "application/wasm",
                    png: "image/png",
                    svg: "image/svg+xml",
                    json: "application/json"
                };
                res.setHeader("Content-Type", mimeTypes[ext] || "application/octet-stream");
                res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
                fs.createReadStream(filePath).pipe(res);
            } else {
                next();
            }
        });
    }
});

// Remove the hashed sqlite3.wasm duplicate from the bundle.
// Vite detects `new URL("sqlite3.wasm", import.meta.url)` in the sqlite-wasm library and
// emits a hashed copy (e.g. sqlite3-B5ovX4lD.wasm). Since the service worker needs the
// unhashed filename anyway (provided by viteStaticCopy), we drop the hashed duplicate.
const sqliteWasmDedupePlugin = (): Plugin => ({
    name: "sqlite-wasm-dedupe",
    generateBundle(_options, bundle) {
        for (const fileName of Object.keys(bundle)) {
            const asset = bundle[fileName];
            if (asset.type === "asset" && /sqlite3-\w+\.wasm$/.test(fileName)) {
                delete bundle[fileName];
            }
        }
    }
});

// Copy SQLite WASM files so they're available to the service worker at runtime.
// The .wasm file must be copied with its original name because the sqlite-wasm library
// resolves it by convention inside the worker (where Vite's hashed import.meta.url won't work).
const sqliteWasmPlugin = viteStaticCopy({
    targets: [
        {
            src: "../../../node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm",
            dest: "assets",
            rename: { stripBase: true }
        },
        {
            src: "../../../node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js",
            dest: "assets",
            rename: { stripBase: true }
        }
    ]
});

/** Source of the in-app help's images and attachments, and the URL the app requests them under. */
const HELP_ASSETS_SOURCE = join(__dirname, "../../docs/User Guide");
const HELP_ASSETS_DEST = join(__dirname, "dist/server-assets/help");
const HELP_ASSETS_URL = "/server-assets/help/";

const HELP_ASSET_MIME_TYPES: Record<string, string> = {
    png: "image/png",
    gif: "image/gif",
    webp: "image/webp",
    jpg: "image/jpeg",
    jpeg: "image/jpeg",
    svg: "image/svg+xml",
    json: "application/json"
};

/** The guide's own files, as opposed to the content bundle that arrives with the server's assets. */
function isHelpAsset(sourcePath: string) {
    return !sourcePath.endsWith(".md") && !sourcePath.endsWith("!!!meta.json");
}

/**
 * Serves the guide's images and attachments beside the content bundle that comes with the
 * server's assets. They belong to the User Guide rather than to the server, so no other copy step
 * brings them in, and the markdown they were built from is not needed at runtime.
 *
 * Both halves are needed: a build copies them into the output, and the dev server reads them
 * straight from the guide — without that, requests fall through to the SPA fallback and every
 * image on a help page comes back as index.html with a 200.
 */
function copyHelpAssetsPlugin(): Plugin {
    return {
        name: "trilium-copy-help-assets",
        configureServer(server) {
            server.middlewares.use((req, res, next) => {
                if (!req.url?.startsWith(HELP_ASSETS_URL)) {
                    return next();
                }

                const relativePath = decodeURIComponent(req.url.slice(HELP_ASSETS_URL.length).split("?")[0]);
                const filePath = join(HELP_ASSETS_SOURCE, relativePath);

                // Same prefix-collision guard as the PDF.js middleware above.
                if (!resolve(filePath).startsWith(resolve(HELP_ASSETS_SOURCE) + sep) || !isHelpAsset(filePath)) {
                    return next();
                }

                // Anything not found here — the content bundle and the tree — is copied in from
                // the server's assets, so it belongs to the next middleware.
                if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
                    return next();
                }

                const extension = filePath.split(".").pop()?.toLowerCase() ?? "";
                res.setHeader("Content-Type", HELP_ASSET_MIME_TYPES[extension] ?? "application/octet-stream");
                fs.createReadStream(filePath).pipe(res);
            });
        },
        closeBundle() {
            if (isDev) {
                return;
            }

            fs.cpSync(HELP_ASSETS_SOURCE, HELP_ASSETS_DEST, {
                recursive: true,
                dereference: true,
                filter: isHelpAsset
            });
        }
    };
}

let plugins: any = [
    stripUniverHyphenation(),
    stripUniverEmojiData(),
    narrowThirdPartySelectors(),
    sqliteWasmDedupePlugin(),
    sqliteWasmPlugin,
    viteStaticCopy({
        targets: clientAssets.map((asset) => ({
            src: `../../client/src/${asset}/**/*`,
            dest: asset,
            rename: { stripBase: 3 }
        })),
        // Enable watching in development
        ...(isDev && {
            watch: {
                reloadPageOnChange: true
            }
        })
    }),
    viteStaticCopy({
        targets: [
            {
                src: [
                    "../../server/src/assets/**/*"
                ],
                dest: "server-assets",
                rename: { stripBase: 3 }
            }
        ]
    }),
    copyHelpAssetsPlugin(),
    // What the share theme's own pages load besides its bundle: the icon-pack fonts the client ships
    // and the logo, each at the path content_renderer.ts writes into the page. The server answers
    // these from express.static routes it registers in routes/assets.ts; here they are copied into
    // the build instead.
    viteStaticCopy({
        targets: [
            {
                src: "../../client/src/fonts/**/*",
                dest: "share/assets/fonts",
                rename: { stripBase: 3 }
            },
            {
                src: "../../server/src/assets/images/**/*",
                dest: `assets/v${coreVersion}/images`,
                rename: { stripBase: 4 }
            }
        ]
    }),
    // PDF.js viewer for PDF preview support
    // stripBase: 4 removes packages/pdfjs-viewer/dist/web (or /build)
    viteStaticCopy({
        targets: [
            {
                src: "../../../packages/pdfjs-viewer/dist/web/**/*",
                dest: "pdfjs/web",
                rename: { stripBase: 4 }
            },
            {
                src: "../../../packages/pdfjs-viewer/dist/build/**/*",
                dest: "pdfjs/build",
                rename: { stripBase: 4 }
            }
        ]
    }),
    // Watch client files for changes in development
    ...(isDev ? [
        prefresh(),
        clientWatchPlugin(),
        pdfjsServePlugin()
    ] : []),
    ...shareTheme({ stubDir: "share/assets" })
];

if (!isDev) {
    plugins = [
        ...plugins,
        viteStaticCopy({
            targets: [
                {
                    src: "../../../node_modules/@excalidraw/excalidraw/dist/prod/fonts/**/*",
                    dest: "",
                }
            ]
        })
    ]
}

// Include the integration test fixture database for e2e tests
if (process.env.TRILIUM_INTEGRATION_TEST) {
    plugins = [
        ...plugins,
        viteStaticCopy({
            targets: [
                {
                    // Forward slashes are required because fast-glob (used
                    // internally) treats backslashes as escape characters on
                    // Windows. `stripBase` drops the source's directory
                    // structure so the file lands flat at `test-fixtures/document.db`
                    // rather than mirroring the `packages/trilium-core/...` path.
                    src: join(__dirname, "../../packages/trilium-core/src/test/fixtures/document.db").replace(/\\/g, "/"),
                    dest: "test-fixtures",
                    rename: { stripBase: true }
                }
            ]
        })
    ]
}

if (!isDev) {
    // Put the Codecov vite plugin after all other plugins.
    // Gated on CODECOV_TOKEN so it stays a no-op locally and in the
    // integration-test build (which sets no token).
    plugins = [
        ...plugins,
        codecovVitePlugin({
            enableBundleAnalysis: !!process.env.CODECOV_TOKEN,
            bundleName: "standalone",
            uploadToken: process.env.CODECOV_TOKEN
        })
    ];
}

export default defineConfig(() => ({
    root: join(__dirname, 'src'),  // Set src as root so index.html is served from /
    envDir: __dirname,  // Load .env files from standalone directory, not src/
    cacheDir: '../../../node_modules/.vite/apps/standalone',
    base: "",
    plugins,
    // Use oxc for JSX transformation (Vite 8+ replaced the deprecated `esbuild` option with `oxc`)
    oxc: {
        jsx: {
            runtime: 'automatic',
            importSource: 'preact',
            development: isDev
        }
    },
    css: {
        transformer: 'lightningcss',
        devSourcemap: isDev
    },
    publicDir: join(__dirname, 'public'),
    resolve: {
        alias: [
            {
                find: "react",
                replacement: "preact/compat"
            },
            {
                find: "react-dom",
                replacement: "preact/compat"
            },
            {
                find: "@client",
                replacement: join(__dirname, "../client/src")
            },
            // Bypass officeparser's `browser` entry — a prebuilt ~2.7 MB monolith that
            // inlines pdfjs-dist for its PDF-parsing feature. The wrapper bundles the
            // package's Node ESM entry instead (plus the Buffer polyfill it needs),
            // keeping only the office-format parsers and the HTML generator (~0.4 MB).
            // Guarded against upstream layout changes by office_preview.spec.ts.
            {
                find: /^officeparser$/,
                replacement: join(__dirname, "src/stubs/officeparser_entry.ts")
            },
            // Heavy optional officeparser dependencies, only reachable via dynamic import
            // on code paths office-format conversion never executes (PDF parsing, OCR,
            // PDF generation via puppeteer). Without the stubs, Vite would either emit
            // their multi-megabyte chunks or fail dev import-analysis on unresolvable
            // specifiers.
            {
                find: /^pdfjs-dist(\/.*)?$/,
                replacement: join(__dirname, "src/stubs/empty.ts")
            },
            {
                find: /^tesseract\.js$/,
                replacement: join(__dirname, "src/stubs/empty.ts")
            },
            {
                find: /^puppeteer$/,
                replacement: join(__dirname, "src/stubs/empty.ts")
            },
            // Turnish's `browser` entry parses HTML with the page's `DOMParser` or `document`, but the
            // Web Worker that runs core has neither. Its Node entry falls back to domino, a DOM written
            // in plain JavaScript that loads no Node built-ins.
            {
                find: /^turnish$/,
                replacement: join(__dirname, "../../node_modules/turnish/dist/index.node.mjs")
            },
            // EJS renders the share pages. Its ESM entry imports `node:fs` and `node:path` for the
            // file-loading it only reaches through its own `include()` without an `includer`; the
            // share renderer resolves includes itself or passes an `includer`, so the package's own
            // browser build serves it and resolves in this bundle.
            {
                find: /^ejs$/,
                replacement: join(__dirname, "../../node_modules/ejs/ejs.min.js")
            }
        ],
        dedupe: [
            "react",
            "react-dom",
            "preact",
            "preact/compat",
            "preact/hooks"
        ]
    },
    server: {
        watch: {
            // Watch workspace packages
            ignored: ['!**/node_modules/@triliumnext/**'],
            // Also watch client assets for live reload
            usePolling: false,
            interval: 100,
            binaryInterval: 300
        },
        // Watch additional directories for changes
        fs: {
            allow: [
                // Allow access to workspace root
                '../../../',
                // Explicitly allow client directory
                '../../client/src/'
            ]
        },
        headers: {
            // COOP is kept for security (prevents window.opener attacks).
            // COEP is intentionally omitted: SAHPool (our primary SQLite VFS) does not
            // require SharedArrayBuffer/COEP, and omitting it allows cross-origin iframes
            // (e.g. in-app help pointing to docs.triliumnotes.org).
            "Cross-Origin-Opener-Policy": "same-origin"
        }
    },
    preview: {
        headers: {
            "Cross-Origin-Opener-Policy": "same-origin"
        }
    },
    optimizeDeps: {
        exclude: ['@sqlite.org/sqlite-wasm', '@triliumnext/core'],
        // Dynamically imported from the excluded @triliumnext/core, so the dep scanner
        // never discovers it on its own. Pre-bundling matters beyond warm-up here: the
        // officeparser alias resolves to CJS internals that only the dep optimizer
        // converts to ESM in dev — without it, the dev server serves raw CJS and the
        // import fails in the browser.
        include: ['officeparser']
    },
    worker: {
        format: "es" as const
    },
    commonjsOptions: {
        transformMixedEsModules: true,
    },
    build: {
        target: "esnext",
        outDir: join(__dirname, 'dist'),
        emptyOutDir: true,
        rollupOptions: {
            input: {
                main: join(__dirname, 'src', 'index.html'),
                sw: join(__dirname, 'src', 'sw.ts'),
                'local-bridge': join(__dirname, 'src', 'local-bridge.ts'),
            },
            output: {
                // Everything below `src/` carries a content hash so a deployment switches over
                // atomically: `index.html` is served uncached and can only ever reference chunks
                // from its own build. Without the hash, a browser holding a still-fresh copy of
                // one chunk mixes it with newly fetched ones — and since the minifier reassigns
                // single-letter export names every build, a cross-build import silently binds to
                // the wrong value ("X is not a function") until the cache expires.
                entryFileNames: (chunkInfo) => {
                    // The service worker must keep a stable URL: `main.ts` registers it as
                    // `./sw.js`, and a hash would orphan the previously registered worker.
                    if (chunkInfo.name === "sw") {
                        return "[name].js";
                    }
                    return "src/[name]-[hash].js";
                },
                chunkFileNames: "src/[name]-[hash].js",
                assetFileNames: "src/[name]-[hash].[ext]"
            }
        }
    },
    test: {
        environment: "happy-dom",
        setupFiles: [join(__dirname, "src/test_setup.ts")],
        dir: join(__dirname),
        reporters: [
            "default",
            // Absolute path on purpose: the Vite `root` above is `src`, so a
            // relative outputFile would land in `src/test-output`. Anchor to the
            // package dir to match the other apps and the CI upload path.
            ["junit", { outputFile: join(__dirname, "test-output/vitest/junit.xml"), addFileAttribute: true }]
        ],
        projects: [
            {
                extends: true,
                test: {
                    name: "standalone",
                    include: ["src/**/*.{test,spec}.{ts,tsx}"]
                }
            },
            // Core runs in a dedicated Web Worker, so its specs run in a scope shaped like one, with
            // dependencies resolved as the worker bundle resolves them. `ssr.resolve.conditions`
            // reaches the packages Node loads itself, through `--conditions`.
            {
                extends: true,
                resolve: { conditions: defaultClientConditions },
                ssr: { resolve: { conditions: ["browser", "development|production"] } },
                test: {
                    name: "core",
                    include: ["../../packages/trilium-core/src/**/*.{test,spec}.{ts,tsx}"],
                    // The same timeouts as the server runner, which runs these specs too.
                    hookTimeout: 20_000,
                    testTimeout: 40_000,
                    environment: join(__dirname, "src/test_environment_worker.ts"),
                    alias: {
                        // The specs make their test images with Jimp, whose `browser` entry is an empty stub.
                        jimp: join(__dirname, "../../node_modules/jimp/dist/esm/index.js")
                    }
                }
            }
        ],
        coverage: {
            // Absolute path for the same reason as the junit reporter above: the
            // Vite `root` is `src`, so a relative path would land in
            // `src/test-output`. Anchor to the package dir (matches the CI upload path).
            reportsDirectory: join(__dirname, "test-output/vitest/coverage"),
            provider: "v8" as const,
            // trilium-core lives outside this project's `root` (which is `src`),
            // so its files are only collected when `allowExternal` is enabled.
            allowExternal: true,
            // Vite `root` above is `src`, so coverage include globs resolve relative to src/.
            // `../../../` walks src -> standalone -> apps -> repo root to reach the core package.
            include: ["**/*.{ts,tsx}", "../../../packages/trilium-core/src/**/*.{ts,tsx}"],
            exclude: [
                "**/*.{test,spec}.{ts,mts,cts,tsx,js,jsx}",
                "**/*.d.ts",
                // Build/E2E config, not unit-testable.
                "**/playwright.config.ts",
                // Test harness, not product code.
                "**/test_setup.ts",
                // Thin re-export entry (forwards the client desktop entry).
                "**/desktop.ts",
                // The web-worker entry boots @triliumnext/core (and a second SQLite
                // WASM init) on load, which conflicts with the unit-test harness's own
                // core initialization; it is exercised by the Playwright e2e suite instead.
                "**/local-server-worker.ts"
            ],
            // Codecov resolves an lcov `SF:` path by matching it against the repo's file list.
            // Vitest defaults the lcov reporter's `projectRoot` to the Vite `root` — here `src`,
            // so paths would emit as bare `main.ts` / `../../../packages/trilium-core/src/…`,
            // which are ambiguous in this monorepo and get attributed to whichever project wins
            // the match. Anchor to the repo root so every path is unambiguous.
            reporter: ["text", "html", ["lcov", { projectRoot: join(__dirname, "../..") }]],
            // Threshold globs match paths relative to the root, `src`.
            thresholds: {
                "../../../packages/trilium-core/src/share/**": FULL_COVERAGE,
                "lightweight/share_provider.ts": FULL_COVERAGE
            }
        },
        server: {
            deps: {
                inline: ["@sqlite.org/sqlite-wasm"]
            }
        },
        alias: {
            // The package's `node.mjs` entry references a non-existent
            // `sqlite3-node.mjs`. Force the browser-style entry which works
            // under Node + happy-dom too.
            "@sqlite.org/sqlite-wasm": join(
                __dirname,
                "../../node_modules/@sqlite.org/sqlite-wasm/index.mjs"
            )
        }
    },
    define: {
        "process.env.IS_PREACT": JSON.stringify("true"),
        __TRILIUM_INTEGRATION_TEST__: JSON.stringify(process.env.TRILIUM_INTEGRATION_TEST ?? ""),
    }
}));