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";192021222324const 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 55 56 57 58 emptyOutDir: false,59 60 61 62 63 64 65 66 67 modulePreload: false,68 target: "esnext",69 rollupOptions: {70 71 72 73 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});