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
string
required
The component source code to preprocess
PreprocessorGroup | PreprocessorGroup[]
required
A preprocessor group or array of preprocessor groups. Each group can have
markup, script, and style hooks.string
The filename of the component (used for debugging and source maps)
Return Value
Returns aPromise<Processed> with:
string
The transformed source code
string | object
A source map mapping back to the original code
string[]
Array of additional files to watch for changes
Preprocessor Hooks
markup
Processes the entire component source.MarkupPreprocessor
script
Processes the content of<script> tags.
Preprocessor
style
Processes the content of<style> tags.
Preprocessor
Same signature as
script preprocessorProcessed Return Type
Each preprocessor hook can return:string
required
The transformed code
string | object
A source map
string[]
Files to watch for changes
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
Related
- compile() - Compile preprocessed source
- Compiler Options - Compiler configuration