Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork81
Compile Svelte components with Rollup
License
sveltejs/rollup-plugin-svelte
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Undecided yet what bundler to use? We suggest usingSvelteKit or Vite withvite-plugin-svelte.
Compile Svelte components.
npm install --save-dev svelte rollup-plugin-svelte
Note that we need to install Svelte as well as the plugin, as it's a 'peer dependency'.
// rollup.config.jsimportsveltefrom'rollup-plugin-svelte';importresolvefrom'@rollup/plugin-node-resolve';exportdefault{input:'src/main.js',output:{file:'public/bundle.js',format:'iife'},plugins:[svelte({// By default, all ".svelte" files are compiledextensions:['.my-custom-extension'],// You can restrict which files are compiled// using `include` and `exclude`include:'src/components/**/*.svelte',// Optionally, preprocess components with svelte.preprocess:// https://svelte.dev/docs#compile-time-svelte-preprocesspreprocess:{style:({ content})=>{returntransformStyles(content);}},// Emit CSS as "files" for other plugins to process. default is trueemitCss:false,// Warnings are normally passed straight to Rollup. You can// optionally handle them here, for example to squelch// warnings with a particular codeonwarn:(warning,handler)=>{// e.g. don't warn on <marquee> elements, cos they're coolif(warning.code==='a11y-distracting-elements')return;// let Rollup handle all other warnings normallyhandler(warning);},// You can pass any of the Svelte compiler optionscompilerOptions:{// By default, the client-side compiler is used. You// can also use the server-side rendering compilergenerate:'ssr',// ensure that extra attributes are added to head// elements for hydration (used with generate: 'ssr')hydratable:true,// You can optionally set 'customElement' to 'true' to compile// your components to custom elements (aka web elements)customElement:false}}),// see NOTICE belowresolve({browser:true,exportConditions:['svelte'],extensions:['.svelte']}),// ...]}
NOTICE: You will need additional Rollup plugins.
Alone, this plugin translates Svelte components into CSS and JavaScript files.
You will need to include@rollup/plugin-node-resolve
– and probably@rollup/plugin-commonjs
– in your Rollup config.
If you are using thepreprocess
feature, then your callback responses may — in addition to thecode
andmap
values described in the Svelte compile docs — also optionally include adependencies
array. This should be the paths of additional files that the preprocessor result in some way depends upon. In Rollup 0.61+ in watch mode, any changes to these additional files will also trigger re-builds.
If you're importing a component from your node_modules folder, and that component'spackage.json
has a"svelte"
property in itsexports
condition...
{"name":"some-component",// this means 'some-component' resolves to 'some-component/src/SomeComponent.svelte'"exports":{".":{"svelte":"./src/MyComponent.svelte"}}}
...then this plugin together with@rollup/plugin-node-resolve
(and itsexportConditions
option containing the'svelte'
condition – see configuration example above) will ensure that your app imports theuncompiled component source code. That will result in a smaller, faster app (because code is deduplicated, and shared functions get optimized quicker), and makes it less likely that you'll run into bugs caused by your app using a different version of Svelte to the component.
Conversely, if you'republishing a component to npm, you should ship the uncompiled source (together with the compiled distributable, for people who aren't using Svelte elsewhere in their app) and include the"svelte"
property in theexports
of yourpackage.json
.
If you are publishing a package containing multiple components, you can create anindex.js
file that re-exports all the components, like this:
export{defaultasComponent1}from'./Component1.svelte';export{defaultasComponent2}from'./Component2.svelte';
and so on. Then, inpackage.json
, set thesvelte
condition to point to thisindex.js
file. Or you may create an export for each individual Svelte file. Using a singleindex.js
which exports all files will allow multiple components to be imported with a single line, but may load more slowly during development. An export per file may load more quickly during development but require a separate import statement for each file.
By default (whenemitCss: true
) the CSS styles will be emitted into a virtual file, allowing another Rollup plugin – for example,rollup-plugin-css-only
,rollup-plugin-postcss
, etc. – to take responsibility for the new stylesheet. In fact, emitting CSS filesrequires that you use a Rollup plugin to handle the CSS. Otherwise, your build(s) will fail! This is because this plugin will add animport
statement to import the emitted CSS file. It's not valid JS to import a CSS file into a JS file, but it allows the CSS to be linked to its respective JS file and is a common pattern that other Rollup CSS plugins know how to handle.
If you setemitCss: false
and your Svelte components contain<style>
tags, the compiler will add JavaScript that injects those styles into the page when the component is rendered. That's not the default, because it adds weight to your JavaScript, prevents styles from being fetched in parallel with your code, and can even cause CSP violations.
MIT
About
Compile Svelte components with Rollup
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.