wxml-parser-rs
v0.0.2
Published
Rust + napi-rs implementation of @wxml/parser
Readme
wxml-parser-rs
wxml-parser-rs 是 @wxml/parser 的 Rust + napi-rs 实现,提供:
- Rust 核心解析库(
wxml-parser-rs) - Node.js API(
parse/parseForESLint),附带多平台预编译二进制
安装
npm install wxml-parser-rs或 Rust 侧:
cargo add wxml-parser-rs使用方法
Node.js
const { parse, parseForESLint } = require('wxml-parser-rs')
const code = `<view wx:if="{{ok}}">hello</view>`
const ast = parse(code)
console.log(ast.type) // Program
const eslintAst = parseForESLint(code)
console.log(eslintAst.ast.type) // ProgramRust
use wxml_parser_rs::{parse_json, parse_for_eslint_json};
fn main() {
let code = r#"<view>{{ message }}</view>"#;
let ast = parse_json(code);
println!("{}", ast["type"]); // "Program"
let eslint_ast = parse_for_eslint_json(code);
println!("{}", eslint_ast["ast"]["type"]); // "Program"
}性能
基于 tests/fixtures/bench/complex-mixed-large.wxml,对比三种实现:
rust-core:直接调用 Rustparse_json/parse_for_eslint_jsonnapi:通过loader.js调用parse/parseForESLintjs-parser:@wxml/parser的 JS 实现
环境:Apple M2 Pro / macOS / Node v20.20.1 / rustc 1.94.1 · warmup 4 轮 · 采样 8 轮
parse
| Implementation | Median ms/op | ops/sec | Relative | | --- | ---: | ---: | ---: | | rust-core | 0.049 | 19,836.6 | 6.69x | | napi | 0.423 | 2,364.5 | 0.78x | | js-parser | 0.330 | 2,812.0 | 1.00x |
parseForESLint
| Implementation | Median ms/op | ops/sec | Relative | | --- | ---: | ---: | ---: | | rust-core | 0.054 | 18,377.7 | 6.74x | | napi | 0.444 | 2,255.2 | 0.83x | | js-parser | 0.366 | 2,750.8 | 1.00x |
Rust 核心解析速度约为 JS 实现的 6.7 倍;通过 napi 调用因跨边界开销,相比 JS 实现约慢 17-20%。可直接运行 npm run bench 复现。
开发
# 安装依赖
npm install
# 构建(debug)
npm run build:debug
# 构建(release)
npm run build
# 测试
npm test
# Rust 侧测试
cargo test -p wxml-parser-rs
# 基准测试
npm run bench版本管理
发布新版本前,使用版本同步脚本更新所有 manifest:
npm run version:bump <new-version>该脚本会同步更新 package.json、crates/wxml-parser-core/Cargo.toml、crates/wxml-parser-napi/Cargo.toml 中的版本号。更新后请一并更新 CHANGELOG.md。
多平台产物
编译后的 .node 文件统一放在 bindings/ 目录。本地构建 + 收集:
npm run build:multi多平台产物通过 CI matrix 在不同平台构建后自动收集。
发布
npm
发布流程由 CI 自动完成:推送 v* tag 即可触发构建、测试和发布。
手动发布(需先构建):
npm run build
npm test
npm publishcrates.io
cargo publish --dry-run -p wxml-parser-rs
cargo publish -p wxml-parser-rs
wxml-parser-napi仅作为 npm 绑定层,不发布到 crates.io。
项目结构
├── crates/
│ ├── wxml-parser-core/ # Rust 解析核心(crate 名:wxml-parser-rs)
│ └── wxml-parser-napi/ # Node-API 绑定层(napi-rs)
├── tests/ # 兼容性测试
├── scripts/ # 构建/版本管理脚本
├── bindings/ # 多平台预编译 .node 产物
├── loader.js # Node.js 加载入口
├── index.js # napi 生成入口
└── index.d.ts # TypeScript 类型声明