Skip to main content

Crate svelte_compiler

Crate svelte_compiler 

Source
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§

ast
cst

Structs§

BytePos
A byte offset into source text, stored as a u32.
CompatibilityOptions
CompileError
An error produced during parsing.
CompileMetadata
Extra metadata produced during compilation.
CompileOptions
Options for compiling a .svelte component or rune-enabled module.
CompileResult
Result of compiling a component or module.
Compiler
Convenience object that mirrors the free compiler functions.
CssHashGetterCallback
Callback used to override Svelte’s generated CSS hash.
CssHashInput
Input passed to a custom CSS hash callback.
ExperimentalOptions
Experimental compiler switches.
LineColumn
A line/column location in source text.
MigrateOptions
Options for best-effort code migration.
MigrateResult
Result of a migration run.
OutputArtifact
Generated output artifact such as JavaScript or CSS.
ParseOptions
Options for parsing a component without compiling it.
PreprocessAttribute
One parsed attribute passed to a tag preprocessor.
PreprocessMarkup
Input passed to a markup preprocessor.
PreprocessOptions
Options for running preprocessors over component source code.
PreprocessOutput
Output returned by one preprocessor step.
PreprocessResult
Final result of a preprocessing run.
PreprocessTag
Input passed to a script or style preprocessor.
PreprocessorGroup
Collection of preprocessors applied in source order.
PrintOptions
Options for converting a parsed AST back into Svelte source.
PrintedOutput
Result of printing a Svelte AST node.
SourceId
An opaque identifier for a source file, used to distinguish multiple inputs during batch processing.
SourceMap
Sourcemap configuration accepted by the compiler.
SourcePosition
A byte range in source text, used by CompileError to indicate where the error occurred.
SourceText
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.
WarningFilterCallback
Callback used to filter warnings during compilation.

Enums§

CompatibilityComponentApi
CssOutputMode
DiagnosticKind
Diagnostic codes for Svelte-specific parse and validation errors.
ErrorMode
FragmentStrategy
GenerateTarget
ModernPrintTarget
Selects which modern AST node should be printed.
Namespace
ParseMode
Selects which public AST representation parse returns.
PreprocessAttributeValue
Attribute values passed to preprocessors.

Statics§

VERSION
Current Svelte compiler version string.

Functions§

compile
Compile a .svelte component 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
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 walk export.

Type Aliases§

AsyncMarkupPreprocessor
AsyncTagPreprocessor
MarkupPreprocessor
PreprocessAttributes
TagPreprocessor