Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rollup-plugin-svelte

sveltejs381.8kMIT7.2.2TypeScript support: included

Compile Svelte components with Rollup

svelte, rollup

readme

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

changelog

rollup-plugin-svelte changelog

7.2.1

  • Silence Svelte 5 version of unused CSS warning code if emitCss is false

7.2.0

  • Support compiling svelte.js/ts files in Svelte 5
  • Prevent crash when css is null in Svelte 5 (#216)

7.1.6

  • Adjust inferred css option for Svelte 4

7.1.5

  • Get ready for Svelte 4

7.1.4

  • Forward options to this.resolve (#208)

7.1.3

  • Warn if svelte export condition is missing (#206)

7.1.2

  • Fix package resolution (#205)

7.1.1

  • Resolve package.json files manually (#198)

7.1.0

  • Preprocessor sourcemap support (#157)

7.0.0

  • New minimum version requirements (#138, #142):
    • Rollup 2+
    • Svelte 3.5+ (Svelte 2 is no longer supported)
    • Node 10+
  • Breaking: Offload CSS handling to Rollup — you will now need an external plugin like rollup-plugin-css-only to extract your styles to .css files as demonstrated in the template (#147)
  • Breaking: Options to be passed directly to the Svelte compiler must now go under a compilerOptions key in the plugin configuration object (#145)
  • Extend CompileOptions interface directly (#126)
  • Pass relative filename to svelte compiler (#131)
  • Link sourcemap with source correctly (#140)
  • Respect sourcemapExcludeSources Rollup config (#93)
  • Keep all sourcemaps from chunk (#44)

6.1.1

  • Use require('svelte/compiler') rather than require('svelte/compiler.js') to work with new Svelte exports map

6.1.0

  • feat: allow custom Svelte compilers via new svelte option: (#124)
  • fix: use native fs.existsSync method: (50e03e5)
  • chore: Power CI via GitHub Action (61ead9a..23e83a4)

6.0.2

  • Added default value to CssWriter.write map option (#135)
  • Do not warn about missing unused css selectors if both css and emitCss are false (#127)

6.0.1

  • Fix types to allow css: false (#125)

6.0.0

  • Breaking changes:
    • Rollup 1.19.2+ is now required
    • The path passed to css.write() is now relative to the destination directory.
  • Other changes:
    • Add types for generate, customElement, and preprocess options (#111, #114, and #118)
    • Use Rollup's emitFile API (#72)
    • Warn when package.json does not expose itself via exports (#119)

5.2.3

  • Actually publish typings (#110)

5.2.2

  • Handle files with .svelte in the middle of their filename (#107)

5.2.1

  • Revert accidental change to Rollup peer dependency

5.2.0

  • Deterministic CSS bundle order (#84)
  • Add typings (#90)

5.1.1

  • Use Svelte 3's built-in logic for automatically determining the component name from its file path (#74)

5.1.0

  • Support array of preprocessors in Svelte 3

5.0.3

  • Handle onwarn correctly in new Svelte 3 beta

5.0.2

  • Support latest Svelte 3 beta

5.0.1

  • Use this.addWatchFile if present (#46)

5.0.0

2018-12-16

  • Replace deprecated ongenerate hook, use Rollup's internal warning mechanism; requires rollup@0.60+ (#45)

4.5.0

  • Pass dependencies through from preprocessors (#40)

4.4.0

  • Support Svelte 3 alpha
  • Internal reorganisation

4.3.2

  • Remove deprecated onerror handler

4.3.1

  • Handle arbitrary file extensions (#38)
  • Generate Windows-friendly import paths (#38)

4.3.0

  • Append inline sourcemaps to virtual CSS files generated with emitCss: true (#36)

4.2.1

  • Fix emitCss with style-less components (#34)

4.2.0

  • Add emitCss option (#32)

4.1.0

  • Support Svelte 1.60 and above (#29)

4.0.0

  • Move svelte to peerDependencies (#25)

3.3.0

  • Pass ID as filename to preprocess (#24)

3.2.0

  • Support preprocess option (#21)
  • Ignore unused CSS selector warnings if css: false (#17)

3.1.0

  • Allow shared option to override default (#16)
  • Use this.warn and this.error, so Rollup can handle failures

3.0.1

  • svelte should be a dependency, not a devDependency...

3.0.0

  • CSS sourcemaps (#14)

2.0.3

  • Ignore virtual modules (#13)

2.0.2

  • Only include code and map in object passed to Rollup

2.0.1

  • Prevent import of built-in modules from blowing up the resolver

2.0.0

  • Add support for pkg.svelte and pkg['svelte.root']

1.8.1

  • Handle components without <style> tags when concatenating CSS

1.8.0

  • Allow options.css to be a function that is called with extracted CSS when bundle is generated

1.7.0

  • Pass all options through to Svelte (e.g. dev)

1.6.1

  • Capitalize component names correctly

1.6.0

  • Update Svelte
  • Use shared helpers

1.3.1

  • Sanitize constructor names

1.3.0

  • Update Svelte
  • Add support for generate: 'ssr'
  • Enforce es format

1.2.5

  • Update Svelte
  • Include code frame in error message

1.2.0

  • Update Svelte
  • Support css and filename options

1.0.0

  • Update Svelte

0.3.0

  • Update Svelte

0.2.0

  • Update Svelte
  • Set options.name to basename of file

0.1.1

  • Update Svelte

0.1.0

  • Update Svelte
  • Install missing rollup-pluginutils dependency

0.0.2

  • First release