vite - WebAssembly入门

2024-04-16 06:20
文章标签 入门 webassembly vite

本文主要是介绍vite - WebAssembly入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 初始化 vite 项目

1.1 安装 nvm(可选)

brew update
brew install nvm

~/.zshrc 添加

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

执行如下命令

source ~/.zshrc

1.2 安装 node

nvm install node
nvm ls                 
->      v21.7.2system
default -> node (-> v21.7.2)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v21.7.2) (default)
stable -> 21.7 (-> v21.7.2) (default)
...

1.3 根据模版初始化项目

npm create vite@latest my-vue-app -- --template vue-tscd my-vue-app
npm install
npm run dev

2. 初始化 rust 环境

2.1 安装 rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2.2 安装 wasm-pack

cargo install wasm-pack
wasm-pack -h
📦 ✨  pack and publish your wasm!Usage: wasm-pack [OPTIONS] <COMMAND>Commands:build    🏗️  build your npm package!pack     🍱  create a tar of your npm package but don't publish!new      🐑 create a new project with a templatepublish  🎆  pack up your npm package and publish!login    👤  Add an npm registry user account! (aliases: adduser, add-user)test     👩‍🔬  test your wasm!help     Print this message or the help of the given subcommand(s)Options:-v, --verbose...             Log verbosity is based off the number of v used-q, --quiet                  No output printed to stdout--log-level <LOG_LEVEL>  The maximum level of messages that should be logged by wasm-pack. [possible values: info, warn, error] [default: info]-h, --help                   Print help-V, --version                Print version

2.3 安装 rsw

cargo install rsw
rsw -h              
rsw 0.8.0
wasm-pack based build toolUSAGE:rsw <SUBCOMMAND>OPTIONS:-h, --help       Print help information-V, --version    Print version informationSUBCOMMANDS:build    build rust crates, useful for shipping to productionclean    clean - `npm link` and `wasm-pack build`help     Print this message or the help of the given subcommand(s)init     generate `rsw.toml` configuration filenew      quickly generate a crate with `wasm-pack new`, or set a custom template in`rsw.toml [new]`watch    automatically rebuilding local changes, useful for development and debugging

2.4 初始化 rsw 配置

rsw init

执行成功后会在当前目录生成 rsw.toml 文件

2.5 新建 wasm 项目

rsw new @rsw/hello

会在当前目录生成项目目录

tree @rsw                
@rsw
└── hello├── Cargo.toml├── LICENSE_APACHE├── LICENSE_MIT├── README.md├── src│   ├── lib.rs│   └── utils.rs└── tests└── web.rs

2.6 修改配置文件

rws.toml 文件中添加如下配置

[[crates]]
name = "@rsw/hello"
link = true

2.7 构建项目

rsw build
[rsw::INFO] 🚧  wasm-pack build @rsw/hello --out-dir pkg --target web --release --scope rsw
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: 🌀  Compiling to Wasm...Finished release [optimized] target(s) in 0.21s
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: ✨   Done in 1.34s
[INFO]: 📦   Your wasm pkg is ready to publish at @rsw/hello/pkg.[✨ rsw::build] @rsw/hello "0.1.0"◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻◼◻up to date, audited 292 packages in 7s69 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities
[🔗 rsw::link] npm link ./@rsw/hello/pkg

3. vite 项目使用 wasm 构建产物

修改 ./src/Components/HelloWorld.vue

<script setup lang="ts">
import { ref } from 'vue'
import init, { greet } from "@rsw/hello";defineProps<{ msg: string }>()const count = ref(0)const click = function () {count.value = count.value + 1init().then(() => {greet();});
}
</script><template><h1>{{ msg }}</h1><div class="card"><button type="button" @click="click">count is {{ count }}</button><p>Edit<code>components/HelloWorld.vue</code> to test HMR</p></div><p>Check out<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank">create-vue</a>, the official Vue + Vite starter</p><p>Install<a href="https://github.com/vuejs/language-tools" target="_blank">Volar</a>in your IDE for a better DX</p><p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template><style scoped>
.read-the-docs {color: #888;
}
</style>

运行后效果如下

npm run dev

这篇关于vite - WebAssembly入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/908003

相关文章

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE