4 releases

0.2.0 Jul 7, 2024
0.0.3 Nov 27, 2023
0.0.2 Apr 20, 2023
0.0.1 Mar 27, 2023

#1408 in Development tools

Download history 32/week @ 2024-07-23 33/week @ 2024-07-30 4/week @ 2024-08-20 14/week @ 2024-08-27 20/week @ 2024-09-03 17/week @ 2024-09-10 13/week @ 2024-09-17 48/week @ 2024-09-24 5/week @ 2024-10-01 6/week @ 2024-10-08 22/week @ 2024-10-15 19/week @ 2024-10-22 12/week @ 2024-10-29 4/week @ 2024-11-05

63 downloads per month
Used in 2 crates

Apache-2.0

46KB
1K SLoC

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::{common::FileName, ecma::ast::Expr};

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

// Parse
let mut parse_errors = vec![];
let mut parser = fervid_parser::SfcParser::new(input, &mut parse_errors);
let sfc = parser.parse_sfc().unwrap();

// Do the necessary transformations
let mut transform_errors = Vec::new();
let transform_options = fervid_transform::TransformSfcOptions {
  is_prod: true,
  scope_id: "filehash",
  filename: "input.vue"
};
let transform_result = fervid_transform::transform_sfc(sfc, transform_options, &mut transform_errors);

// 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.and_then(|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,
    None,
);

// (Optional) Stringify the code
let compiled_code = fervid_codegen::CodegenContext::stringify(input, &sfc_module, FileName::Custom("input.vue".into()), false, false);

Dependencies

~19–29MB
~480K SLoC