3 releases

0.0.3 Nov 27, 2023
0.0.2 Apr 20, 2023
0.0.1 Mar 27, 2023

#258 in Template engine

Download history 7/week @ 2024-02-18 40/week @ 2024-02-25 3/week @ 2024-03-03 6/week @ 2024-03-10 1/week @ 2024-03-17 58/week @ 2024-03-31 4/week @ 2024-04-07

62 downloads per month
Used in fervid_napi

Apache-2.0

39KB
923 lines

The main public crate of the fervid project.

Here's how you can use fervid to generate a module from an SFC:

Warning: This example is very likely to change in the future. Please note that fervid is still unstable.

use swc_core::ecma::ast::Expr;

let input = r#"
  <template><div>hello world</div></template>
"#;

// Parse
let (remaining_input, sfc) = fervid::parser::core::parse_sfc(input).unwrap();

// Do the necessary transformations
let is_prod = true;
let transform_result = fervid_transform::transform_sfc(sfc, is_prod);

// Create the context and generate the template block
let mut ctx = fervid_codegen::CodegenContext::with_bindings_helper(transform_result.bindings_helper);

let template_expr: Option<Expr> = transform_result.template_block.map(|template_block| {
    ctx.generate_sfc_template(&template_block)
});

// Generate the module code
let sfc_module = ctx.generate_module(
    template_expr,
    transform_result.module,
    transform_result.exported_obj,
    transform_result.setup_fn,
);

// (Optional) Stringify the code
let compiled_code = fervid_codegen::CodegenContext::stringify(input, &sfc_module, false);

Dependencies

~14MB
~293K SLoC