UNPKG

5.61 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```html
61<template>
62 <div>Hey</div>
63</template>
64
65<style></style>
66
67<script></script>
68```
69
70### External files
71
72```html
73<template src="./template.html"></template>
74<script src="./script.js"></script>
75<style src="./style.css"></style>
76```
77
78### Global style
79
80#### `global` attribute
81
82Add a `global` attribute to your `style` tag and instead of scoping the CSS, all of its content will be interpreted as global style.
83
84```html
85<style global>
86 div {
87 color: red;
88 }
89</style>
90```
91
92#### `:global` rule
93
94Use a `:global` rule to only expose parts of the stylesheet:
95
96```html
97<style lang="scss">
98 .scoped-style {
99 }
100
101 :global {
102 @import 'global-stylesheet.scss';
103
104 .global-style {
105 .global-child-style {
106 }
107 }
108 }
109</style>
110```
111
112Works best with nesting-enabled CSS preprocessors, but regular CSS selectors like `div :global .global1 .global2` are also supported.
113
114_**Note**: needs PostCSS to be installed._
115
116### Modern JavaScript syntax
117
118`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+.
119
120For example, with `@babel/preset-env` your config could be:
121
122```js
123import preprocess from 'svelte-preprocess'
124 ...
125 preprocess: preprocess({
126 babel: {
127 presets: [
128 [
129 '@babel/preset-env',
130 {
131 loose: true,
132 // No need for babel to resolve modules
133 modules: false,
134 targets: {
135 // ! Very important. Target es6+
136 esmodules: true,
137 },
138 },
139 ],
140 ],
141 },
142 });
143 ...
144```
145
146_**Note:** If you want to transpile your app to be supported in older browsers, you must run babel from the context of your bundler._
147
148### Replace values
149
150Replace 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.
151
152In example, to replace inject the value of `process.env.NODE_ENV`:
153
154```js
155autoPreprocess({
156 replace: ['process.env.NODE_ENV', JSON.stringify(process.env.NODE_ENV)],
157});
158```
159
160Which, in a production environment, would turn
161
162```svelte
163{#if process.env.NODE_ENV !== 'development'}
164 <h1>Production environment!</h1>
165{/if}
166```
167
168into
169
170```svelte
171{#if "production" !== 'development'}
172 <h1>Production environment!</h1>
173{/if}
174```
175
176### Built-in support for commonly used languages
177
178The current supported languages out-of-the-box are Sass, Stylus, Less, CoffeeScript, TypeScript, Pug, PostCSS, Babel.
179
180```html
181<template lang="pug">
182 div Posts
183 +each('posts as post')
184 a(href="{post.url}") {post.title}
185</template>
186
187<script lang="ts">
188 export const hello: string = 'world';
189</script>
190
191<style src="./style.scss"></style>
192```
193
194---
195
196### [Getting started](/docs/getting-started.md)
197
198### [Preprocessing documentation](/docs/preprocessing.md)
199
200### [Usage documentation](/docs/usage.md)
201
202### [Migration Guide](/docs/migration-guide.md)
203
204---
205
206## License
207
208[MIT](LICENSE)