[WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack
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的更多相关文章
- 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 ...
- [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 ...
- 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. ...
- 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 ...
- npm & package.json & directories & files
npm & package.json & directories & files package.json https://docs.npmjs.com/files/packa ...
- [Node.js] Configuring npm package.json scripts
With a node package manager's (npm) package.json script property, you can preconfigure common tasks ...
- Angular Npm Package.Json文件详解
Angular7 Npm Package.Json文件详解 近期时间比较充裕,正好想了解下Angular Project相关内容.于是将Npm官网上关于Package.json的官方说明文档进行了 ...
- Node.js NPM Package.json
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...
- 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 ...
随机推荐
- gpio/外设/控制器
1.项目中所有的外设pad都是通过GPIO与控制器相连的.比如FSHC<=>gpio<=>flash 2.gpio类似多个 mux 集合. 3.对于与gpio相连的pad具体结 ...
- CSS 媒体查询 响应式
媒体查询 从 CSS 版本 2 开始,就可以通过媒体类型在 CSS 中获得媒体支持.如果您曾经使用过打印样式表,那么您可能已经使用过媒体类型.清单 1 展示了一个示例. 清单 1. 使用媒体类型 &l ...
- web项目架构
二
- [android开发篇]使用系统权限流程
1 声明权限https://developer.android.com/training/permissions/declaring.html 每款 Android 应用都在访问受限的沙盒中运行.如果 ...
- 快速排序php
<?php /** * Created by PhpStorm. * User: brady.wang * Date: 2017/11/10 * Time: 9:45 */ $arr=array ...
- shell的if-else的基本用法
if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... ...
- iOS静态库(.a文件)
1.找到静态库工程
- 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流
题目描述 Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的 ...
- BZOJ 1933 [Shoi2007]Bookcase 书柜的尺寸 ——动态规划
状态设计的方法很巧妙,六个值 h1,h2,h3,t1,t2,t3,我们发现t1,t2,t3可以通过前缀和优化掉一维. 然后考虑把h留下还是t留下,如果留下h显然t是会发生改变的,一个int存不下. 如 ...
- 如何快速下载maven依赖jar包
找到settings.xml文件.在mirrors里面添加下面的代码: <mirror> <id>alimaven</id> <mirrorOf>cen ...