> ## 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.

# Rspack configuration in extension.config.js

> Customize Rspack bundling with loaders, plugins, and aliases through extension.config.js without forking the default Extension.js build setup.

Customize bundling behavior without leaving the Extension.js workflow.

Solve custom bundler needs without forking the default setup. Add loaders (file-type transforms), plugins (build-time extensions), and aliases (import shortcuts) directly. Extension.js builds with [Rspack](https://rspack.dev), a Rust-based JavaScript bundler (`@rspack/core`), and you can extend the generated configuration directly.

## How it works

Create one of these files at project root:

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

Use the `config` key to patch the generated bundler configuration.

### `config` capabilities

| Capability | What it does |
| - | - |
| Function hook (`config: (config) => config`) | Gives full control to read and modify generated Rspack config before build. |
| Object merge (`config: { ... }`) | Merges additional config into the generated base config. |
| Rules (`config.module.rules`) | Adds or changes loader rules for file types. |
| Plugins (`config.plugins`) | Adds compile-time plugins and transformations. |
| Resolve (`config.resolve`) | Adds aliases and module resolution behavior. |

### Option 1: function hook (recommended)

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

### Option 2: object merge

`config` can also be an object. Extension.js merges it into the base configuration.

```js theme={null}
export default {
  config: {
    resolve: {
      alias: {
        "@ui": "/absolute/path/to/ui",
      },
    },
  },
};
```

## Rspack-first, webpack-compatible

Extension.js is Rspack-native, but you can still use much of the webpack ecosystem.

* The config type extends `@rspack/core` `Configuration`.
* Many webpack loaders/plugins work through compatibility layers.
* Some webpack internals/plugins are not 1:1 compatible with Rspack.

Example using a Rspack-native plugin. From the release after 4.1.39, the hook's second argument carries `rspack`, the bundler that Extension.js compiles with. Take plugin classes from it, so your project needs no `@rspack/core` of its own:

```js theme={null}
export default {
  config: (config, { rspack }) => {
    config.plugins = [
      ...config.plugins,
      new rspack.DefinePlugin({
        __FEATURE_FLAG__: JSON.stringify("enabled"),
      }),
    ];
    return config;
  },
};
```

On 4.1.39 and earlier, import the class from `@rspack/core`. See [the hook context](https://extension.js.org/docs/features/extension-configuration.md#the-hook-context) for every field of that argument, and [Node APIs in browser extensions](https://extension.js.org/docs/languages-and-frameworks/node.md) for the `resolve.fallback` recipe.

### The bundler name in diagnostics

Build output says Extension.js where the bundler's own text says Rspack, because Extension.js is the tool that you run. From the release after 4.1.39, a name that you type stays as written. That covers text in backticks, the quoted key `"rspack"`, the destructured `{rspack}`, and a member such as `rspack.ProvidePlugin`. You can paste a line such as `new rspack.NormalModuleReplacementPlugin(...)` from a diagnostic as printed.

## Share a module between entries

Extension.js splits HTML pages only. The default `optimization.splitChunks` gives pages two stable siblings: `shared/framework.js` for the UI framework runtime, and `shared/commons.js` for anything two or more pages import. The framework group covers React, Preact, Vue, Svelte and Lit. Neither name carries a hash, so a manifest entry or a public asset can keep pointing at it.

From the release after 4.1.39, a Firefox build ships a shared file past 4 MB as numbered parts, such as `shared/commons-0.js`. See [Firefox builds split large files](https://extension.js.org/docs/commands/build.md#firefox-builds-split-large-files).

Every other surface keeps exactly one file. The manifest names a single script for a background worker, a content script or an injected script, and those surfaces never load a sibling.

That is why sharing a module between a page and a content script still needs `import()`. Rspack emits one chunk for it, and every caller fetches that chunk on demand from the extension origin. A cache group gives the chunk a stable name:

```js theme={null}
export default {
  config: (config) => {
    config.optimization.splitChunks = {
      chunks: "async",
      cacheGroups: {
        shared: {
          name: "shared",
          test: /[\\/]src[\\/]shared[\\/]/,
          minChunks: 1,
          enforce: true,
        },
      },
    };
    return config;
  },
};
```

With this in place, `await import("../shared/big")` from the popup and from the options page both resolve to one emitted `shared.js`. Keep `chunks` at `"async"`. A sync split moves code out of an entry file. The emitted HTML and the manifest reference only that entry file, so the page never loads the split chunk. The service worker cannot use dynamic `import()` and keeps its own copy of the module. A content script can share the chunk too, but it must expose the file through `web_accessible_resources` and import it through `chrome.runtime.getURL`. See [Lazy loading](https://extension.js.org/docs/implementation-guide/lazy-loading.md) for both rules.

## When to use this

* Add custom loaders/rules for project-specific file types.
* Add plugins for compile-time transforms and diagnostics.
* Override resolve aliases and module behavior not exposed by first-class Extension.js options.

## Best practices

* **Prefer first-class options first**: Use `browser` / `commands` configuration keys before low-level bundler overrides.
* **Patch minimally**: Change only the pieces you need, then return the configuration.
* **Keep plugins Rspack-aware**: Prefer Rspack-native plugins when available.
* **Verify on all targets**: Test `dev`, `start`, `preview`, and `build` for your browser matrix after configuration changes.

## Next steps

* Learn more about [extension config (`extension.config.js`)](https://extension.js.org/docs/features/extension-configuration).
* Learn more about [Multi-platform builds](https://extension.js.org/docs/features/multi-platform-builds.md).


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