vite - WebAssembly入门

news2024/9/20 1:10:41

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.2
         system
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-ts

cd 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 template
  publish  🎆  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 tool

USAGE:
    rsw <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    build    build rust crates, useful for shipping to production
    clean    clean - `npm link` and `wasm-pack build`
    help     Print this message or the help of the given subcommand(s)
    init     generate `rsw.toml` configuration file
    new      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 7s

69 packages are looking for funding
  run `npm fund` for details

found 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 + 1
  init().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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1597636.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

非线性特征曲线线性化插补器(CODESYS 完整ST代码)

1、如何利用博途PLC和信捷PLC实现非线性特征曲线的线性化可以参考下面文章链接: 非线性特征曲线线性化(插补功能块SCL源代码+C代码)_scl直线插补程序-CSDN博客文章浏览阅读382次。信捷PLC压力闭环控制应用(C语言完整PD、PID源代码)_RXXW_Dor的博客-CSDN博客闭环控制的系列文章…

【QT+QGIS跨平台编译】181:【QGIS+Qt跨平台编译】—【错误处理:找不到_DEBUGA】

点击查看专栏目录 文章目录 一、找不到_DEBUGA二、原因分析三、错误处理 一、找不到_DEBUGA 报错信息&#xff1a; 二、原因分析 采用了非UNICODE&#xff1a; DEFINES - UNICODE没法识别 _DEBUGA 但可以识别 _DEBUG 三、错误处理 修改 _DEBUGA 为 _DEBUG

轻量级的Spring Cloud Gateway实践,实现api和websocket转发

当国内大部分都是粘贴复制一些重型框架时&#xff0c;有没有人会想到&#xff0c;我们自己做一个小项目&#xff0c;几个小的Spring boot的项目时&#xff0c;我们是否还需要按部就班的用我们公司中用到的Nacos&#xff0c;这种冗余且调配复杂的组件呢&#xff1f; 不是本人说…

学习STM32第十四天

软件SPI读写W25Q64 一、简介 对W25Q64模块进行读写操作时&#xff0c;输出引脚配置为推挽输出&#xff0c;输入引脚配置为浮空或上拉输入。时钟、主机输出和片选都是输出引脚&#xff0c;主机输入是输入引脚。SPI协议是通过命令和数据进行通信&#xff0c;在硬件中使用移位寄…

【练习】二分查找

1、704 &#xff08;1&#xff09;题目描述 &#xff08;2&#xff09;代码实现 package com.hh.practice.leetcode.array.demo_02;public class BinarySearch_704 {public int search(int[] nums, int target) {int i 0,j nums.length -1;while (i < j){int mid (ij) &…

ansible-tower连接git实现简单执行playbook

前提&#xff1a;安装好ansible-tower和git&#xff0c;其中git存放ansible得剧本 其中git中得内容为&#xff1a; --- - name: yjxtesthosts: yinremote_user: rootgather_facts: noroles:- testroles/test/tasks/main.yml #文件内容 --- #- name: Perform Test Task # tas…

【论文笔记】PointMamba: A Simple State Space Model for Point Cloud Analysis

原文链接&#xff1a;https://arxiv.org/abs/2402.10739 1. 引言 基于Transformer的点云分析方法有二次时空复杂度&#xff0c;一些方法通过限制感受野降低计算。这引出了一个问题&#xff1a;如何设计方法实现线性复杂度并有全局感受野。 状态空间模型&#xff08;SSM&…

Win 运维 | Windows Server 系统事件日志浅析与日志审计实践

