Wasm
Rslib natively supports WebAssembly (WASM) modules, allowing you to directly import and use .wasm files in your project using the ESM import mechanism defined by the WebAssembly ESM Integration proposal.
When importing .wasm modules using WebAssembly ESM Integration, only ESM output is supported, so format must be set to 'esm' (the default).
Use WASM modules
Rslib supports the following ways to use .wasm modules.
Static imports and re-exports
You can use standard ESM syntax to import a .wasm module and access its instantiated exports, or re-export them directly:
Dynamic import
You can use import() to dynamically import a .wasm module and access its instantiated exports:
Source phase import
You can use Source Phase Imports to obtain a compiled WebAssembly.Module and instantiate it manually with a custom import object:
You can also use import.source() to obtain a compiled WebAssembly.Module dynamically:
TypeScript cannot currently parse import source or import.source(). Use these forms in JavaScript files, or choose a toolchain that supports them.
Output modes
You can use lib.wasm.mode to select the output mode for .wasm modules:
- bundle is
true: Onlycompilemode is available. - bundle is
false:preservemode is used by default when the.wasmmodules are resolved and loaded by a downstream build tool that supports WebAssembly ESM Integration (such as Rsbuild or Rspack) or a target runtime with native support (such as Node.js versions matching>=24.5.0). If the consumer does not support this feature, or you do not want to rely on it to handle the.wasmmodules, usecompilemode.
The following source files demonstrate how to configure the compile and preserve modes and their build outputs.
compile mode
Rslib parses each .wasm module, generates the JavaScript glue code required to load and instantiate it, and emits its binary as a static asset to the directory specified by output.distPath.wasm (dist/static/wasm by default), with a content hash in the filename.
Depending on the bundle configuration, Rslib emits the following files in the dist directory:
The generated loading code depends on output.target:
web: Loads.wasmfiles withfetch.node: Loads.wasmfiles with asynchronous Node.js file system APIs.
preserve mode
Rslib keeps imports of .wasm modules in the JavaScript output and emits them unchanged in the dist directory, preserving their source-relative paths and original filenames:
preserve mode retains the original .wasm filenames, so output.filenameHash does not affect them.
Rslib updates imports of .wasm files in the JavaScript output to point to the emitted files, but does not update import module names recorded inside .wasm binaries.
If any of these module names resolve to JavaScript output, avoid changing its relative paths or filenames with these options:
Use with wasm-bindgen
wasm-bindgen is a tool for building WebAssembly libraries with Rust. The --target option generates output for different runtime environments.
Rslib currently supports the following targets:
bundler(recommended): The JavaScript glue imports the.wasmmodule as an ES module while providing the imports required to instantiate it. Bothcompileandpreservemodes are supported. When usingpreservemode, follow the path and filename constraints described above.module: The JavaScript glue uses a Source Phase import to obtain the compiledWebAssembly.Module, then constructs the import object and instantiates the module. Bothcompileandpreservemodes are supported.web/experimental-nodejs-module: The JavaScript glue locates and loads the.wasmfile throughnew URL('./pkg.wasm', import.meta.url). Rslib treats the file as a static asset, so lib.wasm.mode does not apply.
Type declaration
TypeScript does not provide built-in module declarations for .wasm files. If you use TypeScript, add a declaration file next to the .wasm file using the .d.wasm.ts extension, and enable allowArbitraryExtensions in tsconfig.json:
