3.2.0 • Published 3 months ago

rust-wasmpack-loader v3.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

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

  npm i rust-wasmpack-loader

Or install into Dev dependencies

  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

parametertypedefaultdescription
webobjectoptions, which used for web target
nodeobjectoptions, which used for node target
web.asyncLoadingbooleanfalseenables load .wasm file asynchronously, instead of bundling in .js file
web.wasmPathModifierarray<string>["/"]rewrite wasm requestPath, if wrong publicPath used
web.publicPathbooleantrueuse webpack PublicPath
node.bundlebooleanfalseBundle .wasm file in .js file (additional .wasm file will not create)
logLevelstringinfoLog 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)

parametertypedefaultdescription
logLevelstringinfoLog 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

3.1.3

7 months ago

3.1.2

7 months ago

3.2.0

3 months ago

3.1.1

8 months ago

3.1.0

8 months ago

3.0.2

1 year ago

3.0.0

2 years ago

2.3.2

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.6

2 years ago

2.3.5

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.3.1

2 years ago

2.2.0

2 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.1.1

3 years ago

1.0.2

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago