vite.config.ts

3.6 KB · raw

1import { defineConfig } from "vite";2import solidPlugin from "vite-plugin-solid";3import { crx } from "@crxjs/vite-plugin";4import { copyFileSync, mkdirSync, readFileSync, writeFileSync } 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 pkg = JSON.parse(readFileSync("package.json", "utf-8"));11if (target === "chrome") {12  manifest.web_accessible_resources = [{13    matches: ["<all_urls>"],14    resources: ["<dynamic_resource>"],15  }];16}17const passkeyE2E = process.env.PEACH_E2E_PASSKEYS === "1";18const outDir = target === "firefox" ? "dist-firefox" : "dist-chrome";1920// Copy the pdf.js worker into the build output so the popup can load it via21// chrome.runtime.getURL('pdf.worker.min.mjs'). The pdfjs-dist module's default22// workerSrc points inside node_modules, which is not reachable from a packaged23// extension — the worker file must sit alongside the rest of the assets.24const copyPdfJsWorker = {25  name: "copy-pdfjs-worker",26  closeBundle() {27    mkdirSync(outDir, { recursive: true });28    copyFileSync(29      resolve("node_modules/pdfjs-dist/build/pdf.worker.min.mjs"),30      resolve(outDir, "pdf.worker.min.mjs"),31    );32    if (target === "chrome") {33      const outputManifest = resolve(outDir, "manifest.json");34      const builtManifest = JSON.parse(readFileSync(outputManifest, "utf8"));35      writeFileSync(outputManifest, `${JSON.stringify(builtManifest, null, 2)}\n`);36    }37  },38};3940export default defineConfig({41  plugins: [42    solidPlugin(),43    crx({ manifest, browser: target === "firefox" ? "firefox" : "chrome" }),44    copyPdfJsWorker,45  ],46  base: "./",47  define: {48    __PEACH_E2E_PASSKEYS__: JSON.stringify(passkeyE2E),49    __PEACH_DEBUG__: false,50    __PEACH_VERSION__: JSON.stringify(pkg.version),51  },52  build: {53    outDir,54    // emptyOutDir must stay false. When true, Vite tries to remove the55    // contents of the outDir before each build, which throws EPERM on FUSE56    // and some mounted filesystems and aborts the whole build. Overwriting57    // in place works everywhere.58    emptyOutDir: false,59    // The MV3 service worker has no `document`. Vite's module-preload helper60    // injects <link rel=modulepreload> via document.getElementsByTagName, and61    // it runs synchronously inside every dynamic import() that carries chunk62    // deps. In the worker that throws `ReferenceError: document is not defined`63    // synchronously — escaping the import's own .catch() and tearing down the64    // caller (e.g. passkey-get persisting signCount via fireSyncTrigger).65    // Disabling preload makes the helper a no-op; the link hints are only a66    // page-load micro-optimisation and the popup loads fine without them.67    modulePreload: false,68    target: "esnext",69    rollupOptions: {70      // HV-2: the offscreen document (Chrome only — Firefox has no71      // chrome.offscreen) must be bundled and emitted so the background can72      // load it via chrome.offscreen.createDocument. Firefox capability-checks73      // chrome.offscreen and never references it.74      input:75        target === "firefox"76          ? undefined77          : { offscreen: resolve("src/background/offscreen.html") },78      output: {79        manualChunks(id) {80          if (id.includes("node_modules/solid-js")) return "vendor-solid";81          if (id.includes("node_modules/otpauth")) return "vendor-otp";82          if (id.includes("node_modules/papaparse")) return "vendor-parse";83        },84      },85    },86  },87});