[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 ...
随机推荐
- day20-python之装饰器
1.装饰器 #!/usr/bin/env python # -*- coding:utf-8 -*- import time def cal(l): start_time=time.time() re ...
- HashMap图解
HashMap的数据结构和put.get.resize等操作的图解,看图轻松掌握HashMap (目前还不包括红黑树相关的部分) HashMap数据结构如下图 HashMap之put操作如下图 Has ...
- pandas中Timestamp类用法讲解
由于网上关于Timestamp类的资料比较少,而且官网上面介绍的很模糊,本文只是对如何创建Timestamp类对象进行简要介绍,详情请读者自行查阅文档. 以下有两种方式可以创建一个Timestamp对 ...
- spring配置datasource三种方式 数据库连接池
尊重原创(原文链接):http://blog.csdn.net/kunkun378263/article/details/8506355 1.使用org.springframework.jdbc.da ...
- C. RMQ with Shifts
C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB 64-bit intege ...
- LINQ-进行数据转换
一.将多个输入联接到一个输出序列中 可以使用 LINQ 查询创建包含元素的输出序列,这些元素来自多个输入序列. 以下示例演示如何组合两个内存中数据结构,但相同的原则可应用于组合来自 XML 或 SQL ...
- Spring Data Redis 的坑
用 Spring data redis 的redisTemplate存储数据的时候发现,它的键值前多出现了字符串:\xac\xed\x00\x05t\x00\x03 如本来key=name,会变成&q ...
- mysql5.7.20搭建
官网mysql下载源码安装 下载MySQL5.7.20源码包,wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.g ...
- 算法复习——虚树(消耗战bzoj2286)
题目: Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战 ...
- Spring JdbcTemplate操作小结
Spring 提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流 ...