Compiler
Preprocess
A number of community-maintained preprocessing plugins are available to allow you to use Svelte with tools like TypeScript, PostCSS, SCSS, and Less.
You can write your own preprocessor using the svelte.preprocess
API.
The preprocess
function provides convenient hooks for arbitrarily transforming component source
code.
The first argument is the component source code. The second is an array of preprocessors (or a single preprocessor, if you only have one), where a preprocessor is an object with markup
, script
and style
functions, each of which is optional.
Each markup
, script
or style
function must return an object (or a Promise that resolves to an object) with a code
property, representing the transformed source code, and an optional array of dependencies
.
The markup
function receives the entire component source text, along with the component's filename
if it was specified in the third argument.
Preprocessor functions should additionally return a
map
object alongsidecode
anddependencies
, wheremap
is a sourcemap representing the transformation.
The script
and style
functions receive the contents of <script>
and <style>
elements respectively (content
) as well as the entire component source text (markup
). In addition to filename
, they get an object of the element's attributes.
If a dependencies
array is returned, it will be included in the result object. This is used by packages like rollup-plugin-svelte to watch additional files for changes, in the case where your <style>
tag has an @import
(for example).
Multiple preprocessors can be used together. The output of the first becomes the input to the second. markup
functions run first, then script
and style
.