[ 重剑无锋&#xff0c;大巧不工。] 大家好&#xff0c;我是【WeiyiGeek/唯一极客】一个正在向全栈工程师(SecDevOps)前进的技术爱好者 作者微信&#xff1a;WeiyiGeeker 公众号/知识星球&#xff1a;全栈工程师修炼指南 主页博客: 【 https://weiyigeek.top 】- 为者常成&…

【vue】购物车案例

change"fun"&#xff1a;元素值发生改变时&#xff0c;会触发事件fun <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale…

怎么把相机储存卡里的照片导出来?介绍两种方法

随着摄影技术的不断发展和普及&#xff0c;相机已成为我们记录生活、捕捉美好瞬间的设备。然而&#xff0c;对于许多摄影爱好者来说&#xff0c;如何将相机储存卡里的照片安全、高效地导出到电脑或其他设备中&#xff0c;却成为了一个令人头疼的问题。本文将为您详细介绍从相机…

求1000以内正整数的平方根(C语言)

一、运行结果&#xff1b; 二、源代码&#xff1b; # define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <math.h>int main() {//初始化变量值&#xff1b;int number 0;int result 0;//提示用户&#xff1b;printf("请输入1000以内求平方根的…

ubuntu 23.10.1 mysql 安装

注&#xff1a;请进入root用户模式下操作&#xff0c;若没有&#xff0c;输入命令前加上sudo 1、更新软件包列表 apt update2、安装最新版的Mysql服务器 apt install mysql-server -y如果不加-y 会在安装过程中&#xff0c;系统将提示你设置MySQL的root密码。确保密码足够强…

数据结构基础题复习-第02套

数据结构-第02套 B C C C C C D A A C B B D A 5*6/2318 01234567891011121314151617181920 D C A B C D 3 15&#xff0c;33&#xff0c;55&#xff0c;65&#xff0c;70&#xff0c;100, B A D A C D A 01234567891011121314461516171819206…

SpringBoot 启动分析

一、序言 本文简单分析一下 SpringBoot 的启动流程。 二、SpringBoot 启动源码分析 public ConfigurableApplicationContext run(String... args) {// 记录当前时间的纳秒数&#xff0c;用于计算应用程序启动所花费的时间long startTime System.nanoTime();// 创建一个默认…

SpringBoot整合消息中间件(ActiveMQ,RabbitMQ,RocketMQ,Kafka)

消息中间件 消息消息队列JMS AMQPMQTTKafka Spring整合消息队列模拟消息队列的工作流程Spring整合ActiveMQSpring整合RabbitMQ直连交换机模式主题交换机模式 Spring整合RocketMQSpring整合kafka 消息 消息的发送方&#xff1a;生产者 消息的接收方&#xff1a;消费者 同步消息…

【机器学习300问】70、向量化技术来计算神经网络时维度如何确保正确?

一、向量化技术在进行神经网络计算时的优势 向量化是一种优化技术&#xff0c;通过使用数组操作代替for循环&#xff0c;可以大大提高代码的性能和效率。在深度学习中尤其明显&#xff0c;可以提高计算效率、简化代码、优化内存使用。 二、如何确保计算时维度是正确的&#xf…

【讲解下常见的Web前端框架】

&#x1f308;个人主页: 程序员不想敲代码啊 &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共…

1260. 二维网格迁移

1260. 二维网格迁移 原题链接&#xff1a;完成情况&#xff1a;解题思路&#xff1a;参考代码&#xff1a;错误经验吸取 原题链接&#xff1a; 1260. 二维网格迁移 https://leetcode.cn/problems/shift-2d-grid/description/ 完成情况&#xff1a; 解题思路&#xff1a; 这…

【Bugku】sqli-0x1

1.打开靶场&#xff0c;进入实验场景 2.按F12查看源代码&#xff0c;发现有一个/?pls_help路径&#xff0c;在url后加上查看。 3.得到的php源码 首先&#xff0c;代码通过 error_reporting(0) 和 error_log(0) 关闭了错误报告&#xff0c;这可以防止攻击者从错误信息中获取敏…

Rust面试宝典第2题:逆序输出整数

题目 写一个方法&#xff0c;将一个整数逆序打印输出到控制台。注意&#xff1a;当输入的数字含有结尾的0时&#xff0c;输出不应带有前导的0。比如&#xff1a;123的逆序输出为321&#xff0c;8600的逆序输出为68&#xff0c;-609的逆序输出为-906。 解析 这道题本身并没有什么…