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. 前端开发中提到的“脚手架”到底指什么,CLI?gulp 和 gulp-cli有什么区别

    一般来说,脚手架是帮你减少「为减少重复性工作而做的重复性工作」的工具. gulp和gulp-cli的区别可以看这个task - what does gulp-"cli" stand ...

  2. CSS3-文本-word-wrap,word-break,white-space

    一.word-wrap使用: 语法: word-wrap : normal | break-word 取值说明: 1.normal为默认值,当其值为normal控制连续文本换行(允许内容顶开容器的边界 ...

  3. js中小数精度问题

    js中小数的取值为近似值,可能比实际值大,也可能比实际值小,进行“四舍五入”得到的 例如:alert(0.1+0.2);值为0.300000004     alert(0.2+0.7);值为1.899 ...

  4. Ubuntu18.04 无法解析域名

    解决方法: 首先先输入以下4条命令 1. sudo lshw -numeric -class network2. sudo ifconfig -a3. sudo route -nv4. sudo dh ...

  5. Codeforces Round #439 (Div. 2) A. The Artful Expedient

    A. The Artful Expedient 题目链接http://codeforces.com/contest/869/problem/A 解题心得:就是一个水题,读懂题就好,题意是,(i,j)a ...

  6. Android开发——ThreadLocal功能介绍

    个静态的监听器对象,显然是无法接受的. 2.  使用实例 //首先定义一个ThreadLocal对象,选择泛型为Boolean类型 private ThreadLocal<Boolean> ...

  7. 通过日志动态查看正在执行的mysql语句

    通过日志动态查看正在执行的mysql语句 :tail -f /tmp/general_log.log

  8. mysql 处理数据库中的重复行

    1.对于主键和唯一索引,可以用IGNORE关键字,遇到重复记录会直接忽略插入记录,返回0.insert ignore into table_name ('id','name') values (1,' ...

  9. HDU 4280 Island Transport

    Island Transport Time Limit: 10000ms Memory Limit: 65536KB This problem will be judged on HDU. Origi ...

  10. Spring Cloud 从入门到精通

    Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...