Typically, you won't interact with the Svelte compiler directly, but will instead integrate it into your build system using a bundler plugin. The bundler plugin that the Svelte team most recommends and invests in is vite-plugin-svelte. The SvelteKit framework provides a setup leveraging vite-plugin-svelte
to build applications as well as a tool for packaging Svelte component libraries. Svelte Society maintains a list of other bundler plugins for additional tools like Rollup and Webpack.
Nonetheless, it's useful to understand how to use the compiler, since bundler plugins generally expose compiler options to you.
compilepermalink
ts
function compile(source: string,options: CompileOptions): CompileResult;
This is where the magic happens. svelte.compile
takes your component source code, and turns it into a JavaScript module that exports a class.
ts
import {compile } from 'svelte/compiler';constresult =compile (source , {// options});
Refer to CompileOptions for all the available options.
The returned result
object contains the code for your component, along with useful bits of metadata.
ts
const {js ,css ,ast ,warnings ,vars ,stats } =compile (source );
Refer to CompileResult for a full description of the compile result.
parsepermalink
ts
function parse(source: string,options?:| {filename?: string | undefined;modern?: boolean | undefined;}| undefined): SvelteNode | LegacySvelteNode;
The parse
function parses a component, returning only its abstract syntax tree. Unlike compiling with the generate: false
option, this will not perform any validation or other analysis of the component beyond parsing it. Note that the returned AST is not considered public API, so breaking changes could occur at any point in time.
ts
import {parse } from 'svelte/compiler';constast =parse (source , {filename : 'App.svelte' });
preprocesspermalink
ts
function preprocess(source: string,preprocessor: PreprocessorGroup | PreprocessorGroup[],options?:| {filename?: string | undefined;}| undefined): Promise<Processed>;
A number of official and 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. For example, it can be used to convert a <style lang="sass">
block into vanilla CSS.
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 a name
which is required, and markup
, script
and style
functions, each of which is optional.
The markup
function receives the entire component source text, along with the component's filename
if it was specified in the third argument.
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.
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. Optionally they can return an array of dependencies
which represents files to watch for changes, and a map
object which is a sourcemap mapping back the transformation to the original code. script
and style
preprocessors can optionally return a record of attributes which represent the updated attributes on the script/style tag.
Preprocessor functions should return a
map
object whenever possible or else debugging becomes harder as stack traces can't link to the original code correctly.
ts
import {preprocess } from 'svelte/compiler';importMagicString from 'magic-string';const {code } = awaitpreprocess (source ,{markup : ({content ,filename }) => {constpos =content .indexOf ('foo');if (pos < 0) {return {code :content };}consts = newMagicString (content , {filename });s .overwrite (pos ,pos + 3, 'bar', {storeName : true });return {code :s .toString (),map :s .generateMap ()};}},{filename : 'App.svelte'});
If a dependencies
array is returned, it will be included in the result object. This is used by packages like vite-plugin-svelte and rollup-plugin-svelte to watch additional files for changes, in the case where your <style>
tag has an @import
(for example).
ts
import {preprocess } from 'svelte/compiler';importMagicString from 'magic-string';importsass from 'sass';import {dirname } from 'path';const {code } = awaitpreprocess (source ,{name : 'my-fancy-preprocessor',markup : ({content ,filename }) => {// Return code as is when no foo string presentconstpos =content .indexOf ('foo');if (pos < 0) {return;}// Replace foo with bar using MagicString which provides// a source map along with the changed codeconsts = newMagicString (content , {filename });Property 'css' does not exist on type 'unknown'.Property 'stats' does not exist on type 'unknown'.2339s .overwrite ( pos ,pos + 3, 'bar', {storeName : true });
2339Property 'css' does not exist on type 'unknown'.Property 'stats' does not exist on type 'unknown'.return {code :s .toString (),map :s .generateMap ({hires : true,file :filename })};Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.2345Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'.},style : async ({content ,attributes ,filename }) => {// only process <style lang="sass">if (attributes .lang !== 'sass') return;const {css ,stats } = await newPromise ((resolve ,reject ) =>sass .render ({file :filename ,data :content ,includePaths : [dirname (filename )]},(err ,result ) => {if (err )reject (err );elseresolve (result );}));// remove lang attribute from style tagdeleteattributes .lang ;return {code :css .toString (),dependencies :stats .includedFiles ,attributes };}},{filename : 'App.svelte'});
Multiple preprocessors can be used together. The output of the first becomes the input to the second. Within one preprocessor, markup
runs first, then script
and style
.
In Svelte 3, all
markup
functions ran first, then allscript
and then allstyle
preprocessors. This order was changed in Svelte 4.
ts
import {preprocess } from 'svelte/compiler';const {code } = awaitpreprocess (source , [{name : 'first preprocessor',markup : () => {console .log ('this runs first');},script : () => {console .log ('this runs second');},style : () => {console .log ('this runs third');}},{name : 'second preprocessor',markup : () => {console .log ('this runs fourth');},script : () => {console .log ('this runs fifth');},style : () => {console .log ('this runs sixth');}}], {filename : 'App.svelte'});
ts
import {preprocess } from 'svelte/compiler';const {code } = awaitpreprocess (source ,[{name : 'first preprocessor',markup : () => {console .log ('this runs first');},script : () => {console .log ('this runs second');},style : () => {console .log ('this runs third');},},{name : 'second preprocessor',markup : () => {console .log ('this runs fourth');},script : () => {console .log ('this runs fifth');},style : () => {console .log ('this runs sixth');},},],{filename : 'App.svelte',},);
walkpermalink
The walk
function provides a way to walk the abstract syntax trees generated by the parser, using the compiler's own built-in instance of estree-walker.
The walker takes an abstract syntax tree to walk and an object with two optional methods: enter
and leave
. For each node, enter
is called (if present). Then, unless this.skip()
is called during enter
, each of the children are traversed, and then leave
is called on the node.
ts
import {walk } from 'svelte/compiler';walk (ast , {enter (node ,parent ,prop ,index ) {do_something (node );if (should_skip_children (node )) {this.skip ();}},leave (node ,parent ,prop ,index ) {do_something_else (node );}});
ts
import {walk } from 'svelte/compiler';walk (ast , {enter (node ,parent ,prop ,index ) {do_something (node );if (should_skip_children (node )) {this.skip ();}},leave (node ,parent ,prop ,index ) {do_something_else (node );},});
VERSIONpermalink
ts
const VERSION: string;
The current version, as set in package.json.
ts
import {VERSION } from 'svelte/compiler';console .log (`running svelte version ${VERSION }`);
Typespermalink
CompileErrorpermalink
ts
class CompileError extends Error {…}
ts
constructor(code: string, message: string, position: [number, number] | undefined);
ts
filename: CompileError_1['filename'];
ts
position: CompileError_1['position'];
ts
start: CompileError_1['start'];
ts
end: CompileError_1['end'];
ts
code: string;