wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated WebAssembly that you would like to interop with JavaScript. This includes ability to publish modules so that you can share your Rust generated WebAssembly code with others.

In this lesson we create a npm package and export a Rust function that will log to the console in the browser. We publish the module to NPM and afterwards use a rust-webpack-template to install and use the newly published package.

Create a new wasm lib:

cargo new my-wasm-lib --lib

Update Cargo.toml:

[package]
name = "my-wasm-lib"
version = "0.1.0"
authors = []
edition = "2018" [lib]
crate-type = ["cdylib"] [dependencies]
wasm-bindgen = "0.2"

lib.rs:

extern crate wasm_bindgen;

use wasm_bindgen::prelude:;*;

#[wasm_bindgen]
extern {
#[wasm_bindgen(js_namespace = console)]
fn log(msg: &str);
} #[wasm_bindgen]
pub fn greet(name: &str) {
log(&format!("Hello {}!", name));
}

Run:

wasm-pack build

This creates a package directory. In previous lessons, I mentioned that it contains a WASM and a Javascript file. What I haven't mentioned yet is that it also creates a package.json based on our Cargo.toml.

Publish:

wasm-pack publish

Then inside another wasm project:

Install:

npm install --save my-wasm-lib1215  # package name

Open index.js:

import("my-wasm-lib1215").then(module => {
module.greet("World!@");
})

Run:

npm start

In the broswer console, you can see the msg:

Hello World!@!

[WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack的更多相关文章

  1. Quickstart: Create and publish a package using Visual Studio (.NET Framework, Windows)

    https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-n ...

  2. [NPM + React] Prepare a Custom React Hook to be Published as an npm Package

    Before we publish our package, we want to make sure everything is set up correctly. We’ll cover vers ...

  3. How to Publish a NuGet Package

    How to Publish a NuGet Package Command line To push packages to nuget.org you must use nuget.exe v4. ...

  4. How to using PyPI publish a Python package

    How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...

  5. npm & package.json & directories & files

    npm & package.json & directories & files package.json https://docs.npmjs.com/files/packa ...

  6. [Node.js] Configuring npm package.json scripts

    With a node package manager's (npm) package.json script property, you can preconfigure common tasks ...

  7. Angular Npm Package.Json文件详解

    Angular7 Npm Package.Json文件详解   近期时间比较充裕,正好想了解下Angular Project相关内容.于是将Npm官网上关于Package.json的官方说明文档进行了 ...

  8. Node.js NPM Package.json

    章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...

  9. how to publish a dart package using Github Actions?

    how to publish a dart package using Github Actions? dart package flutter package Github Actions publ ...

随机推荐

  1. I2C驱动框架(五)

    参考:I2C子系统之 adapter driver注册——I2C_dev_init() i2c的操作在内核中是当做字符设备来操作的,相关初始化在由i2c_dev_init函数来初始化. static ...

  2. DocView mode 3 -- 配置

    ;在当前页中滚动doc-view-continuous nil ;指定默认的字体大小doc-view-resolution ;gs生成的缓存的目录doc-view-cache-directory

  3. Java-从一个字符串获取子字符串

    substring函数 package com.tj; public class MyClass implements Cloneable { public static void main(Stri ...

  4. CI - Set CSRF Hash and Cookie

    /** * Set CSRF Hash and Cookie * * @return string */ protected function _csrf_set_hash() { if ($this ...

  5. NYOJ 747 蚂蚁的难题(三)

    蚂蚁的难题(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:4   描述 蚂蚁终于把尽可能多的食材都搬回家了,现在开始了大厨计划. 已知一共有 n 件食材,每件食材有一个美味 ...

  6. [转载] Laya性能优化精选内容整理

    第一是性能统计工具,这是LayaAir引擎内置的性能统计工具,在代码加入Laya.Stat.show(); 引擎内置的性能统计工具 打开这个工具后,可以用于观察性能,除了FPS越高越好外,其它的值越低 ...

  7. 2014 ACM/ICPC Asia Regional 北京 Online

    G - Grade Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushroo ...

  8. 在LoadRunner向远程Linux/Unix执行命令行并收集性能数据

    前面介绍过在LoadRunner的Java协议实现“使用SSH连接Linux”,当然连接之后的故事由你主导. 今天要讲的,是一个非Java版本.是对“在LoadRunner中执行命令行程序之:pope ...

  9. 【Luogu】P3567Kur-Couriers(主席树)

    题目链接 数组大小开到一千二百万才过- - 可以把数先离散化再全都加到主席树中. 对于一个区间[from,to] 取中间点mid 看看小于mid的数有多少个,如果个数的两倍<=to-from+1 ...

  10. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...