Fix crxjs dev service worker crashes after Vite upgrade.

Downgrade to Vite 6 with crxjs 2.6, add dev-only CSP for HMR, and stop the background script from importing Svelte plugin UI into the service worker.
This commit is contained in:
SethBurkart123
2026-06-16 12:45:56 +10:00
parent 5c195f1148
commit feaf4dced5
5 changed files with 1129 additions and 202 deletions
+39 -5
View File
@@ -4,7 +4,7 @@ import { join, resolve } from "path";
import touchGlobalCSSPlugin from "./lib/touchGlobalCSS";
import InlineWorkerPlugin from "./lib/inlineWorker";
import { base64Loader } from "./lib/base64loader";
import type { BuildTarget } from "./lib/types";
import type { BuildTarget, Manifest } from "./lib/types";
import ClosePlugin from "./lib/closePlugin";
import { firefoxStripFunctionProbe } from "./lib/firefoxStripFunctionProbe";
@@ -22,6 +22,37 @@ import { crx } from "@crxjs/vite-plugin";
const targets: BuildTarget[] = [chrome, brave, edge, firefox, opera, safari];
const DEV_SERVER_PORT = 5173;
/** Vite HMR needs localhost script + ws origins; only applied during `vite dev`. */
function withDevManifestCsp(manifest: Manifest, command: string): Manifest {
if (command !== "serve") return manifest;
const extensionPages = manifest.content_security_policy?.extension_pages;
if (!extensionPages) return manifest;
const localhost = `http://localhost:${DEV_SERVER_PORT}`;
const localhostWs = `ws://localhost:${DEV_SERVER_PORT}`;
const loopback = `http://127.0.0.1:${DEV_SERVER_PORT}`;
const loopbackWs = `ws://127.0.0.1:${DEV_SERVER_PORT}`;
return {
...manifest,
content_security_policy: {
...manifest.content_security_policy,
extension_pages: extensionPages
.replace(
"script-src 'self'",
`script-src 'self' ${localhost} ${loopback}`,
)
.replace(
/connect-src ([^;]+)/,
`connect-src $1 ${localhost} ${localhostWs} ${loopback} ${loopbackWs}`,
),
},
};
}
const mode = process.env.MODE || "chrome"; // Check the environment variable to determine which build type to use.
//const sourcemap = (process.env.SOURCEMAP === "true") || false; // Check whether we want sourcemaps.
/** Million's compiler can emit `new Function()`, which Firefox extension pages block (strict CSP, no unsafe-eval). */
@@ -46,9 +77,11 @@ export default defineConfig(({ command }) => ({
}),
...(useMillion ? [million.vite({ auto: true })] : []),
crx({
manifest:
manifest: withDevManifestCsp(
targets.find((t) => t.browser === mode.toLowerCase())?.manifest ??
chrome.manifest,
chrome.manifest,
command,
),
browser: mode.toLowerCase() === "firefox" ? "firefox" : "chrome",
}),
touchGlobalCSSPlugin(),
@@ -61,11 +94,12 @@ export default defineConfig(({ command }) => ({
},
},
server: {
port: 5173,
port: DEV_SERVER_PORT,
strictPort: true,
hmr: {
host: "localhost",
protocol: "ws",
port: 5173,
port: DEV_SERVER_PORT,
},
},
css: {