> ## Documentation Index
> Fetch the complete documentation index at: https://extension.js.org/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Extension.js needs Node.js 22.12 or newer; Node.js 18 and 20 do not work. It also runs on Deno 2.5+ and Bun 1.2+.
> The default browser target is chromium and a build lands in dist/<browser>, so dist/chromium by default. extension build writes a zip only when --zip is passed.
> Prefix a manifest key with a browser name when browsers differ. chromium: covers every Chromium-family target and Safari builds read it too. Since 4.1.19 chrome: means Chrome alone and edge: means Edge alone.
> Write one background.service_worker in manifest.json instead of hand-writing per-browser background keys; Extension.js converts it for each browser.
> package.json or deno.json marks the project root, not manifest.json. A config file is optional, and only extension.config.js, .mjs and .cjs are loaded, never .ts.
> The bundler is Rspack, extended through the config hook in extension.config.js. A project needs no separate bundler config file.
> Use --no-browser to skip the browser launch; --no-runner no longer exists and --no-open still launches the browser. Use --output for machine output; --format and --wait-format are deprecated.

# Extension config (extension.config.js)

> Set browser defaults, command behavior, and Rspack bundler options in one config file. Applies to dev, start, preview, and build commands.

Use one configuration file to set browser defaults, command behavior, and bundler customization.

Share browser defaults, command options, and build settings across your team. Stop repeating CLI flags. Extension.js reads `extension.config.js` (or `.mjs` / `.cjs`) from your project root. It applies settings to every command and the bundler.

## How it works

Add `extension.config.js` at your project root (same level as `package.json` in typical setups).

Supported file names:

* `extension.config.js`
* `extension.config.mjs`
* `extension.config.cjs`

Top-level keys:

| Key | Description |
| - | - |
| `browser` | Browser-specific defaults keyed by browser target. |
| `commands` | Per-command defaults (`dev`, `start`, `preview`, `build`). |
| `extensions` | Companion extensions applied across commands (load-only). |
| `transpilePackages` | Packages to compile before bundling (useful for monorepo/workspace setups). |
| `perfBudgets` | Per-category asset size budgets in bytes, overridable per browser and per command. |
| `define` | Compile-time constants inlined into every bundle, overridable per browser and per command. |
| `folders` | Where the special folders live, or `false` to turn one off, overridable per browser and per command. |
| `include` | Extra files copied into the output, keyed by output path, overridable per browser and per command. |
| `exclude` | Files the build leaves out, relative to the project root, overridable per browser and per command. |
| `config` | Hook to extend/override the generated Rspack config. Receives the target browser, mode, and command. |
| `configResolved` | Hook that runs once the Rspack config is final, with every loader rule attached. |

### Type-safe configuration

Extension.js exports the `FileConfig` type from the `extension` package so editors can autocomplete and type-check your config. Annotate the export with a JSDoc `@type` tag, which works in `extension.config.js`, `.mjs`, and `.cjs` without a build step:

```js theme={null}
/** @type {import('extension').FileConfig} */
export default {
  browser: {
    chrome: { profile: "path/to/profile" },
  },
};
```

### Environment loading for configuration files

`extension.config.*` runs in Node and should read values from `process.env.*`.

* Extension.js preloads env files before evaluating `extension.config.*`: `.env.defaults`, `.env`, `.env.local`, and `.env.development`, weakest to strongest. Every file that exists loads, and a variable your shell exported always wins.
* It first checks the project folder.
* In monorepos, if Extension.js finds no project-local `.env*` file, it falls back to the nearest workspace root. The workspace root is the folder containing `pnpm-workspace.yaml`.
* Prefer built-in env preload over importing `dotenv` in your configuration file.

### Browser configuration

Need different browser defaults per target? Use `browser`:

