Skip to main content
The 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 a CompileResult object with the following properties:
object
The compiled JavaScript
string
The generated JavaScript code
SourceMap
A source map for the JavaScript
object | null
The compiled CSS (null if no styles)
string
The generated CSS code
SourceMap
A source map for the CSS
boolean
Whether the CSS includes global rules
Warning[]
Array of warning objects generated during compilation. Each warning has:
  • code: A string identifying the category of warning
  • message: Human-readable description
  • start and end: Location objects with line, column, and character properties (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