• Stars
    star
    500
  • Rank 88,178 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compile Svelte components with Rollup

Undecided yet what bundler to use? We suggest using SvelteKit or Vite with vite-plugin-svelte.

rollup-plugin-svelte CI

Compile Svelte components.

Installation

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'.

Usage

// rollup.config.js
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: {
    file: 'public/bundle.js',
    format: 'iife'
  },
  plugins: [
    svelte({
      // By default, all ".svelte" files are compiled
      extensions: ['.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-preprocess
      preprocess: {
        style: ({ content }) => {
          return transformStyles(content);
        }
      },

      // Emit CSS as "files" for other plugins to process. default is true
      emitCss: false,

      // Warnings are normally passed straight to Rollup. You can
      // optionally handle them here, for example to squelch
      // warnings with a particular code
      onwarn: (warning, handler) => {
        // e.g. don't warn on <marquee> elements, cos they're cool
        if (warning.code === 'a11y-distracting-elements') return;

        // let Rollup handle all other warnings normally
        handler(warning);
      },

      // You can pass any of the Svelte compiler options
      compilerOptions: {

        // By default, the client-side compiler is used. You
        // can also use the server-side rendering compiler
        generate: '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 below
    resolve({
      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.

Preprocessing and dependencies

If 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.

svelte exports condition

If you're importing a component from your node_modules folder, and that component's package.json has a "svelte" property in its exports 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 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.

Conversely, 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.

If you are publishing a package containing multiple components, you can create an index.js file that re-exports all the components, like this:

export { default as Component1 } from './Component1.svelte';
export { default as Component2 } from './Component2.svelte';

and 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.

Extracting CSS

By default (when emitCss: 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 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.

If 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.

License

MIT

More Repositories

1

svelte

Cybernetically enhanced web apps
JavaScript
77,898
star
2

kit

web development, streamlined
JavaScript
18,346
star
3

sapper

The next small thing in web development, powered by Svelte
TypeScript
7,007
star
4

realworld

SvelteKit implementation of the RealWorld app
Svelte
2,212
star
5

template

Template for building basic applications with Svelte
JavaScript
1,743
star
6

svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
TypeScript
1,743
star
7

svelte-devtools

A browser extension to inspect Svelte application by extending your browser devtools capabilities
Svelte
1,457
star
8

language-tools

The Svelte Language Server, and official extensions which use it
TypeScript
1,199
star
9

vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
JavaScript
838
star
10

prettier-plugin-svelte

Format your svelte components using prettier.
TypeScript
726
star
11

sapper-template

Starter template for Sapper apps
JavaScript
703
star
12

svelte-virtual-list

A virtual list component for Svelte apps
JavaScript
673
star
13

integrations

Ways to incorporate Svelte into your stack
632
star
14

gl

A (very experimental) project to bring WebGL to Svelte
JavaScript
603
star
15

svelte-loader

Webpack loader for svelte components.
JavaScript
596
star
16

community-legacy

Svelte community meetups, packages, resources, recipes, showcase websites, and more
Svelte
563
star
17

component-template

A base for building shareable Svelte components
JavaScript
554
star
18

learn.svelte.dev

A soup-to-nuts interactive tutorial on how to build apps with Svelte
Svelte
522
star
19

eslint-plugin-svelte3

An ESLint plugin for Svelte v3 components.
JavaScript
372
star
20

svelte-scroller

A <Scroller> component for Svelte apps
Svelte
345
star
21

template-webpack

Template for building basic Svelte applications with webpack
JavaScript
303
star
22

sites

Monorepo for the sites in the Svelte ecosystem
Svelte
291
star
23

eslint-plugin-svelte

ESLint plugin for Svelte using AST
TypeScript
287
star
24

svelte-repl

The <Repl> component used on the Svelte website
Svelte
274
star
25

rfcs

RFCs for changes to Svelte
273
star
26

sapper-studio

An electron app for building Sapper projects
HTML
220
star
27

v2.svelte.dev

The Svelte v2 website
HTML
209
star
28

svelte-hmr

HMR commons for Svelte 3
JavaScript
202
star
29

site-kit

Svelte
196
star
30

hn.svelte.dev

Hacker News clone built with Svelte and Sapper
Svelte
164
star
31

svelte-todomvc

TodoMVC implemented in Svelte
Svelte
139
star
32

svelte-subdivide

A component for building Blender-style layouts in Svelte apps
JavaScript
130
star
33

svelte-cli

Command line interface for Svelte
JavaScript
104
star
34

svelte-eslint-parser

Svelte parser for ESLint
TypeScript
92
star
35

gestures

Svelte actions for cross-platform gesture detection
TypeScript
89
star
36

svelte-hackernews

WIP Hacker News clone written in Svelte
JavaScript
78
star
37

svelte-custom-elements

Turn Svelte components into web components
JavaScript
49
star
38

svelte-extras

Extra methods for Svelte components
JavaScript
43
star
39

sapper-template-rollup

Starter Rollup template for Sapper apps
JavaScript
40
star
40

kit-template-default

The default SvelteKit template, generated with create-svelte
Svelte
36
star
41

svelte-upgrade

Upgrade your Svelte templates for version 2
JavaScript
36
star
42

examples

A collection of Svelte(Kit) examples
35
star
43

branding

Logos etc for Svelte and related projects
35
star
44

hn.svelte.technology

Hacker News, built with Sapper
HTML
35
star
45

svelte-transitions

Officially supported transition plugins for Svelte
JavaScript
33
star
46

sapper-template-webpack

Starter webpack template for Sapper apps
JavaScript
32
star
47

discord-bot

TypeScript
32
star
48

svelte-atom

Syntax, diagnostics, and other smarts for Svelte in Atom
JavaScript
30
star
49

community

28
star
50

svelte-dbmonster

Svelte implementation of DBMonster
JavaScript
27
star
51

template-custom-element

Template for building basic applications with Svelte and custom elements
HTML
21
star
52

generate-ssr

Server-side rendering for Svelte
JavaScript
21
star
53

kit-sandbox

A sandbox for maintainers
JavaScript
18
star
54

template-store

Demonstrating the use of svelte/store
JavaScript
17
star
55

svelte-transitions-draw

Draw transition plugin for Svelte
JavaScript
16
star
56

api.svelte.dev

The API worker source for https://api.svelte.dev
TypeScript
14
star
57

sapper-legacy.svelte.dev

Old docs site for Sapper
HTML
13
star
58

action-deploy-docs

github action for the svelte org to deploy documentation to the svelte api
TypeScript
12
star
59

svelte-bench

Benchmarks for Svelte
JavaScript
10
star
60

sveltegram

Sapper/Svelte remix of nextgram
JavaScript
8
star
61

svelte-transitions-fade

Fade transition plugin for Svelte
JavaScript
8
star
62

assets

Large static files used on the Svelte website
7
star
63

svelte-transitions-slide

Slide transition plugin for Svelte
JavaScript
5
star
64

svelte-transitions-fly

Fly transition plugin for Svelte
JavaScript
4
star
65

redirects

Redirect old Svelte websites to their shiny new equivalents
JavaScript
3
star