UNPKG

6.38 kBMarkdownView Raw
1> Undecided yet what bundler to use? We suggest using [SvelteKit](https://kit.svelte.dev) or Vite with [vite-plugin-svelte](https://github.com/sveltejs/vite-plugin-svelte/).
2
3# rollup-plugin-svelte [![CI](https://github.com/sveltejs/rollup-plugin-svelte/workflows/CI/badge.svg)](https://github.com/sveltejs/rollup-plugin-svelte/actions)
4
5Compile Svelte components.
6
7
8## Installation
9
10```bash
11npm install --save-dev svelte rollup-plugin-svelte
12```
13
14Note that we need to install Svelte as well as the plugin, as it's a 'peer dependency'.
15
16
17## Usage
18
19```js
20// rollup.config.js
21import svelte from 'rollup-plugin-svelte';
22import resolve from '@rollup/plugin-node-resolve';
23
24export default {
25 input: 'src/main.js',
26 output: {
27 file: 'public/bundle.js',
28 format: 'iife'
29 },
30 plugins: [
31 svelte({
32 // By default, all ".svelte" files are compiled
33 extensions: ['.my-custom-extension'],
34
35 // You can restrict which files are compiled
36 // using `include` and `exclude`
37 include: 'src/components/**/*.svelte',
38
39 // Optionally, preprocess components with svelte.preprocess:
40 // https://svelte.dev/docs#compile-time-svelte-preprocess
41 preprocess: {
42 style: ({ content }) => {
43 return transformStyles(content);
44 }
45 },
46
47 // Emit CSS as "files" for other plugins to process. default is true
48 emitCss: false,
49
50 // Warnings are normally passed straight to Rollup. You can
51 // optionally handle them here, for example to squelch
52 // warnings with a particular code
53 onwarn: (warning, handler) => {
54 // e.g. don't warn on <marquee> elements, cos they're cool
55 if (warning.code === 'a11y-distracting-elements') return;
56
57 // let Rollup handle all other warnings normally
58 handler(warning);
59 },
60
61 // You can pass any of the Svelte compiler options
62 compilerOptions: {
63
64 // By default, the client-side compiler is used. You
65 // can also use the server-side rendering compiler
66 generate: 'ssr',
67
68 // ensure that extra attributes are added to head
69 // elements for hydration (used with generate: 'ssr')
70 hydratable: true,
71
72 // You can optionally set 'customElement' to 'true' to compile
73 // your components to custom elements (aka web elements)
74 customElement: false
75 }
76 }),
77 // see NOTICE below
78 resolve({
79 browser: true,
80 exportConditions: ['svelte'],
81 extensions: ['.svelte']
82 }),
83 // ...
84 ]
85}
86```
87
88> **NOTICE:** You will need additional Rollup plugins. <br>Alone, this plugin translates Svelte components into CSS and JavaScript files. <br>You will need to include [`@rollup/plugin-node-resolve`](https://www.npmjs.com/package/@rollup/plugin-node-resolve) – and probably [`@rollup/plugin-commonjs`](https://www.npmjs.com/package/@rollup/plugin-commonjs) – in your Rollup config.
89
90
91## Preprocessing and dependencies
92
93If you are using the `preprocess` feature, then your callback responses may — in addition to the `code` and `map` values described in the Svelte compile docs — also optionally include a `dependencies` 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.
94
95
96## `svelte` exports condition
97
98If you're importing a component from your node_modules folder, and that component's `package.json` has a `"svelte"` property in its `exports` condition...
99
100```js
101{
102 "name": "some-component",
103
104 // this means 'some-component' resolves to 'some-component/src/SomeComponent.svelte'
105 "exports": {
106 ".": {
107 "svelte": "./src/MyComponent.svelte"
108 }
109 }
110}
111```
112
113...then this plugin together with `@rollup/plugin-node-resolve` (and its `exportConditions` option containing the `'svelte'` condition – see configuration example above) will ensure that your app imports the *uncompiled* 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.
114
115Conversely, if you're *publishing* 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 the `exports` of your `package.json`.
116
117If you are publishing a package containing multiple components, you can create an `index.js` file that re-exports all the components, like this:
118
119```js
120export { default as Component1 } from './Component1.svelte';
121export { default as Component2 } from './Component2.svelte';
122```
123
124and so on. Then, in `package.json`, set the `svelte` condition to point to this `index.js` file. Or you may create an export for each individual Svelte file. Using a single `index.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.
125
126
127## Extracting CSS
128
129By default (when `emitCss: true`) the CSS styles will be emitted into a virtual file, allowing another Rollup plugin – for example, [`rollup-plugin-css-only`](https://www.npmjs.com/package/rollup-plugin-css-only), [`rollup-plugin-postcss`](https://www.npmjs.com/package/rollup-plugin-postcss), etc. – to take responsibility for the new stylesheet. In fact, emitting CSS files _requires_ that you use a Rollup plugin to handle the CSS. Otherwise, your build(s) will fail! This is because this plugin will add an `import` 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.
130
131If you set `emitCss: 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.
132
133## License
134
135MIT