Skip to content
jshacks.dev
Tooling

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.

1 min read
Summarize in AI

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.

vite.config.jsJavaScript
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',
      },
    },
  },
});
public/manifest.jsonJSON
{
  "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.

Also inBrowser
Share

More snippets

Tooling

Create a Vite project from a template

Create a Vite project from a template with npm create vite, choose the vanilla-ts or react-ts template and see which files it generates.

1 min read
APIs

Fetch JSON and handle HTTP errors

Fetch JSON in the browser or Node.js 24 and throw on 404 or 500 yourself, because fetch only rejects on network failure and does not check the status.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.