compile() function converts your .svelte source code into a JavaScript module that exports a component.
Signature
Parameters
string
required
The component source code
CompileOptions
required
Compiler options. See Compiler Options for details.
Return Value
Returns aCompileResult object with the following properties:
object
object | null
Warning[]
Array of warning objects generated during compilation. Each warning has:
code: A string identifying the category of warningmessage: Human-readable descriptionstartandend: Location objects withline,column, andcharacterproperties (if applicable)
object
Metadata about the compiled component
boolean
Whether the file was compiled in runes mode, either because of an explicit option or inferred from usage
AST.Root
The abstract syntax tree of the component
Usage Examples
Basic Usage
With Vite Plugin
With Rollup
Server-Side Rendering
Custom Element
Related
- Compiler Options - Full list of compiler options
- preprocess() - Transform component source before compilation
- migrate() - Migrate Svelte 4 code to Svelte 5