diff --git a/Cargo.lock b/Cargo.lock index 39f8c38..4304df1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -512,6 +512,7 @@ name = "fervid_transform" version = "0.0.1" dependencies = [ "fervid_core", + "fxhash", "lazy_static", "phf 0.11.2", "smallvec", diff --git a/crates/fervid/src/lib.rs b/crates/fervid/src/lib.rs index d563ffc..2b3496d 100644 --- a/crates/fervid/src/lib.rs +++ b/crates/fervid/src/lib.rs @@ -42,10 +42,7 @@ pub mod parser; use fervid_codegen::CodegenContext; pub use fervid_core::*; -use fervid_transform::{ - script::transform_and_record_scripts, structs::ScopeHelper, - template::transform_and_record_template, -}; +use fervid_transform::transform_sfc; use swc_core::ecma::ast::Expr; /// Naive implementation of the SFC compilation, meaning that: @@ -60,28 +57,25 @@ pub fn compile_sync_naive(source: &str) -> Result { // })?; let mut errors = Vec::new(); - let mut sfc = fervid_parser::parse_sfc(&source, &mut errors).map_err(|err| { + let sfc = fervid_parser::parse_sfc(&source, &mut errors).map_err(|err| { return err.to_string(); })?; - let mut scope_helper = ScopeHelper::default(); - let transform_result = - transform_and_record_scripts(sfc.script_setup, sfc.script_legacy, &mut scope_helper); + // TODO Return template used variables as a part of transformation result. + // Also `used_imports`? `vue_imports`? User imports? + let transform_result = transform_sfc(sfc); let mut ctx = CodegenContext::default(); + ctx.used_imports = transform_result.used_vue_imports; - let template_expr: Option = sfc.template.as_mut().map(|template_block| { - transform_and_record_template(template_block, &mut scope_helper); + let template_expr: Option = transform_result.template_block.map(|template_block| { ctx.generate_sfc_template(&template_block) }); - // Add imports from script transformation (because macros generate new imports) - ctx.used_imports |= transform_result.added_imports; - let sfc_module = ctx.generate_module( template_expr, transform_result.module, - transform_result.export_obj, + transform_result.exported_obj, transform_result.setup_fn, ); diff --git a/crates/fervid_transform/Cargo.toml b/crates/fervid_transform/Cargo.toml index 0fb34bc..d7541e6 100644 --- a/crates/fervid_transform/Cargo.toml +++ b/crates/fervid_transform/Cargo.toml @@ -11,6 +11,7 @@ license = "Apache-2.0" [dependencies] fervid_core = { path="../fervid_core", version = "0.0.1" } +fxhash = "0.2.1" lazy_static = "1.4.0" phf = { version = "0.11", features = ["macros"] } swc_core = { version = "0.83.*", features = ["common", "ecma_ast", "ecma_visit"] } diff --git a/crates/fervid_transform/src/lib.rs b/crates/fervid_transform/src/lib.rs index dc874be..ab6c2fa 100644 --- a/crates/fervid_transform/src/lib.rs +++ b/crates/fervid_transform/src/lib.rs @@ -1,3 +1,10 @@ +use fervid_core::{SfcDescriptor, SfcTemplateBlock, VueImportsSet, BindingTypes, FervidAtom}; +use fxhash::FxHashMap as HashMap; +use script::transform_and_record_scripts; +use structs::ScopeHelper; +use swc_core::ecma::ast::{Module, ObjectLit, Function}; +use template::transform_and_record_template; + #[macro_use] extern crate lazy_static; @@ -8,3 +15,46 @@ pub mod template; #[cfg(test)] mod test_utils; + +pub struct TransformSfcResult { + /// Object exported from the `Module`, but detached from it + pub exported_obj: ObjectLit, + /// Module obtained by processing `