Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rust-wasmpack-loader

yeskiy462MIT3.2.0

wasm Webpack/Bun loader for .rs (Rust) resources. Boost your Webpack-powered projects with native WebAssembly support using the 'rust-wasmpack-loader'. This powerful loader enables seamless integration of Rust resources, unlocking high-performance capabil

RUST, RS, Webpack, loader, wasm, WebAssembly, Rust, Rust programming language, Rust WebAssembly, Rust Webpack loader, Rust loader for Webpack, WebAssembly, WebAssembly loader, WebAssembly support, Webpack, Webpack loader, Webpack plugin, Rust resources, Rust integration, Native WebAssembly, High-performance web applications, Optimized Webpack builds, Frontend development, Backend development, Rust ecosystem, rust wasm load file, wasm-loader, rust wasm-pack, rust wasm library, rust wasm example, rustwasm github, rust wasm starter, rust wasm cdylib, rust wasm use cases, rust wasm course, rust load wasm, rust wasm nodejs, rust wasm size, How do you convert Rust to wasm?, What is the wasm-Bindgen feature?, What is Rust WebAssembly?, webpack wasm loader, webpack wasm file-loader, webpack wasm example, webpack loader not working, webpack style loader not working, wasm examples, How do I load Wasm into webpack?, What is a webpack loader?, Is WebAssembly actually faster?, How do I use multiple loaders in webpack?, webpack wasm, asyncwebassembly, webpack 5 wasm, webpack asyncwebassembly, rust wasm webpack, webpack wasm example, error: 'entryoptions.layer' is only allowed when 'experiments.layers' is enabled, webpack assemblyscript, rust webpack, rust webpack alternative, react webpack loader, rust webassembly react, How do I load Wasm into Webpack?

readme

MIT License View this project on NPM View this project on NPM Quality Gate Status Known Vulnerabilities

rust-wasmpack-loader

Native wasm Webpack/Bun loader for .rs (Rust) resources

Works with webpack ^5.0.0

Works fine with web and node (node-async) targets

NEW! Bun support (node target only)

Dynamically finds the Cargo.toml file for building the wasm source. Provides the ability to use both wasm_bindgen and regular functions. Allows you to use the standard import of a .rs file in a .js or .ts file without any headache

Installation

Please be sure that rust is installed on your machine. If not, please install it from the official site

Install rust-wasmpack-loader with npm

`shell script npm i rust-wasmpack-loader


Or install into Dev dependencies

```shell script
  npm i --save-dev rust-wasmpack-loader

Usage

Create Cargo.toml file (in root or in a folder, where you want to create .rs file)

# Cargo.toml

[package]
name = "wasm-fibonacci-test"
version = "0.1.0"
authors = ["Yehor Brodskiy"]

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2.95"

Webpack Configuration

Add .rs rule to webpack config

// webpack.config.js

module.exports = {
    // ...
    module: {
        // ...
        rules: [
            // ...
            {
                test: /\.rs$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "rust-wasmpack-loader",
                    },
                ],
            },
        ]
    }
}

Bun Configuration

Basic Configuration

Add preload.js into bun configuration file

# bunfig.toml
preload = [
    #    ...
    "./node_modules/rust-wasmpack-loader/bun/preload.js"
    #    ...
]

This preload file will load default rust-wasmpack-loader configuration

Advanced Configuration

If you want to override default configuration, you can create your own init.js file

// init.js | init.ts
import { plugin } from "bun";
import loader from "rust-wasmpack-loader";
// ...

plugin(loader.bun({
    // here you can override default configuration
}));

// ...

And then add it to preload

# bunfig.toml
preload = [
    #    ...
    "./path/to/init.js"
    #    ...
]

Webpack Options

parameter type default description
web object | options, which used for web target
node object | options, which used for node target
web.asyncLoading boolean false enables load .wasm file asynchronously, instead of bundling in .js file
web.wasmPathModifier array<string> ["/"] rewrite wasm requestPath, if wrong publicPath used
web.publicPath boolean true use webpack PublicPath
node.bundle boolean false Bundle .wasm file in .js file (additional .wasm file will not create)
logLevel string info Log Level (verbose, info, warn, error, quiet)

Bun Options

Currently, bun supports only node target. Built .wasm file will be bundled in .js file (since bun doesn't support external file watching)

parameter type default description
logLevel string info Log Level (verbose, info, warn, error, quiet)

Examples

Check the example folder for a better understanding of how the loader works

Contributing

Contributions are always welcome!

See CONTRIBUTING.md for ways to get started.

Acknowledgements

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

3.2.0 (2025-03-04)

Features

  • workspace: update dependencies and upgrade eslint config, provide some tests. (#12) (7166d42)

3.1.3 (2024-11-05)

Bug Fixes

  • utils: correct path parsing logic in findNearestCargo.util.js (f8385b7)

3.1.2 (2024-11-05)

Bug Fixes

  • utils: adjust path handling in findNearestCargo.util.js (03974ca)

3.1.1 (2024-10-31)

Bug Fixes

  • dependencies: update wasm-pack to version ^0.13.1 and clean up redundant dependencies in package-lock.json (10b0004)

3.1.0 (2024-10-29)

Features

  • loader: add Bun support and update dependencies (75b287e)

3.0.2 (2024-01-31)

Bug Fixes

3.0.1 (2024-01-31)

Bug Fixes

  • Upgrade dependencies; Update LICENSE year (7a4aa1c)

3.0.0 (2023-11-20)

⚠ BREAKING CHANGES

  • Drop Node.js v16 support. Upgrade dependencies. Upgrade LICENSE year.

Features

  • Drop Node.js v16 support. Upgrade dependencies. Upgrade LICENSE year. (cd95c46)

2.3.6 (2023-07-13)

Bug Fixes

  • 3rd Upgrade package content (6c59c87)

2.3.5 (2023-07-13)

Bug Fixes

  • Another Upgrade package content (39029c2)

2.3.4 (2023-07-13)

Bug Fixes

  • Upgrade package content (44a4771)

2.3.3 (2023-07-13)

Bug Fixes

  • Upgrade dependency versions. Upgrade wasm-pack to 0.12.1 (35df276)

2.3.2 (2023-05-27)

Bug Fixes

  • Upgrade dependency versions. Upgrade wasm-pack (bfc9fdf)

2.3.1 (2023-04-23)

Bug Fixes

  • Upgrade dependency versions (6d01e7e)

2.3.0 (2023-03-26)

Features

  • Update wasm-pack to 0.11.0. Upgrade dependency versions (52e73d0)

2.2.1 (2023-03-14)

Bug Fixes

  • Upgrade dependency versions (a6d8f2b)
  • Upgrade dependency versions (6edb792)

2.2.0 (2023-01-13)

Features

  • Upgrade dependency versions (135ddb6)

2.1.1 (2022-11-30)

Bug Fixes

  • Update path to file, when data.lib set (78ca1aa)

2.1.0 (2022-10-12)

Features

2.0.0 (2022-09-29)

⚠ BREAKING CHANGES

  • Make able to work with web target

Features

  • Make able to work with web target (138d752)

1.1.1 (2022-09-15)

1.0.3 (2022-09-15)

1.0.2 (2022-08-29)

1.0.1 (2022-08-17)

0.0.2 (2022-07-26)