ESM output
Rspack supports building with ESM format output. When building applications, Rspack generates IIFE-based bundled output by default instead of standard CommonJS or ESM format. You can configure output.module to generate ESM format output. The following sections introduce how to build application ESM output and build library ESM output, and finally list the configuration checklist related to ESM output.
Building application ESM output
When building applications, Rspack generates CommonJS-like format output by default. If you need to generate ESM format output, you can configure it in rspack.config.mjs as follows:
You can check out examples of building React applications to ESM output and React ESM SSR in rspack-examples.
Building library ESM output
We recommend using Rslib to build library output. Rslib is built on top of Rspack and can build multiple formats including ES Module, CommonJS, and UMD. Rslib provides higher-level APIs that simplify the library building process. Compared to using Rspack directly, using Rslib for library building is more convenient and efficient.
If you need to use Rspack directly to build ESM format output for libraries (npm library), you need to configure the following:
You can check out this rspack-examples ESM library example.
Building ESM output also requires handling various detailed issues, which are not listed exhaustively here. You can refer to the ESM format related configurations in Rslib, or use Rslib directly for out-of-the-box library building.
Future plans for ESM output
Currently, Rspack's ESM support is still being improved. In certain use cases, you may encounter integration issues between ESM and other features. The main limitations include:
- Lack of static analysis friendly code splitting support
- Limited custom chunk splitting support in ESM format
- Lack of module-level side effect information preservation, affecting tree-shaking accuracy
- Re-exports of external modules (
export * from '...') cannot be properly preserved in ESM format
We are continuously improving Rspack's ESM support to provide a comprehensive solution that addresses existing issues and makes ESM usage more simple and intuitive, better embracing the modern JavaScript module system.
Configuration checklist
Below are the main configuration options related to ESM and their descriptions, which are used when building both applications and libraries.
- output.module: Set to
trueto generate ESM format output. When this option is enabled, it affects the following configurations:- Affects externalsType default value: When
output.moduleistrue,externalsTypedefaults to'module-import' - Affects output.filename default value: When
output.moduleistrue, the default filename is'[name].mjs'instead of'[name].js' - Affects output.chunkFilename default value: When
output.moduleistrue, chunk filename defaults to'[id].mjs'instead of'[id].js' - Affects output.hotUpdateChunkFilename default value: When
output.moduleistrue, defaults to'[id].[fullhash].hot-update.mjs' - Affects output.hotUpdateMainFilename default value: When
output.moduleistrue, defaults to'[runtime].[fullhash].hot-update.json.mjs' - Affects output.iife default value: When
output.moduleistrue,output.iifedefaults tofalse - Affects output.library.type default value: When
output.moduleistrue, defaults to'module'instead of'var' - Affects output.scriptType default value: When
output.moduleistrue, defaults to'module' - Affects output.environment.dynamicImport default value: Enabled when
output.moduleistrue - Affects output.environment.dynamicImportInWorker default value: Enabled when
output.moduleistrue - Affects output.environment.module default value: Enabled when
output.moduleistrue - Affects Node.js related configuration defaults: In Node.js environment, when
output.moduleistrue,__filenameand__dirnamedefault to'node-module'
- Affects externalsType default value: When
- output.chunkFormat: Set to
'module'to use ESM format. - output.library.type: Set to
'modern-module'for additional optimization of library ESM output format. - output.chunkLoading: Set to
'import'to use ESMimportfor loading chunks. - output.workerChunkLoading: Set to
'import'to use ESMimportfor loading workers. - optimization.concatenateModules: modern-module depends on this option being enabled to ensure the output supports good tree-shaking and correct library exports.
- optimization.avoidEntryIife: In some cases, Rspack wraps ESM output in an IIFE, which breaks ESM's modular characteristics.
- experiments.outputModule: Enable the experimental feature required for output.module.
- HtmlWebpackPlugin.scriptLoading: Set to
'module'to use ESM<script type="module">for loading.mjsmodules.

