Compile
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.
svelte.compile
This is where the magic happens. svelte.compile
takes your component source code, and turns it into a JavaScript module that exports a class.
The following options can be passed to the compiler. None are required:
The returned result
object contains the code for your component, along with useful bits of metadata.
js
andcss
are objects with the following properties:code
is a JavaScript stringmap
is a sourcemap with additionaltoString()
andtoUrl()
convenience methods
ast
is an abstract syntax tree representing the structure of your component.warnings
is an array of warning objects that were generated during compilation. Each warning has several properties:code
is a string identifying the category of warningmessage
describes the issue in human-readable termsstart
andend
, if the warning relates to a specific location, are objects withline
,column
andcharacter
propertiesframe
, if applicable, is a string highlighting the offending code with line numbers
vars
is an array of the component's declarations, used by eslint-plugin-svelte3 for example. Each variable has several properties:name
is self-explanatoryexport_name
is the name the value is exported as, if it is exported (will matchname
unless you doexport...as
)injected
istrue
if the declaration is injected by Svelte, rather than in the code you wrotemodule
istrue
if the value is declared in acontext="module"
scriptmutated
istrue
if the value's properties are assigned to inside the componentreassigned
istrue
if the value is reassigned inside the componentreferenced
istrue
if the value is used in the templatereferenced_from_script
istrue
if the value is used in the<script>
outside the declarationwritable
istrue
if the value was declared withlet
orvar
(but notconst
,class
orfunction
)
stats
is an object used by the Svelte developer team for diagnosing the compiler. Avoid relying on it to stay the same!