vite.config.ts

3.1 KB · raw

1import { defineConfig } from "vite";2import solidPlugin from "vite-plugin-solid";3import { crx } from "@crxjs/vite-plugin";4import { copyFileSync, mkdirSync, readFileSync } from "fs";5import { resolve } from "path";67const target = process.env.BUILD_TARGET || "chrome";8const manifestPath = target === "firefox" ? "./manifest.firefox.json" : "./manifest.json";9const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));10const passkeyE2E = process.env.PEACH_E2E_PASSKEYS === "1";11const outDir = target === "firefox" ? "dist-firefox" : "dist-chrome";1213// Copy the pdf.js worker into the build output so the popup can load it via14// chrome.runtime.getURL('pdf.worker.min.mjs'). The pdfjs-dist module's default15// workerSrc points inside node_modules, which is not reachable from a packaged16// extension — the worker file must sit alongside the rest of the assets.17const copyPdfJsWorker = {18  name: "copy-pdfjs-worker",19  closeBundle() {20    mkdirSync(outDir, { recursive: true });21    copyFileSync(22      resolve("node_modules/pdfjs-dist/build/pdf.worker.min.mjs"),23      resolve(outDir, "pdf.worker.min.mjs"),24    );25  },26};2728export default defineConfig({29  plugins: [30    solidPlugin(),31    crx({ manifest, browser: target === "firefox" ? "firefox" : "chrome" }),32    copyPdfJsWorker,33  ],34  base: "./",35  define: {36    __PEACH_E2E_PASSKEYS__: JSON.stringify(passkeyE2E),37    __PEACH_DEBUG__: false,38  },39  build: {40    outDir,41    // emptyOutDir must stay false. When true, Vite tries to remove the42    // contents of the outDir before each build, which throws EPERM on FUSE43    // and some mounted filesystems and aborts the whole build. Overwriting44    // in place works everywhere.45    emptyOutDir: false,46    // The MV3 service worker has no `document`. Vite's module-preload helper47    // injects <link rel=modulepreload> via document.getElementsByTagName, and48    // it runs synchronously inside every dynamic import() that carries chunk49    // deps. In the worker that throws `ReferenceError: document is not defined`50    // synchronously — escaping the import's own .catch() and tearing down the51    // caller (e.g. passkey-get persisting signCount via fireSyncTrigger).52    // Disabling preload makes the helper a no-op; the link hints are only a53    // page-load micro-optimisation and the popup loads fine without them.54    modulePreload: false,55    target: "esnext",56    rollupOptions: {57      // HV-2: the offscreen document (Chrome only — Firefox has no58      // chrome.offscreen) must be bundled and emitted so the background can59      // load it via chrome.offscreen.createDocument. Firefox capability-checks60      // chrome.offscreen and never references it.61      input:62        target === "firefox"63          ? undefined64          : { offscreen: resolve("src/background/offscreen.html") },65      output: {66        manualChunks(id) {67          if (id.includes("node_modules/solid-js")) return "vendor-solid";68          if (id.includes("node_modules/otpauth")) return "vendor-otp";69          if (id.includes("node_modules/papaparse")) return "vendor-parse";70        },71      },72    },73  },74});