```js theme={null}
export default {
  browser: {
    chrome: {
      profile: "path/to/profile",
      preferences: { darkMode: true },
      browserFlags: ["--disable-web-security"],
      excludeBrowserFlags: ["--mute-audio"],
    },
    "chromium-based": {
      chromiumBinary: "/path/to/brave-or-other-chromium-binary",
    },
    firefox: {
      geckoBinary: "/path/to/firefox",
    },
  },
};
```

Supported browser keys include: `chrome`, `edge`, `firefox`, `chromium`, `chromium-based`, `gecko-based`, `firefox-based`.

Common browser fields:

* `profile`, `persistProfile`
* `preferences`
* `browserFlags`, `excludeBrowserFlags`
* `chromiumBinary`, `geckoBinary`
* `extensions` (companion load-only extensions)

#### Browser target capabilities

| Key | What it does |
| - | - |
| `profile` | Sets the browser profile path (or profile behavior) used when launching. |
| `persistProfile` | Reuses profile data between runs. |
| `keepProfileChanges` | Keeps the managed profile and its changes across runs. |
| `copyFromProfile` | Seeds the managed profile as a copy of an existing profile directory. |
| `preferences` | Applies browser preference overrides. |
| `browserFlags` | Adds launch flags for the browser process. |
| `excludeBrowserFlags` | Removes default launch flags you do not want. |
| `chromiumBinary` | Uses a custom Chromium-family binary path. |
| `geckoBinary` | Uses a custom Gecko/Firefox binary path. |
| `extensions` | Loads companion extensions together with the main extension. |
| `transpilePackages` | Packages to compile for this browser. Outranks the top-level list. |
| `perfBudgets` | Asset budgets for this browser. Outranks the top-level budgets. |
| `define` | Constants for this browser. Adds to the top-level map and overrides the keys it repeats. |
| `folders` | Special folder locations for this browser. Replaces the top-level `folders` object. |
| `include` | Extra files for this browser's output. Adds to the top-level `include` key by key. |
| `exclude` | Files this browser's build leaves out, for example `["pages/offscreen.html"]`. Adds to the top-level `exclude`. |

### Commands configuration

Use `commands` to define defaults per command:

```js theme={null}
export default {
  transpilePackages: ["@workspace/ui"],
  commands: {
    dev: {
      browser: "chrome",
      polyfill: true,
      logLevel: "info",
      persistProfile: true,
      browserFlags: ["--headless=new"],
      extensions: { dir: "./extensions" },
    },
    start: {
      browser: "firefox",
    },
    preview: {
      browser: "edge",
      noBrowser: false,
    },
    build: {
      browser: "chrome",
      zip: true,
      zipSource: true,
      zipFilename: "my-extension.zip",
      transpilePackages: ["@workspace/ui", "@workspace/icons"],
    },
  },
};
```

Notes:

* `extensions`, `transpilePackages`, and `perfBudgets` layer from weakest to strongest on every command: top-level, then `browser.<vendor>`, then `commands.<cmd>`, then a CLI flag.
* `define` merges key by key across the same layers: top-level, then `browser.<vendor>`, then `commands.<cmd>` on `dev`, `start`, and `build`.
* `include` merges key by key across the same layers as `define`. A later layer wins an output path both name.
* `exclude` lists add up across the same layers, like `browserFlags`. A file named in any layer is left out of the build.
* `folders` never merges key by key. `browser.<vendor>.folders` replaces the top-level object, and `commands.<cmd>.folders` on `dev`, `start`, and `build` replaces both.
* `commands.build.browser`, `commands.start.browser`, and `commands.preview.browser` pick the target for those commands. A `--browser` flag still wins.
* The `start` command runs `build` then `preview` internally. Extension.js applies settings from `commands.start`, including browser-launch options like `profile`, `browserFlags`, and `startingUrl`. You can also put build-specific settings in `commands.build`.

#### Command capabilities (shared)

