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麻省理工學院