从零实现 React v18,但 WASM 版 - [1] 项目框架搭建

模仿 big-react,使用 Rust 和 WebAssembly,从 0 到 1 实现从零实现 React v18 的核心功能。深入理解 React 源码的同时,还锻炼了 Rust 的技能,简直赢麻了!

代码地址:github.com/ParadeTo/bi...

本文对应 tag:v1

工具

Rust:一种安全、高效和现代的编程语言(省略一万字)。按照官网所示开发即可。

wasm-pack:构建、测试、和发布 Rust WebAssembly 的一站式工具。

cargo-generate:通过已有的 Git 仓库作为模版,快速创建 Rust 项目。

更多介绍可以参考 Rust 和 WebAssembly 入门教程。

其他前端常用的工具自行安装即可,就不赘述了。

项目结构

首先,我们搭建起如下的项目结构:

js 复制代码
.
├── Cargo.toml
├── package.json
├── examples
│   └── hello-world // vite 初始化的 react 项目
├── packages
│   ├── react // 通过 cargo generate --git https://github.com/rustwasm/wasm-pack-template 生成的 WASM 项目
│   ├── react-dom // 通过 cargo generate --git https://github.com/rustwasm/wasm-pack-template 生成的 WASM 项目
│   ├── react-reconciler // cargo new 生成的普通 rust 项目
│   └── shared // cargo new 生成的普通 rust 项目

Cargo.toml 如下所示,类似于前端中常说的 monorepo 架构。

ini 复制代码
[workspace]

members = [
    "packages/react",
    "packages/react-dom",
    "packages/react-reconciler",
    "packages/shared"
]

因为 react 和 react-dom 会导出方法供 JS 侧进行调用,所以需要通过 cargo generate --git https://github.com/rustwasm/wasm-pack-template 创建 WASM 项目,另外两个使用 cargon new 创建普通的 Rust 项目即可。

构建调试环境

我们先删掉 hello-world/src/main.tsx 中的代码,写一个非常简单的例子:

ts 复制代码
import {createRoot} from 'react-dom'

const comp = <div>hello world</div>
console.log(comp)
console.log(createRoot(document.getElementById('root')))

在开发环境运行起来,在浏览器调试窗口中可以看到编译后的代码是这样的:

现在我们的目标是让 hello-world 里面使用的是我们当前正在开发的 React,要让上述代码成功运行,我们需要做以下几步:

  1. 修改 hello-world 项目下的 package.json:
json 复制代码
    "react": "file://../../packages/react/pkg/react",
    "react-dom": "file://../../packages/react-dom/pkg/react-dom",
  1. 在根目录下的 package.json 添加打包命令,使用 wasm-pack 把 react 和 react-dom 打包成 WASM:
json 复制代码
  "scripts": {
    "build:react": "wasm-pack build packages/react --out-dir pkg/react --out-name jsx-dev-runtime",
    "build:react-dom": "wasm-pack build packages/react-dom --out-dir pkg/react-dom --out-name index",
    "build": "npm run build:react && npm run build:react-dom"
  },
  1. 分别在 react 和 react-dom 中的 lib.rs 添加如下代码:
rust 复制代码
// react/src/lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn jsxDEV() -> String {
    "hello world".to_string()
}
rust 复制代码
// react-dom/src/lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn createRoot(container: &JsValue) -> JsValue {
    JsValue::null()
}

由于 Rust 中的命名风格一般是下划线风格的,所以最好改成这样:

rust 复制代码
// react/src/lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = jsxDev)]
pub fn jsx_dev() -> String {
    "hello world".to_string()
}
rust 复制代码
// react-dom/src/lib.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = createRoot)]
pub fn create_root(container: &JsValue) -> JsValue {
    JsValue::null()
}
  1. 在根目录下运行 npm run build,然后在 hello-world 目录下运行 pnpm install 和 npm run dev,在浏览器中打开页面,可以看到如下输出:

这说明在 JS 侧成功调用了 WASM 导出的方法,调试环境搭建好了。不过有点麻烦的是,如果修改了代码,需要重新执行步骤 4。

下一篇我们来实现 jsx_dev 的功能,敬请期待。

相关推荐
遇码3 小时前
给 Rust 桌面应用做双击安装包:Windows NSIS setup.exe + macOS DMG 的完整做法
人工智能·windows·ai·rust·大模型
GitCode官方6 小时前
本期精选 Rust 开源项目推荐
开发语言·rust·开源·g-star·atomgit
nezuko359 小时前
用 AI 把代码改写成 Rust 之后,怎么证明它没改坏?我做了个开源工具
rust
爆米花byh1 天前
在RockyLinux9环境下安装Rust1.100.0环境
linux·rust
hai3152475431 天前
阶乘分层关系模型存储文本与表格
数据结构·算法·rust·排序算法·线性回归
低代码布道师1 天前
第06篇:用户新增——React Hook Form + Zod 搭建可复用的表单体系
前端·react.js·前端框架
Shota Kishi1 天前
Solana Stream SDK 的 UDP Shred 接收:data-only 与 recover 两种模式的取舍与实测
rust·区块链·solana
打工仔折腾 AI1 天前
智能体开发实战:Agent架构拆解与ReAct执行循环工程落地
前端·后端·python·react.js·架构·langchain·ai agent 实战
tomatohug1 天前
Tauri v2 实战:做一个贴在任何输入框上的提示词优化助手
rust
Zenova EdgeOS1 天前
Rust 数据库实战:工业边缘场景下的 sqlx
数据库·oracle·rust·边缘计算·工业网关