UNPKG

5.65 kBMarkdownView Raw
1# Svelte Preprocess
2
3> A [Svelte](https://svelte.dev) preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, CoffeeScript, TypeScript, Pug and much more.
4
5<p>
6 <a href="https://www.npmjs.com/package/svelte-preprocess">
7 <img src="https://img.shields.io/npm/v/svelte-preprocess.svg" alt="npm version">
8 </a>
9
10 <a href="https://github.com/sveltejs/svelte-preprocess/blob/master/LICENSE">
11 <img src="https://img.shields.io/npm/l/svelte-preprocess.svg" alt="license">
12 </a>
13
14 <a href="https://github.com/sveltejs/svelte-preprocess/actions?query=workflow%3ACI">
15 <img src="https://github.com/sveltejs/svelte-preprocess/workflows/CI/badge.svg" alt="action-CI">
16 </a>
17</p>
18
19- [What is it?](#what-is-it)
20- [Getting Started](docs/getting-started.md)
21- [Usage](docs/usage.md)
22- [Migration Guide](docs/migration-guide.md)
23- [Preprocessing](docs/preprocessing.md)
24 - [Preprocessors](docs/preprocessing.md#preprocessors)
25- [Features](#features)
26 - [Template tag](#template-tag)
27 - [External files](#external-files)
28 - [Global style](#global-style)
29 - [Modern JavaScript syntax](#modern-javascript-syntax)
30 - [Replace values](#replace-values)
31 - [Built-in support for commonly used languages](#built-in-support-for-commonly-used-languages)
32
33## What is it?
34
35`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](https://svelte.dev/docs#svelte_preprocess), which allows to easily transform the content of your `markup` and your `style`/`script` tags.
36
37Writing 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.
38
39`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.
40
41```js
42import svelte from 'rollup-plugin-svelte'
43import sveltePreprocess from 'svelte-preprocess'
44
45export default {
46 plugins: [
47 svelte({
48 preprocess: sveltePreprocess({ ... })
49 })
50 ]
51}
52```
53
54## Features
55
56### Template tag
57
58Add _vue-like_ support for defining your markup between a `<template>` tag. The tagname can be customized to something like `markup` for example. See [#options](#options).
59
60_**Note:** only for auto preprocessing_
61
62```html
63<template>
64 <div>Hey</div>
65</template>
66
67<style></style>
68
69<script></script>
70```
71
72### External files
73
74```html
75<template src="./template.html"></template>
76<script src="./script.js"></script>
77<style src="./style.css"></style>
78```
79
80### Global style
81
82#### `global` attribute
83
84Add a `global` attribute to your `style` tag and instead of scoping the CSS, all of its content will be interpreted as global style.
85
86```html
87<style global>
88 div {
89 color: red;
90 }
91</style>
92```
93
94#### `:global` rule
95
96Use a `:global` rule to only expose parts of the stylesheet:
97
98```html
99<style lang="scss">
100 .scoped-style {
101 }
102
103 :global {
104 @import 'global-stylesheet.scss';
105
106 .global-style {
107 .global-child-style {
108 }
109 }
110 }
111</style>
112```
113
114Works best with nesting-enabled CSS preprocessors, but regular CSS selectors like `div :global .global1 .global2` are also supported.
115
116_**Note**: needs PostCSS to be installed._
117
118### Modern JavaScript syntax
119
120`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+.
121
122For example, with `@babel/preset-env` your config could be:
123
124```js
125import preprocess from 'svelte-preprocess'
126 ...
127 preprocess: preprocess({
128 babel: {
129 presets: [
130 [
131 '@babel/preset-env',
132 {
133 loose: true,
134 // No need for babel to resolve modules
135 modules: false,
136 targets: {
137 // ! Very important. Target es6+
138 esmodules: true,
139 },
140 },
141 ],
142 ],
143 },
144 });
145 ...
146```
147
148_**Note:** If you want to transpile your app to be supported in older browsers, you must run babel from the context of your bundler._
149
150### Replace values
151
152Replace 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.
153
154In example, to replace inject the value of `process.env.NODE_ENV`:
155
156```js
157autoPreprocess({
158 replace: ['process.env.NODE_ENV', JSON.stringify(process.env.NODE_ENV)],
159});
160```
161
162Which, in a production environment, would turn
163
164```svelte
165{#if process.env.NODE_ENV !== 'development'}
166 <h1>Production environment!</h1>
167{/if}
168```
169
170into
171
172```svelte
173{#if "production" !== 'development'}
174 <h1>Production environment!</h1>
175{/if}
176```
177
178### Built-in support for commonly used languages
179
180The current supported languages out-of-the-box are Sass, Stylus, Less, CoffeeScript, TypeScript, Pug, PostCSS, Babel.
181
182```html
183<template lang="pug">
184 div Posts
185 +each('posts as post')
186 a(href="{post.url}") {post.title}
187</template>
188
189<script lang="ts">
190 export const hello: string = 'world';
191</script>
192
193<style src="./style.scss"></style>
194```
195
196---
197
198### [Getting started](/docs/getting-started.md)
199
200### [Preprocessing documentation](/docs/preprocessing.md)
201
202### [Usage documentation](/docs/usage.md)
203
204### [Migration Guide](/docs/migration-guide.md)
205
206---
207
208## License
209
210[MIT](LICENSE)