Crate fervid

Source
Expand description

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);

Modules§

error
errors
Error definitions for the glue code of fervid
parser_oldDeprecated

Macros§

fervid_atom

Structs§

CompileEmittedAsset
CompileEmittedStyle
CompileOptions
CompileResult
Conditional
A wrapper around an ElementNode with a condition attached to it. This is used in v-if and v-else-if nodes.
ConditionalNodeSequence
This is a synthetic node type only available after AST optimizations. Its purpose is to make conditional code generation trivial.
ElementNode
Element node is a classic HTML node with some added functionality:
Interpolation
A special Vue {{ expression }}, which would be rendered as a stringified value of executing said expression.
PatchHints
A helper structure attached to ElementNodes to handle Patch Flags and contain the list of dynamic props.
SfcCustomBlock
SfcDescriptor
SfcScriptBlock
SfcStyleBlock
SfcTemplateBlock
StartingTag
Starting tag represents ElementNode’s tag name and attributes
VBindDirective
v-bind and its shorthand :
VCustomDirective
A custom directive defined by a user.
VForDirective
v-for
VModelDirective
v-model
VOnDirective
v-on and its shorthand @
VSlotDirective
v-slot
VueDirectives
A structure which stores all the Vue directives of an ElementNode.

Enums§

AttributeOrBinding
Denotes the basic attributes or bindings of a DOM element As of directives, this only covers v-bind and v-on, because they bind something to DOM. v-model is not covered here because its code generation is not as trivial.
BindingTypes
The type of a binding (or identifier) which is used to show where this binding came from, e.g. Data is for Options API data(), SetupRef if for refs and computeds in Composition API.
BuiltinType
ComponentBinding
A binding of a component which was found in the template
CustomDirectiveBinding
A binding of a directive which was found in the template
ElementKind
Node
A Node represents a part of the Abstract Syntax Tree (AST).
PatchFlags
From https://github.com/vuejs/core/blob/b8fc18c0b23be9a77b05dc41ed452a87a0becf82/packages/shared/src/patchFlags.ts
SfcScriptLang
StrOrExpr
Describes a type which can be either a static &str or a js Expr. This is mostly usable for dynamic binding scenarios.
TemplateGenerationMode
Mode with which the template is attached to the exported SFC object.
VueImports

Statics§

VUE_BUILTINS

Functions§

check_attribute_name
Checks whether the attributes name is the same as expected_name
compile
A more general-purpose SFC compilation function. Not production-ready yet.
compile_sync_naive
Naive implementation of the SFC compilation, meaning that:
is_from_default_slot
Checks whether a Node is from the component’s default slot or not
is_html_tag

Type Aliases§

FervidAtom
PatchFlagsSet
VueImportsSet