diff --git a/fastn-resolved-to-js/src/lib.rs b/fastn-resolved-to-js/src/lib.rs index 4b32f155f..2cfaff633 100644 --- a/fastn-resolved-to-js/src/lib.rs +++ b/fastn-resolved-to-js/src/lib.rs @@ -308,3 +308,56 @@ impl VecMap { values } } + +pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec { + let mut ftd_asts = vec![]; + + let mut export_asts = vec![]; + for thing in fastn_builtins::get_default_bag().values() { + if let fastn_resolved::Definition::Variable(v) = thing { + ftd_asts.push(v.to_ast(doc, None, &mut false)); + } else if let fastn_resolved::Definition::Function(f) = thing { + if f.external_implementation { + continue; + } + ftd_asts.push(f.to_ast(doc)); + } else if let fastn_resolved::Definition::Export { from, to, .. } = thing { + export_asts.push(fastn_js::Ast::Export { + from: from.to_string(), + to: to.to_string(), + }) + } + } + + // Global default inherited variable + ftd_asts.push(fastn_js::Ast::StaticVariable(fastn_js::StaticVariable { + name: "inherited".to_string(), + value: fastn_js::SetPropertyValue::Value(fastn_js::Value::Record { + fields: vec![ + ( + "colors".to_string(), + fastn_js::SetPropertyValue::Reference( + "ftd#default-colors__DOT__getClone()__DOT__setAndReturn\ + (\"is_root\"__COMMA__\ + true)" + .to_string(), + ), + ), + ( + "types".to_string(), + fastn_js::SetPropertyValue::Reference( + "ftd#default-types__DOT__getClone()__DOT__setAndReturn\ + (\"is_root\"__COMMA__\ + true)" + .to_string(), + ), + ), + ], + other_references: vec![], + }), + prefix: None, + })); + + ftd_asts.extend(export_asts); + ftd_asts +} diff --git a/ftd/src/js/mod.rs b/ftd/src/js/mod.rs index 713f1619f..ae76d496f 100644 --- a/ftd/src/js/mod.rs +++ b/ftd/src/js/mod.rs @@ -92,7 +92,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { // Check if document tree has rive. This is used to add rive script. let mut has_rive_components = false; - let mut document_asts = vec![ftd::js::from_tree( + let mut document_asts = vec![fastn_resolved_to_js::from_tree( document.tree.as_slice(), &doc, &mut has_rive_components, @@ -172,24 +172,3 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { scripts, } } - -pub fn from_tree( - tree: &[fastn_resolved::ComponentInvocation], - doc: &dyn fastn_resolved::tdoc::TDoc, - has_rive_components: &mut bool, -) -> fastn_js::Ast { - use fastn_resolved_to_js::ComponentExt; - - let mut statements = vec![]; - for (index, component) in tree.iter().enumerate() { - statements.extend(component.to_component_statements( - fastn_js::COMPONENT_PARENT, - index, - doc, - &fastn_resolved_to_js::ResolverData::none(), - false, - has_rive_components, - )) - } - fastn_js::component0(fastn_js::MAIN_FUNCTION, statements) -} diff --git a/v0.5/Cargo.lock b/v0.5/Cargo.lock index 241267cb5..98406b485 100644 --- a/v0.5/Cargo.lock +++ b/v0.5/Cargo.lock @@ -32,6 +32,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "autocfg" version = "1.4.0" @@ -65,12 +71,39 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +[[package]] +name = "cc" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +dependencies = [ + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "copy_dir" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "543d1dd138ef086e2ff05e3a48cf9da045da2033d16f8538fd76b86cd49b2ca3" +dependencies = [ + "walkdir", +] + [[package]] name = "either" version = "1.13.0" @@ -112,7 +145,9 @@ name = "fastn-compiler" version = "0.1.0" dependencies = [ "fastn-builtins", + "fastn-js", "fastn-resolved", + "fastn-resolved-to-js", "fastn-section", "fastn-unresolved", "indexmap", @@ -135,6 +170,20 @@ dependencies = [ "serde_json", ] +[[package]] +name = "fastn-js" +version = "0.1.0" +dependencies = [ + "fastn-resolved", + "indoc", + "itertools", + "prettify-js", + "pretty", + "quick-js", + "rquickjs", + "thiserror", +] + [[package]] name = "fastn-resolved" version = "0.1.1" @@ -142,6 +191,18 @@ dependencies = [ "serde", ] +[[package]] +name = "fastn-resolved-to-js" +version = "0.1.0" +dependencies = [ + "fastn-builtins", + "fastn-js", + "fastn-resolved", + "indoc", + "itertools", + "serde", +] + [[package]] name = "fastn-section" version = "0.1.0" @@ -173,6 +234,12 @@ dependencies = [ name = "fastn-update" version = "0.1.0" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foldhash" version = "0.1.3" @@ -200,6 +267,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "indexmap" version = "2.6.0" @@ -211,6 +284,12 @@ dependencies = [ "serde", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "itertools" version = "0.13.0" @@ -232,6 +311,16 @@ version = "0.2.164" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" +[[package]] +name = "libquickjs-sys" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f0b24e9bd171b75ae0295bd428fb8fe58410fb23156e5f34a4657a70c3cee96" +dependencies = [ + "cc", + "copy_dir", +] + [[package]] name = "lock_api" version = "0.4.12" @@ -242,6 +331,12 @@ dependencies = [ "scopeguard", ] +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + [[package]] name = "memchr" version = "2.7.4" @@ -278,6 +373,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "parking_lot" version = "0.12.3" @@ -307,6 +408,63 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" +[[package]] +name = "prettify-js" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7ff18ec712649921186715eeee7d880c3c5fb41c86a5a3524057da346e27771" +dependencies = [ + "log", + "ress", + "serde", + "serde_json", +] + +[[package]] +name = "pretty" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b55c4d17d994b637e2f4daf6e5dc5d660d209d5642377d675d7a1c3ab69fa579" +dependencies = [ + "arrayvec", + "typed-arena", + "unicode-width", +] + +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.89" @@ -316,6 +474,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-js" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19cb4cefcb00f4ab9b332664d06005a74f582ac16aa959c6ad5912957bd83e5f" +dependencies = [ + "libquickjs-sys", + "once_cell", +] + [[package]] name = "quote" version = "1.0.37" @@ -363,6 +531,69 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + +[[package]] +name = "ress" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fb1cedc0fd02351568b53be91c4ba2f94beb31ff730bd7df1e54e9b19169b9b" +dependencies = [ + "log", + "unicode-xid", +] + +[[package]] +name = "rquickjs" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cbd33e0b668aea0ab238b9164523aca929096f9f40834700d71d91dd4888882" +dependencies = [ + "rquickjs-core", + "rquickjs-macro", +] + +[[package]] +name = "rquickjs-core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9129d69b7b8f7ee8ad1da5b12c7f4a8a8acd45f2e6dd9cb2ee1bc5a1f2fa3d" +dependencies = [ + "relative-path", + "rquickjs-sys", +] + +[[package]] +name = "rquickjs-macro" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d2ecaf7c9eda262e02a91e9541989a9dd18984d17d0d97f99f33b464318057" +dependencies = [ + "convert_case", + "fnv", + "ident_case", + "indexmap", + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "rquickjs-core", + "syn 2.0.87", +] + +[[package]] +name = "rquickjs-sys" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf6f2288d8e7fbb5130f62cf720451641e99d55f6fde9db86aa2914ecb553fd2" +dependencies = [ + "cc", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -375,6 +606,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -398,7 +638,7 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.87", ] [[package]] @@ -413,6 +653,12 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -448,6 +694,16 @@ dependencies = [ "serde", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.87" @@ -459,6 +715,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "tokio" version = "1.41.1" @@ -485,21 +761,87 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.87", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "unicode-segmentation" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -572,3 +914,12 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] diff --git a/v0.5/Cargo.toml b/v0.5/Cargo.toml index bf6f280bd..bbc456f16 100644 --- a/v0.5/Cargo.toml +++ b/v0.5/Cargo.toml @@ -46,6 +46,8 @@ fastn-section = { path = "fastn-section" } fastn-static = { path = "fastn-static" } fastn-resolved = { path = "../fastn-resolved" } fastn-builtins = { path = "../fastn-builtins" } +fastn-resolved-to-js = { path = "../fastn-resolved-to-js" } +fastn-js = { path = "../fastn-js" } #fastn-resolved = { git = "https://github.com/fastn-stack/fastn.git", rev = "17b7ebca9869c20f6ce86090116614168a18f37e" } #fastn-builtins = { git = "https://github.com/fastn-stack/fastn.git", rev = "17b7ebca9869c20f6ce86090116614168a18f37e" } fastn-unresolved = { path = "fastn-unresolved" } diff --git a/v0.5/fastn-compiler/Cargo.toml b/v0.5/fastn-compiler/Cargo.toml index 763f5dccd..d26c6be60 100644 --- a/v0.5/fastn-compiler/Cargo.toml +++ b/v0.5/fastn-compiler/Cargo.toml @@ -16,5 +16,5 @@ fastn-builtins.workspace = true fastn-unresolved.workspace = true indexmap.workspace = true string-interner.workspace = true - - +fastn-resolved-to-js.workspace = true +fastn-js.workspace = true diff --git a/v0.5/fastn-compiler/src/js.rs b/v0.5/fastn-compiler/src/js.rs index 8fe1afef3..be9442ad0 100644 --- a/v0.5/fastn-compiler/src/js.rs +++ b/v0.5/fastn-compiler/src/js.rs @@ -1,14 +1,107 @@ impl fastn_compiler::Compiler { pub(crate) fn js(&self) -> String { + use fastn_resolved::tdoc::TDoc; + use fastn_resolved_to_js::{ + ComponentDefinitionExt, FunctionExt, PropertyValueExt, VariableExt, + WebComponentDefinitionExt, + }; + // this function should look a bit like ftd::js::document_into_js_ast(), we do not need // to construct the Document object there, but will fetch all the fields as variables // self.content should be all UR::R now - let _resolved_content = self.resolved_content(); + let resolved_content = self.resolved_content(); // every symbol in self.symbol_used in the bag must be UR::R now let needed_symbols = self.needed_symbols(); - let _js_files = self.external_js_files(&needed_symbols); - let _css_files = self.external_css_files(&needed_symbols); + let doc = fastn_compiler::TDoc { + name: "", + bag: &needed_symbols, + default_bag: &fastn_builtins::get_default_bag(), + }; + + // Check if document tree has rive. This is used to add rive script. + let mut has_rive_components = false; + let mut export_asts = vec![]; + + let mut document_asts = vec![fastn_resolved_to_js::from_tree( + resolved_content.as_slice(), + &doc, + &mut has_rive_components, + )]; + + for (_key, thing) in needed_symbols.iter() { + if let fastn_resolved::Definition::Component(c) = thing { + document_asts.push(c.to_ast(&doc, &mut has_rive_components)); + } else if let fastn_resolved::Definition::Variable(v) = thing { + document_asts.push(v.to_ast( + &doc, + Some(fastn_js::GLOBAL_VARIABLE_MAP.to_string()), + &mut has_rive_components, + )); + } else if let fastn_resolved::Definition::WebComponent(web_component) = thing { + document_asts.push(web_component.to_ast(&doc)); + } else if let fastn_resolved::Definition::Function(f) = thing { + document_asts.push(f.to_ast(&doc)); + } else if let fastn_resolved::Definition::Export { from, to, .. } = thing { + if doc.get_opt_record(from).is_some() { + continue; + } + export_asts.push(fastn_js::Ast::Export { + from: from.to_string(), + to: to.to_string(), + }) + } else if let fastn_resolved::Definition::OrType(ot) = thing { + let mut fields = vec![]; + for variant in &ot.variants { + if let Some(value) = &variant.clone().fields().get(0).unwrap().value { + fields.push(( + variant + .name() + .trim_start_matches( + format!( + "{}.", + fastn_resolved::OrType::or_type_name(ot.name.as_str()) + ) + .as_str(), + ) + .to_string(), + value.to_fastn_js_value_with_none(&doc, &mut false), + )); + } + } + document_asts.push(fastn_js::Ast::OrType(fastn_js::OrType { + name: ot.name.clone(), + variant: fastn_js::SetPropertyValue::Value(fastn_js::Value::Record { + fields, + other_references: vec![], + }), + prefix: Some(fastn_js::GLOBAL_VARIABLE_MAP.to_string()), + })); + } + } + + document_asts.extend(export_asts); + + let mut scripts = fastn_resolved_to_js::utils::get_external_scripts(has_rive_components); + scripts.push(fastn_resolved_to_js::utils::get_js_html( + self.external_js_files(&needed_symbols).as_slice(), + )); + scripts.push(fastn_resolved_to_js::utils::get_css_html( + self.external_css_files(&needed_symbols).as_slice(), + )); + + let js_document_script = fastn_js::to_js(document_asts.as_slice(), ""); + + let js_ftd_script = fastn_js::to_js( + fastn_resolved_to_js::default_bag_into_js_ast(&doc).as_slice(), + "foo", + ); + + let _ssr_body = fastn_js::ssr_with_js_string( + "foo", + format!("{js_ftd_script}\n{js_document_script}").as_str(), + ) + .unwrap(); todo!() } diff --git a/v0.5/fastn-compiler/src/lib.rs b/v0.5/fastn-compiler/src/lib.rs index 69311b2df..ffeeb2663 100644 --- a/v0.5/fastn-compiler/src/lib.rs +++ b/v0.5/fastn-compiler/src/lib.rs @@ -8,6 +8,7 @@ mod compiler; mod js; mod symbols; mod tdoc; +pub use tdoc::TDoc; mod utils; pub use compiler::compile; diff --git a/v0.5/fastn-compiler/src/tdoc.rs b/v0.5/fastn-compiler/src/tdoc.rs index 11dd0d039..e14a0a2c2 100644 --- a/v0.5/fastn-compiler/src/tdoc.rs +++ b/v0.5/fastn-compiler/src/tdoc.rs @@ -1,18 +1,19 @@ pub struct TDoc<'a> { - name: &'a str, - bag: &'a indexmap::IndexMap, + pub name: &'a str, + pub bag: &'a indexmap::IndexMap, + pub default_bag: &'a indexmap::IndexMap, } impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> { fn get_opt_function(&self, name: &str) -> Option { - match self.bag.get(name) { + match self.bag.get(name).or_else(|| self.default_bag.get(name)) { Some(fastn_resolved::Definition::Function(f)) => Some(f.clone()), _ => None, } } fn get_opt_record(&self, name: &str) -> Option { - match self.bag.get(name) { + match self.bag.get(name).or_else(|| self.default_bag.get(name)) { Some(fastn_resolved::Definition::Record(f)) => Some(f.clone()), _ => None, } @@ -23,14 +24,14 @@ impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> { } fn get_opt_component(&self, name: &str) -> Option { - match self.bag.get(name) { + match self.bag.get(name).or_else(|| self.default_bag.get(name)) { Some(fastn_resolved::Definition::Component(f)) => Some(f.clone()), _ => None, } } fn get_opt_web_component(&self, name: &str) -> Option { - match self.bag.get(name) { + match self.bag.get(name).or_else(|| self.default_bag.get(name)) { Some(fastn_resolved::Definition::WebComponent(f)) => Some(f.clone()), _ => None, }