percy
1.0.0
使用Rust + WebAssembly构建前端浏览器应用程序。支持服务器端渲染。
此读书人对珀西进行了介绍。查看珀西书,完整步行。
珀西(Percy
在夜间生锈中,您可以创建文本节点而无需引用。
// Nightly Rust does not require quotes around text nodes.
html ! { <div> My text nodes here </div> } ;在稳定的生锈上,需要引号。
// Stable Rust requires quotes around text nodes.
html ! { <div> { "My text nodes here " } </div> } ;一旦跨度位置稳定在Rust编译器 - 生锈的跟踪问题中,这种差异就会消失。
提高速度的最佳方法是查看珀西书,但这是一个非常基本的例子,可以弄湿您的脚。
Percy允许您创建仅具有服务器端渲染的应用程序,仅客户端渲染或服务器和客户端渲染。
这是您可以立即尝试的客户端渲染的快速和愉快的工作示例:
首先,使用
cargo new client-side-web-app --lib
cd client-side-web-app将以下文件添加到您的项目中。
touch build.sh
touch index.html
touch app.css这是目录结构:
.
├── Cargo.toml
├── build.sh
├── index.html
├── app.css
└── src
└── lib.rs现在,用以下内容编辑每个文件:
# contents of build.sh
#! /bin/bash
cd " $( dirname " $0 " ) "
mkdir -p public
cargo build --target wasm32-unknown-unknown
wasm-bindgen target/wasm32-unknown-unknown/debug/client_side_web_app.wasm --no-typescript --target web --out-dir ./public --debug
cp index.html public/
cp app.css public/ // contents of src/lib.rs
use wasm_bindgen :: prelude :: * ;
use web_sys ;
use percy_dom :: prelude :: * ;
# [ wasm_bindgen ]
struct App {
pdom : PercyDom
}
# [ wasm_bindgen ]
impl App {
# [ wasm_bindgen ( constructor ) ]
pub fn new ( ) -> App {
let start_view = html ! { <div> Hello </div> } ;
let window = web_sys :: window ( ) . unwrap ( ) ;
let document = window . document ( ) . unwrap ( ) ;
let body = document . body ( ) . unwrap ( ) ;
let mut pdom = PercyDom :: new_append_to_mount ( start_view , & body ) ;
let greetings = "Hello, World!" ;
let end_view = html ! {
// Use regular Rust comments within your html
<div class= [ "big" , "blue" ] >
/* Interpolate values using braces */
<strong> { greetings } </strong>
<button
class= "giant-button"
onclick=|_event| {
web_sys :: console :: log_1 ( & "Button Clicked!" . into ( ) ) ;
}
>
// No need to wrap text in quotation marks (:
Click me and check your console
</button>
</div>
} ;
pdom . update ( end_view ) ;
App { pdom }
}
} # contents of Cargo.toml
[ package ]
name = " client-side-web-app "
version = " 0.1.0 "
authors = [ " Friends of Percy " ]
edition = " 2018 "
[ lib ]
crate-type = [ " cdylib " ] # Don't forget this!
[ dependencies ]
wasm-bindgen = " 0.2 "
js-sys = " 0.3 "
percy-dom = " 0.9 "
[ dependencies . web-sys ]
version = " 0.3 "
features = [
" Document " ,
" MouseEvent " ,
" Window " ,
" console "
] <!-- contents of index.html -->
<!DOCTYPE html >
< html lang =" en " >
< head >
< meta charset =" UTF-8 " >
< meta name =" viewport " content =" width=device-width, initial-scale=1 " >
< link rel =" stylesheet " type =" text/css " href =" app.css " />
< title > Client Side Demo </ title >
</ head >
< body style =' margin: 0; padding: 0; width: 100%; height: 100%; ' >
< script type =" module " >
import init , { App } from '/client_side_web_app.js'
async function run ( ) {
await init ( '/client_side_web_app_bg.wasm' )
new App ( )
}
run ( )
</ script >
</ body >
</ html > /* contents of app.css */
. big {
font-size : 30 px ;
}
. blue {
color : blue;
}
. giant-button {
font-size : 24 px ;
font-weight : bold;
}现在运行
# Used to compile your Rust code to WebAssembly
cargo install wasm-bindgen-cli
# Or any other static file server that supports the application/wasm mime type
cargo install https
chmod +x ./build.sh
./build.sh
# Visit localhost:8080 in your browser
http ./public --port 8080而且您应该看到以下内容:

做得好!
同构Web应用程序
单元测试视图组件
如果您有一个有用的例子,请打开问题或公关!
Percy-Dom API文档
HTML-MACRO API文档
Percy-Router API文档
始终有任何问题 /想法都可以自由地打开问题和公关!
即使感觉很基本或简单 - 如果您想到您无法快速回答自己的问题,那是文档中的失败。
有关如何为代码库做出贡献的更多信息可以在珀西书的贡献部分中找到!
要运行所有单元,集成和浏览器测试,请抓住依赖项:
./test.sh麻省理工学院