diff --git a/vite.config.ts b/vite.config.ts index 1bd6811..99a112e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from 'vite'; +import {defineConfig, build} from 'vite'; import path from 'node:path'; import react from '@vitejs/plugin-react'; import process from 'node:process'; @@ -7,6 +7,46 @@ import checker from 'vite-plugin-checker'; import clean from 'vite-plugin-clean'; import WextManifest from 'vite-plugin-wext-manifest'; +import type {Plugin} from 'vite'; + +// Custom plugin to build content scripts as IIFE (self-contained, no ES module imports) +function buildContentScripts(options: { + scripts: {name: string; entry: string}[]; + outDir: string; + isDevelopment: boolean; +}): Plugin { + return { + name: 'build-content-scripts', + async writeBundle() { + for (const script of options.scripts) { + await build({ + configFile: false, + build: { + write: true, + outDir: options.outDir, + emptyOutDir: false, + sourcemap: options.isDevelopment ? 'inline' : false, + minify: !options.isDevelopment, + rollupOptions: { + input: script.entry, + output: { + entryFileNames: `assets/js/${script.name}.bundle.js`, + format: 'iife', + inlineDynamicImports: true, + }, + }, + lib: { + entry: script.entry, + formats: ['iife'], + name: script.name, + }, + }, + }); + } + }, + }; +} + export default defineConfig(({ mode }) => { const isDevelopment = mode !== 'production'; const sourcePath = path.resolve(__dirname, 'source'); @@ -69,6 +109,19 @@ export default defineConfig(({ mode }) => { usePackageJSONVersion: true, }), + // Build content scripts as IIFE (no ES module imports) + // Content scripts can't use ES modules in manifest-injected scripts + buildContentScripts({ + scripts: [ + { + name: 'contentScript', + entry: path.resolve(sourcePath, 'ContentScript/index.ts'), + }, + ], + outDir: getOutDir(), + isDevelopment, + }), + !isDevelopment && zipPack({ inDir: getOutDir(), @@ -93,29 +146,19 @@ export default defineConfig(({ mode }) => { // Vite will find the