@rspack/browser
is a version of Rspack specifically designed for browser environments, without relying on WebContainers or any particular platform. Its API is consistent with the JavaScript API of @rspack/core
, while additionally providing features and interfaces tailored for the browser environment.
Welcome to try running Rspack in the browser at the Rspack Playground.
@rspack/browser
is currently experimental. We will continue to improve its online bundling capabilities, and future releases may introduce breaking changes.
The following example demonstrates the basic usage of @rspack/browser
. Except for the additional APIs used to read and write project files and outputs, other APIs are consistent with the JavaScript API of @rspack/core
.
Note that @rspack/browser
internally uses SharedArrayBuffer to implement shared memory across multiple threads. Therefore, you need to set response headers for both your development server and production deployment environment.
If you are using Rspack as your project's bundler, you can set it through devServer.headers:
If you are using Rsbuild as your project's bundler, you can set it through server.headers:
For production environments, please refer to the documentation of your project's deployment platform.
Since browsers cannot directly access the local file system, @rspack/browser
provides an in-memory file system object builtinMemFs
based on memfs for reading and writing files in the browser environment. All file system reads and writes in both the Node.js and Rust sides are redirected to this in-memory file system, including reading project configuration, source code, node_modules
dependencies, and writing output files.
Here is a basic usage example. For the full API, please refer to the memfs documentation:
builtinMemFs
is a global singleton instance. In scenarios involving concurrent builds of multiple projects, it is recommended to enable experiments.useInputFileSystem to avoid conflicts.
Please note that, currently in the @rspack/browser
, experiments.useInputFileSystem
can only intercept project files that will be ultimately bundled, and cannot intercept files relied upon during the build process, such as those used by Loaders (see also BrowserRequirePlugin#modules below).
To better meet the bundling needs in browser environments, @rspack/browser
offers several dedicated plugins.
In local development, developers usually download project dependencies via package managers and store them in the node_modules
directory at the root of the project. When using @rspack/browser
, you can pre-write dependencies into the node_modules
directory within the in-memory file system. However, when the modules your project depends on are uncertain (e.g., allowing users to freely choose third-party dependencies), pre-writing all dependencies becomes impractical.
@rspack/browser
provides the BrowserHttpImportEsmPlugin
plugin. This plugin rewrites third-party dependency module specifiers to URLs of ESM CDNs during module resolution. For example, import React from "react"
will be rewritten as import React from "https://esm.sh/react"
. Together with Rspack's buildHttp feature, dependencies can be dynamically loaded over HTTP during bundling.
As shown below, BrowserHttpImportEsmPlugin
supports options to specify the ESM CDN domain or to specify particular versions or URLs for certain dependencies.
resolve.alias
When using BrowserHttpImportEsmPlugin
, the rewriting of dependency identifiers occurs before the alias replacements defined in resolve.alias. This ordering can lead to conflicts between the plugin’s rewriting logic and alias resolution.
To resolve this issue, you can configure the plugin’s dependencyUrl
option to preemptively skip requests that should be handled by resolve.alias
:
In Rspack, certain scenarios require dynamically loading and executing JavaScript code, such as Loaders or the template functions of HtmlRspackPlugin. Since this code may come from untrusted users, executing it directly in the browser environment poses potential security risks. To ensure safety, @rspack/browser
throws errors by default in such cases to prevent unsafe code execution.
The BrowserRequirePlugin
plugin provides two ways to address this requirement. Options for BrowserRequirePlugin
are as follows:
modules
This option allows you to directly map a module request id to any JavaScript object within the project. Note that you need to create a corresponding empty file in memfs
:
execute
This option is used to simulate the require
process in Node.js: it resolves modules based on memfs, reads file contents, and executes them. When modules is not provided, or the corresponding result is not found in modules, this path will be attempted.
Rspack does not execute user code during bundling. For security, it is recommended to run the final bundled output inside an iframe.
You need to provide an execute
function to dynamically run and load CommonJS modules and modify runtime.module.exports
to set the module's exports. @rspack/browser
provides an unsafe implementation BrowserRequirePlugin.unsafeExecute
that internally uses new Function
to execute code. You can also implement a safer version based on this API according to your needs, for example:
@rspack/browser
supports Rspack's ModuleFederationPlugin. With this feature, you can pre-bundle complex dependency modules as provider projects deployed to CDN, and then use them in consumer projects that are bundled online in the browser.
When using ModuleFederationPlugin in the browser, please note the following:
@module-federation/webpack-bundler-runtime
(Magic Comments in the output shouldn't be removed).external
and specified versions in BrowserHttpImportEsmPlugin. If you're using esm.sh
as CDN, you can refer to the following code:import: false
, meaning that consumer projects bundled online cannot bundle shared modules themselves; shared modules must be provided by pre-bundled provider projects.