Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow lazily checking of the imported names #110

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion glass-easel-template-compiler/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::*;
// P (preserved for runtime)
// Q (extra runtime helpers)
// R: the global script module / the `ProcGenWrapper` object
// S: the `DefineSlot` function
// S: the `DefineSlot` function / the imported group cache
// T: the `DefineTextNode` function / the `updateText` function
// U: the update path tree
// V: the current slot values
Expand Down
54 changes: 31 additions & 23 deletions glass-easel-template-compiler/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,29 +269,37 @@ impl TmplTree {
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "var I={{}}")?;
write!(w, "var S")?;
Ok(())
})?;
for target_path in self.imports.iter() {
let p = path::resolve(&self.path, target_path);
if let Ok(target_tree) = group.get_tree(&p) {
for k in target_tree.sub_templates.keys() {
let k = gen_lit_str(k);
w.expr_stmt(|w| {
write!(
w,
r#"I[{}]=function(R,C,D,U){{return G[{}]({})(R,C,D,U)}}"#,
k,
gen_lit_str(&p),
k
)?;
Ok(())
w.expr_stmt(|w| {
write!(w, "var I=")?;
w.function_args("P", |w| {
w.expr_stmt(|w| {
write!(w, "if(!S)")?;
w.brace_block(|w| {
w.expr_stmt(|w| {
write!(w, "S={{}}")?;
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "Object.assign(S")?;
for target_path in self.imports.iter() {
let p = path::resolve(&self.path, target_path);
write!(w, ",G[{}]._", gen_lit_str(&p))?;
}
write!(w, ",H)")?;
Ok(())
})
})?;
}
} else {
// FIXME warn no target file
}
}
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "return S[P]")?;
Ok(())
})
})
})?;
let write_template_item = |
key,
w: &mut JsFunctionScopeWriter<W>,
Expand All @@ -301,7 +309,7 @@ impl TmplTree {
has_scripts: bool,
| {
w.expr_stmt(|w| {
write!(w, "H[{key}]=I[{key}]=", key = gen_lit_str(key))?;
write!(w, "H[{key}]=", key = gen_lit_str(key))?;
w.function_args("R,C,D,U", |w| {
if has_scripts {
w.expr_stmt(|w| {
Expand Down Expand Up @@ -392,7 +400,7 @@ impl TmplTree {
has_scripts,
)?;
w.expr_stmt(|w| {
write!(w, "return function(R){{return H[R]}}")?;
write!(w, "return Object.assign(function(R){{return H[R]}},{{_:H}})")?;
Ok(())
})?;
Ok(())
Expand Down Expand Up @@ -1054,7 +1062,7 @@ impl TmplElement {
w.function_args("C,T,E,B,F,S,J", |w| {
let var_target = w.gen_ident();
w.expr_stmt(|w| {
write!(w, "var {}=I[{}]", var_target, var_key)?;
write!(w, "var {}=I({})", var_target, var_key)?;
Ok(())
})?;
match data {
Expand Down
Loading