Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 1.4 KB

README.md

File metadata and controls

82 lines (62 loc) · 1.4 KB

@wasm-tool/rust-loader

Webpack plugin for Rust

Installation

yarn add --dev @wasm-tool/rust-loader

Usage

It is highly recommended to use the Rust Webpack Template to create a new Rust project.


But if you instead want to manually create the project yourself, follow these steps:

Create a Cargo.toml file which has the cdylib crate type:

[package]
name = "foo"
version = "0.1.0"
edition = "2018"

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

[dependencies]
wasm-bindgen = "0.2.48"

[dependencies.web-sys]
version = "0.3.25"
features = ["console"]

And create a src/lib.rs file:

use wasm_bindgen::prelude::*;
use web_sys::console;

#[wasm_bindgen(start)]
pub fn main_js() {
    console::log_1(&JsValue::from("Hello world!"));
}

Now add the loader to your webpack.config.js and import the Cargo.toml file:

module.exports = {
  entry: {
    index: "./Cargo.toml"
  },
  module: {
    rules: [{ test: /Cargo\.toml$/, use: "@wasm-tool/rust-loader" }]
  }
};

Then just run webpack as usual.

It also fully supports webpack-dev-server (including live reloading):

module.exports = {
  entry: {
    index: "./Cargo.toml"
  },
  devServer: {
    liveReload: true,
    open: true,
    noInfo: true,
    overlay: true
  },
  module: {
    rules: [{ test: /Cargo\.toml$/, use: "@wasm-tool/rust-loader" }]
  }
};