| Key | Applies to | What it does |
| - | - | - |
| `browser` | `dev`, `start`, `preview`, `build` | Sets browser or browser-family target. A `--browser` flag wins. |
| `profile` | `dev`, `start`, `preview` | Sets browser profile path/behavior. |
| `persistProfile` | `dev`, `start`, `preview` | Reuses browser profile data between runs. |
| `chromiumBinary` | `dev`, `start`, `preview` | Uses a custom Chromium-family binary. |
| `geckoBinary` | `dev`, `start`, `preview` | Uses a custom Gecko-family binary. |
| `polyfill` | `dev`, `start`, `build` | Enables compatibility polyfill behavior where relevant. |
| `preferences` | `dev`, `start`, `preview` | Applies browser preference overrides. |
| `browserFlags` | `dev`, `start`, `preview` | Adds launch flags for the browser process. |
| `excludeBrowserFlags` | `dev`, `start`, `preview` | Removes default launch flags you do not want. |
| `startingUrl` | `dev`, `start`, `preview` | Opens a specific URL on browser launch. |
| `port` | `dev`, `start`, `preview` | Sets runner/DevTools port. |
| `host` | `dev`, `start`, `preview` | Sets dev server host. Use `0.0.0.0` for Docker/dev containers. |
| `publicHost` | `dev`, `start`, `preview` | Sets the connectable host the browser dials for HMR and the reload bridge. `start` and `preview` accept it for parity and ignore it. |
| `allowedHosts` | `dev` | Extra `Host` names the dev server answers, as an array or a comma string. A leading dot allows every subdomain. |
| `noBrowser` | `dev`, `start`, `preview` | Disables browser launch (`commands.<cmd>.noBrowser`). |
| `extensions` | `dev`, `start`, `preview`, `build` | Loads companion extensions (or extension list). |
| `folders` | `dev`, `start`, `build` | Special folder locations for this command. Replaces the top-level and per-browser `folders`. |
| `include` | `dev`, `start`, `build` | Extra files for this command's output. Adds to the top-level and per-browser `include` key by key. |
| `exclude` | `dev`, `start`, `build` | Files this command's build leaves out. Adds to the top-level and per-browser `exclude` lists. |
| `install` | `dev`, `start`, `build` | Installs missing dependencies before running. |
| `transpilePackages` | `dev`, `start`, `preview`, `build` | Transpiles listed workspace/external packages. |

#### `build` command capabilities

| Key | What it does |
| - | - |
| `zip` | Generates a packaged zip artifact. |
| `zipSource` | Adds source bundle/archive output for review/compliance workflows. |
| `zipFilename` | Names both archives, like `--zip-filename`. The browser is always appended to the distribution archive, and the source archive is `<name>-source.zip` with no browser. |
| `zipDir` | From the release after 4.1.39. Sets the folder for both archives, like `--zip-dir`. Relative to the project root, or absolute. |
| `sourceMaps` | From the release after 4.1.39. Writes a `.map` file beside each script and stylesheet of a production build, like `--source-maps`. The maps stay out of the ZIP file. Defaults to `false`. |
| `silent` | Reduces build log output. |
| `perfBudgets` | Overrides the top-level per-category asset budgets for builds. |

#### `dev` command capabilities

| Key | What it does |
| - | - |
| `noOpen` | Runs dev server without auto-opening browser. |
| `hashContentScripts` | Set `false` to disable content-hashed content-script filenames in dev (on by default). |
| `perfBudgets` | Overrides the top-level per-category asset budgets for dev. |

#### Logging capabilities

| Key | Applies to | What it does |
| - | - | - |
| `logLevel` | `dev`, `start`, `preview` | Sets minimum log level (`off` to `all`). |
| `logContexts` | `dev`, `start`, `preview` | Filters logs by context (`background`, `content`, etc.). |
| `logFormat` | `dev`, `start`, `preview` | Sets output format (`pretty`, `json`, `ndjson`). |
| `logTimestamps` | `dev`, `start`, `preview` | Toggles timestamps on log lines. |
| `logColor` | `dev`, `start`, `preview` | Toggles color in log output. |
| `logUrl` | `dev`, `start`, `preview` | Filters logs by URL pattern. |
| `logTab` | `dev`, `start`, `preview` | Filters logs by tab ID. |

