Skip to content

Commit 72ca879

Browse files
janechuCopilot
andcommitted
feat: add @microsoft/fast-build npm package with WASM CLI
- Add wasm-bindgen as a conditional WASM-target dependency to the microsoft-fast-build Rust crate (zero-dependency for native targets) - Add src/wasm.rs exposing render() and render_with_templates() via wasm-bindgen for use in Node.js - Build the crate to WASM (NodeJS target) with wasm-pack - Create packages/fast-build/ with: - package.json (@microsoft/fast-build, no runtime npm dependencies) - bin/fast.js: CLI supporting `fast build` with arguments: --templates="<glob>" custom element HTML templates (warns if missing) --output="output.html" (default: output.html) --entry="index.html" (default: index.html) --state="state.json" (default: state.json) - wasm/: compiled WASM module and JS bindings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3c3d3da commit 72ca879

10 files changed

Lines changed: 618 additions & 0 deletions

File tree

crates/microsoft-fast-build/Cargo.lock

Lines changed: 107 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/microsoft-fast-build/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ categories = ["template-engine", "web-programming"]
1313
[lib]
1414
name = "microsoft_fast_build"
1515
path = "src/lib.rs"
16+
crate-type = ["cdylib", "rlib"]
17+
18+
[target.'cfg(target_arch = "wasm32")'.dependencies]
19+
wasm-bindgen = "0.2"

crates/microsoft-fast-build/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ mod node;
99
mod renderer;
1010
mod error;
1111
mod locator;
12+
#[cfg(target_arch = "wasm32")]
13+
mod wasm;
1214

1315
pub use json::{JsonValue, JsonError};
1416
pub use error::RenderError;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use wasm_bindgen::prelude::*;
2+
use std::collections::HashMap;
3+
use crate::{json, Locator, render_template, render_template_with_locator};
4+
5+
/// Render a FAST HTML template with a JSON state string.
6+
/// Returns the rendered HTML or throws a JavaScript error.
7+
#[wasm_bindgen]
8+
pub fn render(entry: &str, state: &str) -> Result<String, JsValue> {
9+
render_template(entry, state).map_err(|e| JsValue::from_str(&e.to_string()))
10+
}
11+
12+
/// Render a FAST HTML template with custom element templates and a JSON state string.
13+
/// `templates_json` is a JSON object mapping element names to their HTML template strings,
14+
/// e.g. `{"my-button": "<template>...</template>"}`.
15+
/// Returns the rendered HTML or throws a JavaScript error.
16+
#[wasm_bindgen]
17+
pub fn render_with_templates(entry: &str, templates_json: &str, state: &str) -> Result<String, JsValue> {
18+
let templates = parse_templates_map(templates_json)?;
19+
let locator = Locator::from_templates(templates);
20+
render_template_with_locator(entry, state, &locator)
21+
.map_err(|e| JsValue::from_str(&e.to_string()))
22+
}
23+
24+
fn parse_templates_map(templates_json: &str) -> Result<HashMap<String, String>, JsValue> {
25+
let parsed = json::parse(templates_json)
26+
.map_err(|e| JsValue::from_str(&format!("Failed to parse templates JSON: {}", e.message)))?;
27+
match parsed {
28+
json::JsonValue::Object(obj) => {
29+
let mut map = HashMap::new();
30+
for (k, v) in obj {
31+
match v {
32+
json::JsonValue::String(s) => { map.insert(k, s); }
33+
_ => return Err(JsValue::from_str(&format!(
34+
"Template value for '{}' must be a string", k
35+
))),
36+
}
37+
}
38+
Ok(map)
39+
}
40+
_ => Err(JsValue::from_str("Templates must be a JSON object")),
41+
}
42+
}

0 commit comments

Comments
 (0)