Expand description
Svelte compiler APIs for parsing, printing, preprocessing, migration, and JavaScript/CSS code generation.
§Examples
Compile a component:
use svelte_compiler::{CompileOptions, compile};
let result = compile(
"<script>let name = 'world';</script><h1>Hello {name}</h1>",
CompileOptions::default(),
)?;
assert!(result.js.code.contains("Hello"));Parse and print a modern AST:
use svelte_compiler::{ModernPrintTarget, ParseMode, ParseOptions, PrintOptions, parse, print_modern};
let document = parse(
"<button class='primary'>save</button>",
ParseOptions {
mode: ParseMode::Modern,
..ParseOptions::default()
},
)?;
let root = match &document.root {
svelte_compiler::ast::Root::Modern(root) => root,
svelte_compiler::ast::Root::Legacy(_) => unreachable!("requested a modern AST"),
};
let printed = print_modern(ModernPrintTarget::root(document.source(), root), PrintOptions::default())?;
assert!(printed.code.contains("<button"));Modules§
Structs§
- BytePos
- A byte offset into source text, stored as a
u32. - Compatibility
Options - Compile
Error - An error produced during parsing.
- Compile
Metadata - Extra metadata produced during compilation.
- Compile
Options - Options for compiling a
.sveltecomponent or rune-enabled module. - Compile
Result - Result of compiling a component or module.
- Compiler
- Convenience object that mirrors the free compiler functions.
- CssHash
Getter Callback - Callback used to override Svelte’s generated CSS hash.
- CssHash
Input - Input passed to a custom CSS hash callback.
- Experimental
Options - Experimental compiler switches.
- Line
Column - A line/column location in source text.
- Migrate
Options - Options for best-effort code migration.
- Migrate
Result - Result of a migration run.
- Output
Artifact - Generated output artifact such as JavaScript or CSS.
- Parse
Options - Options for parsing a component without compiling it.
- Preprocess
Attribute - One parsed attribute passed to a tag preprocessor.
- Preprocess
Markup - Input passed to a markup preprocessor.
- Preprocess
Options - Options for running preprocessors over component source code.
- Preprocess
Output - Output returned by one preprocessor step.
- Preprocess
Result - Final result of a preprocessing run.
- Preprocess
Tag - Input passed to a script or style preprocessor.
- Preprocessor
Group - Collection of preprocessors applied in source order.
- Print
Options - Options for converting a parsed AST back into Svelte source.
- Printed
Output - Result of printing a Svelte AST node.
- Source
Id - An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
- Source
Map - Sourcemap configuration accepted by the compiler.
- Source
Position - A byte range in source text, used by
CompileErrorto indicate where the error occurred. - Source
Text - Borrowed source text with an identifier and optional filename.
- Span
- A half-open byte range (
start..end) in source text. - Warning
- Warning emitted during analysis or code generation.
- Warning
Filter Callback - Callback used to filter warnings during compilation.
Enums§
- Compatibility
Component Api - CssOutput
Mode - Diagnostic
Kind - Diagnostic codes for Svelte-specific parse and validation errors.
- Error
Mode - Fragment
Strategy - Generate
Target - Modern
Print Target - Selects which modern AST node should be printed.
- Namespace
- Parse
Mode - Selects which public AST representation
parsereturns. - Preprocess
Attribute Value - Attribute values passed to preprocessors.
Statics§
- VERSION
- Current Svelte compiler version string.
Functions§
- compile
- Compile a
.sveltecomponent into JavaScript and optional CSS artifacts. - compile_
module - Compile a JavaScript or TypeScript module that uses runes.
- migrate
- Attempt a best-effort migration of a component to modern Svelte syntax.
- parse
- Parse a component into the public Svelte AST.
- parse_
css - Parse a stylesheet into the public CSS AST.
- parse_
svelte - Parse Svelte source into a tree-sitter CST document.
- preprocess
- Run one or more preprocessors over a component source string.
- preprocess_
async - Run one or more preprocessors over a component source string asynchronously.
- Print a parsed document back to Svelte source.
- print_
modern - Print a node from the modern AST back to Svelte source.
- walk
- Compatibility stub for the old JavaScript
walkexport.