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

Package detail

svelte-preprocess

sveltejs1.1mMIT6.0.3TypeScript support: included

A Svelte preprocessor wrapper with baked-in support for commonly used preprocessors

svelte, preprocess, typescript, less, stylus, sass, scss, pug, coffeescript

readme

Svelte Preprocess

A Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug and much more.

npm version license action-CI

What is it?

Svelte's own parser understands only JavaScript, CSS and its HTML-like syntax. To make it possible to write components in other languages, such as TypeScript or SCSS, Svelte provides the preprocess API, which allows to easily transform the content of your markup and your style/script tags.

Writing your own preprocessor for, i.e SCSS is easy enough, but it can be cumbersome to have to always configure multiple preprocessors for the languages you'll be using.

svelte-preprocess is a custom svelte preprocessor that acts as a facilitator to use other languages with Svelte, providing multiple features, sensible defaults and a less noisy development experience.

It is recommended to use with svelte.config.js file, located at the project root. For other usage, please refer to usage documentation.

import { sveltePreprocess } from 'svelte-preprocess';

const config = {
  preprocess: sveltePreprocess({ ... })
}

export default config;

Features

Template tag

Vue-like support for defining your markup between a specific tag. The default tag is template but it can be customized.

<template>
  <div>Hey</div>
</template>

<style></style>

<script></script>

External files

<template src="./template.html"></template>
<script src="./script.js"></script>
<style src="./style.css"></style>

Note: using a relative path starting with . is important. Otherwise svelte-preprocess will ignore the src attribute.

Global style

global attribute

Add a global attribute to your style tag and instead of scoping the CSS, all of its content will be interpreted as global style.

<style global>
  div {
    color: red;
  }
</style>

:global rule

Use a :global rule to only expose parts of the stylesheet:

<style lang="scss">
  .scoped-style {
  }

  :global {
    @import 'global-stylesheet.scss';

    .global-style {
      .global-child-style {
      }
    }
  }
</style>

Works best with nesting-enabled CSS preprocessors, but regular CSS selectors like div :global .global1 .global2 are also supported.

Note: needs PostCSS to be installed.

Modern JavaScript syntax

svelte-preprocess allows you to run your component code through Babel before sending it to the compiler, allowing you to use new language features such as optional operators and nullish coalescing. However, note that Babel should transpile your component code to the javascript version supported by the Svelte compiler, so ES6+.

For example, with @babel/preset-env your config could be:

import { sveltePreprocess } from 'svelte-preprocess'
  ...
  preprocess: sveltePreprocess({
    babel: {
      presets: [
        [
          '@babel/preset-env',
          {
            loose: true,
            // No need for babel to resolve modules
            modules: false,
            targets: {
              // ! Very important. Target es6+
              esmodules: true,
            },
          },
        ],
      ],
    },
  });
  ...

Note: If you want to transpile your app to be supported in older browsers, you must run babel from the context of your bundler.

Replace values

Replace a set of string patterns in your components markup by passing an array of [RegExp, ReplaceFn | string], the same arguments received by the String.prototype.replace method.

In example, to replace inject the value of process.env.NODE_ENV:

autoPreprocess({
  replace: [[/process\.env\.NODE_ENV/g, JSON.stringify(process.env.NODE_ENV)]],
});

Which, in a production environment, would turn

