Configure Vite to build a Chrome extension
Configure Vite to build a Manifest V3 Chrome extension by giving the popup and the background worker their own entries with stable file names.
A Manifest V3 extension needs a few files with fixed names that manifest.json points to. Vite hashes names by default, so turn that off for the entries.
import { resolve } from 'node:path';
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
input: {
popup: resolve(import.meta.dirname, 'popup.html'),
background: resolve(import.meta.dirname, 'src/background.js'),
},
output: {
entryFileNames: '[name].js',
chunkFileNames: 'chunks/[name].js',
},
},
},
});{
"manifest_version": 3,
"name": "Example",
"version": "1.0.0",
"action": { "default_popup": "popup.html" },
"background": { "service_worker": "background.js", "type": "module" }
}Anything in public/ is copied to dist/ as it is, so the manifest lands next to the built files. Entry names stay fixed, while shared chunks are placed in a folder. Load dist through chrome://extensions with "Load unpacked". See Chrome's Manifest V3 overview.
Extensions cannot load remote code, so keep everything bundled and avoid CDN scripts. A plugin such as CRXJS handles the manifest and hot reload if you want more.