Giter Club home page Giter Club logo

rust-wasm-loader's Introduction

Rust WebAssembly loader

npm

Usage

This is a simple Webpack loader that shells out to cargo to build a Rust project targeting WebAssembly. See this post for more details on using Rust to target the web.

To use it, first install the package:

$ npm install rust-wasm-loader

Configure the loader in your Webpack config:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.rs$/,
        use: {
          loader: 'rust-wasm-loader',
          options: {
            // Path to your 'build' or 'dist' directory relative to project root
            path: 'build/',
          }
        }
      },
      // ...
    ]
  }
}

Note: if you are using file-loader, make sure to add .wasm to the test field, otherwise the module will not be copied! (e.g. test: /\.(wasm|jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,).

Make sure you have the cargo, rustc, and (optionally) emsdk binaries somewhere in your PATH. stdweb and other Rust libraries require a nightly build, which can be installed from https://rustup.rs/ .

Require and initialize the wasm module:

const wasm = require('./lib.rs')
wasm.then(module => {
  // Use your module here
  console.log(module.doub(21))
})

or with async/await:

async function loadwasm() {
  const lib = await require('./lib.rs');
  // Use your module here
  console.log(lib.doub(21));
}
loadwasm();

Configuration

The following options can be added to the Webpack loader query:

Name Description Required Default
release Whether or not to pass the --release flag to cargo false false
path Path to your webpack output folder relative to project root true ''
target Allows one to specify wasm32-unknown-emscripten as build target false 'wasm32-unknown-unknown'

Example

Check out the example directory for a simple Hello World example.

This project is based off of rust-emscripten-loader by mrdziuban.

rust-wasm-loader's People

Contributors

bclarke123 avatar germania avatar ianjsikes avatar mrdziuban avatar sorin-davidoi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rust-wasm-loader's Issues

Push latest to npm

Hey @ianjsikes , what's involved to push a 0.2 build to npm? Is that something I can take on? I'm g3rmania on npmjs.com

Rust loader does not copy over the wasm file from the target directory correctly

image

As you can see from the screenshot, rust-wasm-webpack-template.wasm and rust_wasm_webpack_template.wasm have different filenames, but are the same file. For some reason, the filenames change during the webpack build process and it is causing errors because the web assembly module does not know where the web assembly file is and this causes errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.