For AI agents: the complete documentation index is available at https://rspack.rs/llms.txt, the full documentation bundle is available at https://rspack.rs/llms-full.txt, and this page is available as Markdown at https://rspack.rs/guide/features/esm.md.
close

ESM output

Rspack can generate ES modules (ESM) for applications and libraries. In a browser application, the page can load the entry with <script type="module">. When you build an ESM library, other projects can import its functions or components by package name.

Build a browser application

By default, Rspack builds a browser application as a classic script, typically wrapping the bundle in an immediately invoked function expression (IIFE). To generate CommonJS or ESM output, select the corresponding format in output.

If your application targets browsers that support ES modules and dynamic import(), enable output.module. When you use HtmlRspackPlugin, the plugin includes the emitted entry file in the generated HTML and sets type="module" on its script tag.

rspack.config.mjs
import { rspack } from '@rspack/core';

export default {
  target: ['web', 'es2020'],
  output: {
    module: true,
  },
  plugins: [new rspack.HtmlRspackPlugin()],
};

With this configuration, Rspack emits async chunks as ES modules. The browser loads them on demand using native import().

See the React hot module replacement example and the React server-side rendering example for complete configurations.

Build an ESM library

For JavaScript libraries and UI component libraries, we recommend Rslib. Built on Rsbuild, it provides out-of-the-box support for building, debugging, and testing your library. Generating multiple output formats and TypeScript declarations takes less configuration with Rslib.

If you need to build an ESM library with Rspack directly, set output.library.type to 'modern-module'. The built file retains the ESM exports from your entry, so downstream bundlers can use tree shaking to remove unused code.

To let users write import { formatDate } from 'my-lib', export formatDate from the library's entry file.

src/index.js
export function formatDate(date) {
  return date.toISOString().slice(0, 10);
}

Name the entry index and set output.filename to '[name].js'. Rspack emits the ES module as dist/index.js.

rspack.config.js
export default {
  entry: { index: './src/index.js' },
  output: {
    filename: '[name].js',
    library: { type: 'modern-module' },
  },
};

Set "type": "module" in package.json so Node.js loads the package’s .js files as ES modules. Point exports to the built file so users can import formatDate from my-lib.

package.json
{
  "name": "my-lib",
  "type": "module",
  "exports": "./dist/index.js"
}

If you want to emit each component in its own file and keep your source directory structure, configure output.library.preserveModules. For a complete project, see the Rspack ESM library example.

Optional configuration

  • ESM entry files use the .mjs extension by default. To customize filenames, configure output.filename for entry files and output.chunkFilename for async chunks.
  • Rspack may wrap the entry module in an IIFE to avoid variable name conflicts within a chunk. With optimization.avoidEntryIife enabled, Rspack tries to avoid that wrapper by renaming variables.
  • If your application loads chunks inside a Web Worker, you can set its loading mechanism separately with output.workerChunkLoading.
  • Serve module files with a JavaScript MIME type such as text/javascript. If you host them on a CDN with a different origin from the page, configure the CDN to allow the page to fetch them using CORS.
  • If your component library should use the application's copy of React, exclude react from the library bundle with externals. externalsType determines how the generated code references it.
  • To turn off minification so users can read or debug the library output, set optimization.minimize to false.