wasm-pack 基于rust 的 WebAssembly 开发工具

发布时间 2023-10-22 09:43:40作者: 荣锋亮

目前基于WebAssembly的应用是越来越多了,同时周边工具以及生成也越来越强大了,wasm-pack 是rust
周边一个很强大的工具,以下是一个简单的试用

参考使用

  • 安装
 
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
  • 创建简单项目
wasm-pack new hello-wasm
  • 代码结构

lib.rs 中包含了基于c 以及原生rust 的方法
一些调整
lib.rs

 
mod utils;
 
use wasm_bindgen::prelude::*;
 
#[wasm_bindgen]
pub fn greet()->String {
    return "dalongdemo".to_string();
}
  • 构建
    会自动下载依赖,会有点慢,会生成一个npm 包
 
wasm-pack build -t nodejs

本地安装npm 包

cd pkg && yarn link
  • 项目集成使用
yarn init -y
yarn link hello-wasm

代码使用

const  {greet}  = require("hello-wasm")
 
let result = greet()
 
console.log(result)
  • 效果

 

参考资料

https://github.com/rustwasm/wasm-pack
https://github.com/appcypher/awesome-wasm-langs
https://github.com/AssemblyScript/assemblyscript