### Compile-time constants

Use `define` to inline constants into every bundle. Extension.js serializes each value as JSON, so strings, numbers, booleans, and plain objects all work:

```js theme={null}
export default {
  define: {
    __APP_VERSION__: "1.4.0",
    __IS_FIREFOX__: false,
  },
  browser: {
    firefox: { define: { __IS_FIREFOX__: true } },
  },
};
```

Your code reads `__APP_VERSION__` as a bare identifier, and the build replaces it with the value. In a TypeScript project, each key also gets an ambient declaration in the generated `extension-env.d.ts`, so `__APP_VERSION__` type-checks as a `string`.

### Special folder locations

Use `folders` to move a [special folder](https://extension.js.org/docs/features/special-folders.md) or to turn one off. Paths resolve from the project root:

```js theme={null}
export default {
  folders: {
    scripts: "src/scripts",
    public: "src/assets",
    pages: false,
  },
};
```

| Key | Default | Accepts |
| - | - | - |
| `scripts` | `scripts/` | A path to a folder named `scripts`, or `false` |
| `pages` | `pages/` | A path to a folder named `pages`, or `false` |
| `public` | `public/` | A path to any folder, or `false` |

* A moved `scripts` or `pages` folder must keep its name. Extension.js does not read a path such as `src/injected`.
* `false` turns the folder off. Extension.js stops compiling its files as entrypoints, and stops copying a `public` folder.
  * A script in a turned-off `scripts` folder that your code names, for example in `chrome.scripting.executeScript({files})`, still ships. Extension.js compiles it like a script in any other folder, without the content script wrapper.
  * Nothing inside a turned-off `public` folder ships, and a root path such as `/icon.png` no longer reaches into it. If the manifest still names a file there, the build fails and lists each file. If a page or a stylesheet references one, the build prints a warning for each reference. Turn the folder back on, or move the files out of `public/`.
* A moved folder behaves exactly like the default one. Scripts in a moved `scripts` folder get the content script wrapper and reload in place during `dev`, pages in a moved `pages` folder build to the same `pages/` output, and a page, a stylesheet, or the manifest reaches a file in a moved `public` folder by the same root path, such as `/logo.png`.
* When `public` names a path, Extension.js copies from that folder only.
* `browser.<vendor>.folders` replaces the top-level object as a whole, and `commands.<cmd>.folders` replaces both. Neither merges key by key.

### Extra files in the build

Use `include` to copy files the bundler does not see into the output. Examples are model weights, a ruleset another tool generates, or a data file that lives outside the project. Each key is the output path, relative to the extension root. Each value is the source path, relative to the project root. A folder source copies its whole tree under the output path:

```js theme={null}
export default {
  include: {
    "models/small.onnx": "./models/small.onnx",
    "models/exported": "../training/export",
  },
};
```

* The file lands at the output path with no hashing. A file that is not a script ships as is, like a file in `public/`.
* From the release after 4.1.39, a production build minifies a `.js`, `.mjs` or `.cjs` file that `include` copies. It shortens names and drops comments, and it rewrites no statement. The value that `chrome.scripting.executeScript` reads from the file is unchanged, and a `/*!` banner stays. A `.min.js` file is left alone.
* In `dev`, an edit to the source lands on the next compile.
* A source that is not there fails the build.
* An output path that is absolute, climbs out of the extension root, or is `manifest.json` fails the build too.
* `include` merges key by key: top-level, then `browser.<vendor>.include`, then `commands.dev|start|build.include`.
* A later layer wins an output path both name, so a build can choose its own file for one path.
* The config file runs in Node, so choose files per environment with plain code:

```js theme={null}
const parity = process.env.PARITY === "1";

export default {
  include: parity
    ? { models: "../training/export" }
    : { "models/small.onnx": "./models/small.onnx" },
};
```

To ship one script exactly as you wrote it, use the object form with `minify: false`:

```js theme={null}
export default {
  include: {
    "vendor/lib.js": { from: "vendor/lib.js", minify: false },
  },
};
```

`extension build --no-minify` does the same for every included script. A file under `public/` always ships byte for byte.

To keep a page or a script out of production builds instead, name it `.dev.`, see [development-only entries](https://extension.js.org/docs/features/special-folders.md#development-only-entries).

### Files one browser leaves out

Use `exclude` to drop a page, a script or a `public/` file from one browser's build. Needs 4.1.39 or later. Each entry is a path relative to the project root. An excluded page or script gets no entry and emits nothing, and an excluded `public/` file is not copied. The typical case is an offscreen document only Chromium opens:

```js theme={null}
export default {
  browser: {
    firefox: {
      exclude: ["pages/offscreen.html"],
    },
  },
};
```

* `exclude` lists add up: top-level, then `browser.<vendor>.exclude`, then `commands.dev|start|build.exclude`.
* A manifest field that still names an excluded file for that browser fails the build with an error naming the file and the browser. Scope the manifest key with a `chromium:`, `gecko:` or `safari:` prefix, or drop the entry.
* An entry that matches no file warns, so a typo does not ship the page silently.

The walkthrough with the dev-mode behavior and the runtime caveats is on the special folders page, see [leaving a file out of one browser](https://extension.js.org/docs/features/special-folders.md#leaving-a-file-out-of-one-browser).

### Rspack configuration

Need advanced bundler customization? Use `config` to patch the generated Rspack configuration. `config.module.rules` is already populated when `config` runs:

```js theme={null}
export default {
  config: (config) => {
    config.module.rules.push({
      test: /\.mdx$/,
      use: ["babel-loader", "@mdx-js/loader"],
    });
    return config;
  },
};
```

`config` may also be an object, which Extension.js merges on top of the generated config.

`config` runs before Extension.js attaches its loader rules. To read or change the final configuration, use `configResolved`. It runs once per `dev`, `build`, or `start` run, right before the first compile, and receives the Rspack configuration with every loader rule attached:

```js theme={null}
export default {
  configResolved: (config) => {
    config.module.rules.push({
      test: /\.graphql$/,
      type: "asset/source",
    });
    return config;
  },
};
```

The hook can change `module`, `resolve`, `resolveLoader`, `node`, `optimization.minimize`, `optimization.minimizer`, and the `output` options that Rspack reads when the build starts, such as file names and `environment`. Change them in place, or return a new configuration object. The hook may be async, and a hook that returns nothing keeps the configuration as it is.

Rspack has already consumed every other key by then. That covers keys such as `entry`, `plugins`, `context`, `mode`, `target`, `devtool`, `externals`, `experiments`, and `performance`, every `optimization` key other than `minimize` and `minimizer`, and these `output` keys: `path`, `module`, `library`, `enabledLibraryTypes`, `chunkFormat`, `chunkLoading`, `enabledChunkLoadingTypes`, `wasmLoading`, `enabledWasmLoadingTypes`, `workerChunkLoading`, `workerWasmLoading`, `workerPublicPath`, `pathinfo`, `sourceMapFilename`, `devtoolModuleFilenameTemplate`, `devtoolFallbackModuleFilenameTemplate`, `devtoolNamespace`, and `bundlerInfo`. Extension.js undoes a change to any of them, including one made deep inside a value, and prints one warning that names each key by its path. Set those in `config`.

#### The hook context

Both hooks receive a second argument that names the run. It carries `browser` (the target, as `--browser` names it), `mode` (`development`, `production`, or `none`), and `command` (`dev`, `build`, `start`, or `preview`). Use it to change the bundler for one browser, without reading `process.argv`. This example keeps the Firefox store build readable for review:

```js theme={null}
export default {
  config: (config, { browser, command }) => {
    if (browser === "firefox" && command === "build") {
      config.optimization.minimize = false;
    }
    return config;
  },
};
```

The `extension` package exports the `ConfigHookContext` type for the argument. A hook that takes one argument keeps working.

From the release after 4.1.39, the context also carries `rspack`, the bundler that this run compiles with. It is the same object that `@rspack/core` exports as `rspack`. Take plugin classes from it and do not import `@rspack/core` in the config file. Your project may not have that package, or may have another version of it:

```js theme={null}
export default {
  config: (config, { rspack }) => {
    config.plugins.push(
      new rspack.ProvidePlugin({ Buffer: ["buffer", "Buffer"] }),
    );
    return config;
  },
};
```

See [Node APIs in browser extensions](https://extension.js.org/docs/languages-and-frameworks/node.md#add-one-fallback-at-a-time) for the full recipe.

#### Keep a set of locales for one browser

A store may accept fewer languages than your `_locales` folder holds. Extension.js copies the whole folder for every browser. To ship a subset for one browser, push a plugin from `config` that deletes the other locale assets:

```js theme={null}
const KEEP = { edge: ["en", "de"] };

export default {
  config: (config, { browser }) => {
    const keep = KEEP[browser];
    if (!keep) return config;
    config.plugins.push({
      apply(compiler) {
        compiler.hooks.thisCompilation.tap("keep-locales", (compilation) => {
          compilation.hooks.processAssets.tap(
            {
              name: "keep-locales",
              stage: compiler.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
            },
            () => {
              for (const name of Object.keys(compilation.assets)) {
                const match = /^_locales\/([^/]+)\//.exec(name);
                if (match && !keep.includes(match[1])) {
                  compilation.deleteAsset(name);
                }
              }
            },
          );
        });
      },
    });
    return config;
  },
};
```

With this file, `extension build --browser=edge` ships `_locales/en` and `_locales/de` only. Every other browser keeps the whole folder. The filter must keep the folder that `default_locale` names. When it removes that folder, the build fails and the error names the locale.

## Full sample

```js theme={null}
export default {
  browser: {
    chrome: { browser: "chrome", profile: "default" },
    firefox: { browser: "firefox", persistProfile: true },
    "chromium-based": {
      browser: "chromium-based",
      chromiumBinary:
        "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
    },
  },
  commands: {
    dev: {
      browser: "chrome",
      polyfill: true,
      host: "0.0.0.0",
      extensions: { dir: "./extensions" },
    },
    start: { browser: "edge" },
    preview: { browser: "firefox" },
    build: {
      zipFilename: "extension.zip",
      zip: true,
      zipSource: true,
    },
  },
  extensions: { dir: "./extensions" },
  transpilePackages: ["@workspace/ui"],
  config: (config) => config,
};
```

## Best practices

* **Keep browser-specific values in `browser`**: Keep command definitions focused on workflow, not browser internals.
* **Use top-level defaults intentionally**: Put shared `extensions` / `transpilePackages` at root; override only where needed.
* **Prefer `chromiumBinary`/`geckoBinary` names**: They align with current command and type surface.
* **Keep `config` hook minimal**: Add only what first-class Extension.js options do not cover.

## Next steps

* Learn more about [Browsers available](https://extension.js.org/docs/browsers/browsers-available.md).
* Learn more about [Rspack configuration](https://extension.js.org/docs/features/rspack-configuration.md).
* Tune launch behavior with [Browser flags](https://extension.js.org/docs/browsers/browser-flags.md) and [Browser preferences](https://extension.js.org/docs/browsers/browser-preferences.md).
* Manage env values with [Environment variables](https://extension.js.org/docs/features/environment-variables.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.