Skip to main content
The preprocess() function provides hooks for transforming component source code before compilation. Use it to add support for TypeScript, PostCSS, SCSS, and other preprocessors.

Signature

Parameters

source
string
required
The component source code to preprocess
preprocessor
PreprocessorGroup | PreprocessorGroup[]
required
A preprocessor group or array of preprocessor groups. Each group can have markup, script, and style hooks.
options.filename
string
The filename of the component (used for debugging and source maps)

Return Value

Returns a Promise<Processed> with:
code
string
The transformed source code
map
string | object
A source map mapping back to the original code
dependencies
string[]
Array of additional files to watch for changes

Preprocessor Hooks

markup

Processes the entire component source.
markup
MarkupPreprocessor

script

Processes the content of <script> tags.
script
Preprocessor

style

Processes the content of <style> tags.
style
Preprocessor
Same signature as script preprocessor

Processed Return Type

Each preprocessor hook can return:
code
string
required
The transformed code
map
string | object
A source map
dependencies
string[]
Files to watch for changes
attributes
Record<string, string | boolean>
(script/style only) Updated attributes for the tag

Usage Examples

TypeScript Preprocessor

SCSS Preprocessor

Multiple Preprocessors

Markup Preprocessor

Using svelte-preprocess

With Vite

Watching Dependencies