{#if process.env.NODE_ENV !== 'development'}
  <h1>Production environment!</h1>
{/if}

into

{#if 'production' !== 'development'}
  <h1>Production environment!</h1>
{/if}

Built-in support for commonly used languages

The current supported languages out-of-the-box are Sass, Stylus, Less, CoffeeScript, TypeScript, Pug, PostCSS, Babel.

<template lang="pug">
  div Posts +each('posts as post') a(href="{post.url}") {post.title}
</template>

<script lang="ts">
  export const hello: string = 'world';
</script>

<style src="./style.scss"></style>

Getting started

Preprocessing documentation

Usage documentation

Migration Guide


License

MIT

changelog

6.0.3 (2024-09-26)

Bug Fixes

6.0.2 (2024-07-09)

Bug Fixes

6.0.1 (2024-06-14)

Bug Fixes

  • deprecate default export in favor of named export (#641) (a43de10), closes #591

6.0.0 (2024-06-12)

BREAKING CHANGES

  • remove TS mixed imports support, require TS 5.0 or higher
  • remove preserve option as it's unnecessary
  • require Svelte 4+, Node 18+
  • add exports map

Bug Fixes

  • adjust globalifySelector to not split selectors with parentheses. (#632) (c435ebd), closes #501
  • fix: allow TS filename to be undefined, fixes #488
  • fix: adjust Svelte compiler type import
  • fix: remove pug types and magic-string from dependencies
  • chore: bump peer deps, fixes #553

5.1.4 (2024-04-16)

Bug Fixes

5.1.3 (2023-12-18)

Bug Fixes

  • sass dependency list referencing source file in win32 (#621) (209312f)

5.1.2 (2023-12-12)

  • chore: mark postcss-load-config 5 as supported (3b5b1f0)

5.1.1 (2023-11-21)

Bug Fixes

  • force module(resolution) (66d3cf9)

5.1.0 (2023-11-10)

Bug Fixes

  • spelling "dianostics" - "diagnostics" (#606) (a48cc4e)

Features

  • add experimental support for Svelte 5 (923f437)

5.0.4 (2023-05-26)

Bug Fixes

  • spell. "identation" → "indentation " (#598) (aa86f0b)
  • mark as ready for Svelte 4

5.0.3 (2023-03-17)

Bug Fixes

5.0.2 (2023-03-14)

Bug Fixes

5.0.1 (2023-01-20)

Bug Fixes

5.0.0 (2022-12-10)

Bug Fixes

  • 🐛 add sugarss v3 and v4 as supported (3f2687b)
  • 🐛 remove support for 'type' attribute (07bc8aa)
  • 🐛 remove support for custom default languages (3d60856)
  • 🐛 map .sss as .css to support sugarss extension (2c0bd45)

Performance Improvements

  • ⚡️ remove support for deprecated node-sass (a617fe1)

BREAKING CHANGES

  • 🧨 Languages must be explicitly defined via the lang attribute.
  • 🧨 Minimum node version was bumped from 9 to 14
  • 🧨 Cannot use "type" attribute to identify the language of a style or script tag anymore. Use lang instead
  • 🧨 node-sass is not supported anymore. Use sass instead
  • minimum node version changed to v14

4.10.7 (2022-06-04)

Bug Fixes

  • add pug const mixin (#518) (0551a9b), closes #467
  • paths in postcss sourcemap sources array (#500) (2027375)
  • preserve [@font-face](https://github.com/font-face) inside :global block (#486) (8064473), closes #236
  • stylus - handle relative sourcemap sources(#513) (77bd3bf)

4.10.6 (2022-04-13)

Bug Fixes

  • paths in postcss sourcemap sources array (#500) (2027375)
  • preserve [@font-face](https://github.com/font-face) inside :global block (#486) (8064473), closes #236

4.10.5 (2022-04-04)

Bug Fixes

4.10.4 (2022-02-23)

Bug Fixes

  • only try to load external files with relative paths (#487) (80d87ed)

4.10.3 (2022-02-09)

Bug Fixes

4.10.2 (2022-01-17)

Bug Fixes

4.10.1 (2021-12-17)

Bug Fixes

4.10.0 (2021-12-13)

Features

  • support preserveValueImports introduced in TS 4.5 (#434) (4ea9982)

4.9.8 (2021-10-02)

Bug Fixes

4.9.7 (2021-09-30)

Bug Fixes

  • don't overwrite target from tsconfig.json (#408) (11cb508)

4.9.6 (2021-09-30)

Bug Fixes

4.9.5 (2021-09-20)

Bug Fixes

4.9.4 (2021-09-09)

Bug Fixes

Performance Improvements

4.9.3 (2021-09-08)

Bug Fixes

4.9.2 (2021-09-07)

Bug Fixes

4.9.1 (2021-09-05)

Bug Fixes

  • fallback to empty string if there are no attributes (#397) (b25838f), closes #396

4.9.0 (2021-09-04)

Features

4.8.0 (2021-08-23)

Bug Fixes

  • add deprecation warning to defaults prop (#393) (406d3a8)

4.7.4 (2021-07-09)

Bug Fixes

  • standalone preprocessors prepending data in other languages (#380) (e6679e4)

4.7.3 (2021-05-03)

Bug Fixes

  • typescript: importing component from pnpm monorepo p… (#337) (bf72637)

4.7.2 (2021-04-19)

Bug Fixes

  • 🐛 prevent svelte file from being added to scss dep list (519b0b6), closes #346
  • support ts build with no tsconfig.json (cf0e44c)

4.7.1 (2021-04-18)

Features

  • export autopreprocess options type (f5e1a63)

4.7.0 (2021-03-26)

Features

4.6.9 (2021-02-13)

Bug Fixes

  • 🐛 make markup tag regexp less greedy (384ba5c), closes #310
  • revert "refactor: use fs/promises" (3f9572c)

4.6.8 (2021-02-11)

Bug Fixes

  • 🐛 make markup tag regexp less greedy (64f3362), closes #310

4.6.7 (2021-02-10)

Bug Fixes

  • 🐛 language custom transformer overriding postcss (2a188bc), closes #309

4.6.6 (2021-02-03)

Bug Fixes

4.6.5 (2021-01-29)

Bug Fixes

  • [scss] remove sourceMappingURL from result.code (#297) (2ff48f8)

4.6.4 (2021-01-29)

Bug Fixes

  • 🐛 better missing postcss message (cafb6c6), closes #301

4.6.3 (2021-01-21)

Bug Fixes

  • 🐛 make postcss config error explicit (21e3086), closes #298

4.6.2 (2021-01-21)

Bug Fixes

  • translate options.sourceMap to options.compilerOptions.sourceMap for ts (#286) (#299) (c8a3cd6)

4.6.1 (2020-11-20)

Bug Fixes

4.6.0 (2020-11-17)

Bug Fixes

  • 🐛 replace separator for windows support (b60a0a8)
  • 🐛 sass tilde importer (934fc04)

Features

  • 🎸 add scss support for tilde (~) imports (5b5c692), closes #277

4.5.2 (2020-10-23)

Bug Fixes

  • 🐛 inline :local without a scope not working (2683fda), closes #238
  • 🐛 warn when global attr is used without potcss (1be483b)

4.5.1 (2020-10-07)

Bug Fixes

  • 🐛 add sugarss as optional dependency (9ed25ee)

4.5.0 (2020-10-07)

Bug Fixes

  • 🐛 postcss config file support for sugarss (4876426)

Features

  • 🎸 add support for lang=sugarss & type=text/sugarss (683715d), closes #250

4.4.3 (2020-10-07)

Bug Fixes

4.4.2 (2020-10-05)

Bug Fixes

  • 🐛 prefixed keyframes globalization (903d95b), closes #264

4.4.1 (2020-10-05)

Bug Fixes

  • 🐛 prevent trying to resolve interpolated src values (780b09a), closes #226

4.4.0 (2020-10-05)

Features

4.3.2 (2020-09-25)

Bug Fixes

  • 🐛 nth-child not being correctly globalified (fa7249f), closes #224
  • 🐛 prevent supressing generic errors on postcss transformer (9a7dd49), closes #216

4.3.1 (2020-09-25)

Bug Fixes

  • 🐛 nth-child not being correctly globalified (c78b260), closes #224

4.3.0 (2020-09-16)

Features

4.2.2 (2020-09-16)

Bug Fixes

4.2.1 (2020-09-01)

Bug Fixes

  • 🐛 self-closing templates with external source (217a09d), closes #235

4.2.0 (2020-08-30)

Features

  • 🎸 enable sourceMap for dev environment automatically (4df9031)

4.1.3 (2020-08-30)

Bug Fixes

  • 🐛 replacer preprocessor options types (d3543da), closes #228

4.1.2 (2020-08-28)

Bug Fixes

  • 🐛 language specific options not being set (f3df8b0), closes #231

4.1.1 (2020-08-18)

Bug Fixes

  • 🐛 guarantee lowercase markup tagname (b277bb3)

4.1.0 (2020-08-18)

Features

  • 🎸 support template wrappers in separate markup processors (dc52009), closes #211

4.0.12 (2020-08-13)

Bug Fixes

  • 🐛 remove accidental console.log (09e9aa8)

4.0.11 (2020-08-12)

Bug Fixes

  • 🐛 babel inputSourceMap object (3b6fc3e), closes #215

4.0.10 (2020-08-04)

Bug Fixes

  • 🐛 strip indent from indentation-sensitive languages only (8d735bd)

4.0.9 (2020-08-04)

Bug Fixes

4.0.8 (2020-07-14)

Bug Fixes

4.0.7 (2020-07-11)

Bug Fixes

  • 🐛 add more meaningful log to importAny (2f7053e)

4.0.6 (2020-07-08)

Bug Fixes

4.0.5 (2020-07-08)

Bug Fixes

4.0.4 (2020-07-08)

Bug Fixes

4.0.3 (2020-07-08)

Bug Fixes

  • 🐛 another type error :shrug: (c55ad93)

4.0.2 (2020-07-08)

Bug Fixes

  • 🐛 transformers type completion (45ed796)

4.0.1 (2020-07-07)

Bug Fixes

  • 🐛 bump minimum node version to 9.11.2 (b8e0568)
  • 🐛 postcss installation check (7df673a)
  • 🐛 prevent globalify to wrongly split escaped selectors (e9c4031), closes #191
  • 🐛 rename scss prepend option from data to prependData (bd1caca)
  • 🐛 try to use sass before node-sass (10af027), closes #163

Code Refactoring

  • 💡 remove deprecated autoProcess props (7fbff08)

Features

  • 🎸 add sourceMap prop to configuration object (9156efc)
  • 🎸 support defining default languages (6483879), closes #189
  • 🎸 support markup preprocessing with no tags (a1a3360)
  • 🎸 support prependData for almost every preprocessor (b80ca90)

BREAKING CHANGES

  • 🧨 This is a general evolution of the specific scss.data property that was used to prepend data to components written in scss. {preprocessorOptions}.prependData is now the way to prepend some string to any preprocessor.
  • 🧨 Node versions below 9.11.2 won't be supported anymore
  • 🧨 Uses Lookbehind assertions, so Node 9.11.2+ is needed
  • 🧨 Content passed through the data property won't be prepended anymore.
  • 🧨 onBefore and transformers were removed

4.0.0 (2020-07-07)

Bug Fixes

  • 🐛 try to use sass before node-sass (89aba0e), closes #163

  • 🐛 bump minimum node version to 9.11.2 (0befa7f)

  • 🐛 prevent globalify to wrongly split escaped selectors (f461320), closes #191
  • 🐛 rename scss prepend option from data to prependData (16b1325)

Code Refactoring

  • 💡 remove deprecated autoProcess props (3dce7e4)

Features

  • 🎸 add sourceMap prop to configuration object (a2505da)
  • 🎸 support defining default languages (d86122f), closes #189
  • 🎸 support markup preprocessing with no tags (290ef98)
  • 🎸 support prependData for almost every preprocessor (ef5272e)

BREAKING CHANGES

  • 🧨 This is a general evolution of the specific scss.data property that was used to prepend data to components written in scss. {preprocessorOptions}.prependData is now the way to prepend some string to any preprocessor.
  • 🧨 Node versions below 9.11.2 won't be supported anymore
  • 🧨 Uses Lookbehind assertions, so Node 9.11.2+ is needed
  • 🧨 Content passed through the data property won't be prepended anymore.
  • 🧨 onBefore and transformers were removed

3.9.12 (2020-07-05)

Bug Fixes

  • 🐛 set bare option to true (312bbb9)

3.9.11 (2020-07-01)

Bug Fixes

  • 🐛 log a warning if local external file is not found (774aece), closes #174

3.9.10 (2020-06-22)

Bug Fixes

  • remove extra indentation for sass content (7d0f437)

3.9.9 (2020-06-19)

Bug Fixes

  • 🐛 prevent including external file if content is not empty (24e90d1), closes #183
  • 🐛 throw if type errors are found (6545a5c), closes #182

3.9.8 (2020-06-17)

Bug Fixes

  • 🐛 prevent including external file if content is not empty (fd1b55a), closes #183

3.9.7 (2020-06-10)

Bug Fixes

  • 🐛 attributes not being passed to transformers (840239d), closes #175
  • quotes in the release script (#173) (5550b3e)

3.9.6 (2020-06-06)

Bug Fixes

  • merge globalStyle and globalRule transformers (a61ada6)

3.9.5 (2020-06-06)

Bug Fixes

  • add the comma into the list of combinators (8386bf3)
  • teach the globalifySelector to determine combinators (1783c55)

3.9.4 (2020-06-06)

Bug Fixes

  • do not break when :global is the only selector (f011b74)

3.9.3 (2020-06-06)

Bug Fixes

  • use the typescript transform to remove type imports (3a15831)

3.9.2 (2020-06-06)

Bug Fixes

  • 🐛 run globalRule only if postcss is installed (3c22a20)
  • 🐛 scss with empty content (12c3af3), closes #166

Features

  • 🎸 add globalStyle.sourceMap and globalRule.sourceMap opts (2717c5b)
  • add implementation option for scss (e4ca556)
  • add the @global {} rule support (453c4be)
  • replace the @global for :global for CSS modules compliance (cca29fb)

3.9.0 (2020-06-05)

Bug Fixes

  • 🐛 run globalRule only if postcss is installed (6294750)

Features

3.8.0 (2020-06-05)

Features

  • add implementation option for scss (909e0c9)

3.7.4 (2020-04-23)

3.7.3 (2020-04-22)

Bug Fixes

  • 🐛 add preprocessors as optional peerDeps (7f434df), closes #137

3.7.2 (2020-04-21)

Bug Fixes

  • 🐛 add bare: true to coffee compiler (7d38bfd), closes #134

3.7.1 (2020-03-29)

Bug Fixes

  • 🐛 syntax and parser props from postcss config file (74d4a89), closes #127

3.7.0 (2020-03-29)

Bug Fixes

  • avoid reporting 'Cannot find name '{0}'. Did you mean '{1}'?' for reactive variables (#126) (a267885)

Features

  • 🎸 add replace transformer (06981f4)

3.6.0 (2020-03-26)

Features

  • add an option to render scss synchronously via renderSync (#123) (1c9285d)

3.5.0 (2020-03-13)

Features

3.4.0 (2020-02-02)

Features

  • 🎸 watch included pug files (a8855fb)

3.3.1 (2020-01-29)

Bug Fixes

  • 🐛 postinstall script breaking on yarn v2 (2f7f62b)

Features

  • 🎸 support :local() pseudo-selector for global styles (52277a8)

3.3.0 (2019-12-19)

Features

  • 🎸 support :local() pseudo-selector for global styles (c9d98c2)

3.2.6 (2019-11-07)

Bug Fixes

  • 🐛 concat passed inclusion paths with default ones (aac8cd4)

3.2.5 (2019-11-06)

Bug Fixes

  • 🐛 empty scss content throwing error (b4a4139)

3.2.4 (2019-11-05)

Bug Fixes

  • 🐛 rollback last release (b4461b4)

3.2.3 (2019-11-05)

Bug Fixes

  • 🐛 add svelte component typings to ts type scope (434d0b4)

3.2.2 (2019-10-31)

Bug Fixes

  • 🐛 support for self closing markup/template tag (d109a89)
  • 🐛 use ts import transformer when transpileOnly:true (752fbde), closes #91

3.2.1 (2019-10-31)

Bug Fixes

  • 🐛 prevent ts from removing unused imports (cfe6dcb), closes #81
  • 🐛 stylus imports on windows (5bee6e0)
  • 🐛 transforming typescript without a tsconfig.json file (7edb18a)
  • 🐛 typescript imports on windows (f6d6195)
  • don't try to include local files that doesn't exist (52594eb)

Performance Improvements

3.1.3 (2019-10-23)

Bug Fixes

  • 🐛 Try to only include files with local paths (a167f6e)

3.1.2 (2019-09-25)

Bug Fixes

  • 🐛 import less cjs instead of es6 (bf8627f)

3.1.1 (2019-09-10)

Bug Fixes

3.1.0 (2019-09-03)

Features

  • 🎸 add "markupTagName" option (746d2ab)

3.0.2 (2019-08-29)

Bug Fixes

  • 🐛 inverted conditionals on typescript transformer (a6937f0)

3.0.1 (2019-08-29)

Bug Fixes

  • 🐛 wrong typescript diagnostic filtering (2630a44), closes #49

3.0.0 (2019-08-28)

Performance Improvements

  • ⚡️ make postcss-load-config optional for better pkg size (7ab9c72)

BREAKING CHANGES

  • To load PostCSS config automatically from a file, now it's needed to manually install "postcss-load-config".

2.16.0 (2019-08-28)

Features

  • 🎸 add "transpileOnly" option to skip type check (3e46741), closes #54

2.15.2 (2019-08-28)

Bug Fixes

  • 🐛 make pug mixins work with space AND tabs (81b0154)
  • rename typescript configuration option to honor the readme docs (67f2137)

2.15.0 (2019-07-20)

Features

  • 🎸 add external src support for stand-alone processors (974ab5a)

2.14.4 (2019-07-03)

Features

  • 🎸 allow to watch stylus dependencies (8aa3dfc)

2.14.3 (2019-07-01)

Bug Fixes

2.14.2 (2019-06-29)

Bug Fixes

2.14.1 (2019-06-28)

Bug Fixes

  • 🐛 transformer imported dependencies being overwritten (423c17a)

2.14.0 (2019-06-22)

2.13.1 (2019-06-21)

2.13.0 (2019-06-21)

2.12.0 (2019-06-03)

Bug Fixes

  • 🐛 template preprocessing running on the whole file (e37da9d)

Features

  • 🎸 add support for typescript type checking (#37) (e6dd744)
  • 🎸 add svelte pug mixins (#38) (543ab75)
  • 🎸 add typescript preprocessor (c195aa1)
  • prepend scss with data property (#36) (dfa2b2a)

2.7.1 (2019-05-08)

Bug Fixes

  • 🐛 cut 90% of downloaded package size (882a4dd)

Features

  • 🎸 watch internal files imported with postcss-import (5b14624)

2.6.5 (2019-05-05)

Bug Fixes

  • 🐛 stand-alone processors not exported (ced0fd1)

2.6.4 (2019-05-05)

Bug Fixes

  • 🐛 less and stylus stand-alone processor (85827bb)

2.6.3 (2019-05-01)

Features

2.6.2 (2019-04-11)

Bug Fixes

  • 🐛 standalone processors breaking everything :) (ce11323)

Features

  • 🎸 add stand-alone processors (f19c90a)

2.5.2 (2019-04-10)

Features

  • 🎸 support async onBefore() (a6af2a2)

2.5.1 (2019-04-02)

Bug Fixes

  • 🐛 custom transformer not working with external src files (cc037c3)

2.4.2 (2018-11-03)

2.4.1 (2018-11-02)

2.4.0 (2018-11-01)

2.3.1 (2018-09-01)

2.2.2 (2018-07-18)

2.2.1 (2018-07-18)

2.2.0 (2018-07-18)

2.1.3 (2018-06-21)

2.1.0 (2018-06-20)

2.0.5 (2018-05-17)

2.0.4 (2018-05-17)

2.0.2 (2018-05-15)

2.0.1 (2018-05-15)

1.1.2 (2018-05-14)