-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.ts
More file actions
59 lines (56 loc) · 1.98 KB
/
vite.config.ts
File metadata and controls
59 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { defineConfig, Plugin } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import { crx } from "@crxjs/vite-plugin";
import manifest from "./src/manifest";
import fs from "node:fs";
import path from "node:path";
/**
* CRXJS beta bug: `service-worker-loader.js` is always written with
* `http://localhost:5173/…` imports, even in production builds.
* This plugin rewrites the file after bundling to import the actual
* built background chunk.
*/
function fixServiceWorkerLoader(): Plugin {
return {
name: "fix-service-worker-loader",
apply: "build",
enforce: "post",
closeBundle() {
const outDir = path.resolve(__dirname, "dist");
const loaderPath = path.join(outDir, "service-worker-loader.js");
const assetsDir = path.join(outDir, "assets");
if (!fs.existsSync(loaderPath) || !fs.existsSync(assetsDir)) return;
// CRXJS names the background bundle after its entry: index.ts-<hash>.js
const bgBundle = fs.readdirSync(assetsDir).find(f => /^index\.ts-.*\.js$/.test(f));
if (!bgBundle) {
console.warn("[fix-service-worker-loader] background bundle not found");
return;
}
fs.writeFileSync(loaderPath, `import './assets/${bgBundle}';\n`);
console.log(`[fix-service-worker-loader] rewrote loader → assets/${bgBundle}`);
}
};
}
export default defineConfig({
publicDir: false,
plugins: [react(), tailwindcss(), crx({
manifest
}), fixServiceWorkerLoader()],
server: {
/* Allow the Chrome extension's service worker (chrome-extension://<id>)
* to fetch @vite/env, HMR scripts, and other dev-server resources.
* Without this, service worker registration fails in dev mode with
* "Status code: 3" and a CORS error. */
cors: {
origin: ["chrome-extension://*", /^chrome-extension:\/\//],
methods: ["GET", "HEAD"]
},
headers: {
"Access-Control-Allow-Origin": "*"
}
},
build: {
outDir: "dist",
}
});