Skip to content

Commit d801e9e

Browse files
committed
docs: ✏️ docs revamp
1 parent f299c6a commit d801e9e

File tree

11 files changed

+1003
-603
lines changed

11 files changed

+1003
-603
lines changed

README.md

Lines changed: 74 additions & 501 deletions
Large diffs are not rendered by default.

docs/getting-started.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# Getting Started
2+
3+
<!-- @import "[TOC]" {cmd="toc" depthFrom=2 depthTo=6 orderedList=false} -->
4+
5+
<!-- code_chunk_output -->
6+
7+
- [1. Installation](#1-installation)
8+
- [2. Adding `svelte-preprocess` to our build workflow](#2-adding-svelte-preprocess-to-our-build-workflow)
9+
- [3. Configuring preprocessors](#3-configuring-preprocessors)
10+
- [3.1. Setting default languages](#31-setting-default-languages)
11+
- [3.2 Prepending content](#32-prepending-content)
12+
13+
<!-- /code_chunk_output -->
14+
15+
_Note: The examples below are going to be using a hypothetical `rollup.config.js` as if we were configuring a simple Svelte application, but `svelte-preprocess` can be used in various setups. See "[Usage](docs/usage.md)"._
16+
17+
## 1. Installation
18+
19+
First things first, let's create a new Svelte app project and add `svelte-preprocess`.
20+
21+
```shell
22+
$ npx degit sveltejs/template my-svelte-app
23+
$ cd my-svelte-app
24+
$ npm install -D svelte-preprocess
25+
```
26+
27+
`svelte-preprocess` doesn't have any language specific dependency, so it's up to us to install the rest of tools we are going to use:
28+
29+
- `babel`: `npm install -D @babel/core @babel/preset-...`
30+
- `coffeescript`: `npm install -D coffeescript`
31+
- `typescript`: `npm install -D typescript`
32+
- `postcss`: `npm install -D postcss postcss-load-config`
33+
- `less`: `npm install -D less`
34+
- `sass`: `npm install -D node-sass` or `npm install -D sass`
35+
- `pug`: `npm install -D pug`
36+
- `stylus`: `npm install -D stylus`
37+
38+
For now, let's just install the main library.
39+
40+
## 2. Adding `svelte-preprocess` to our build workflow
41+
42+
Let's use `svelte-preprocess` in [auto-preprocessing mode](/docs/preprocessing##auto-preprocessing) and add it to our `rollup.config.js`:
43+
44+
```diff
45+
import svelte from 'rollup-plugin-svelte'
46+
+ import autoPreprocess from 'svelte-preprocess';
47+
48+
const production = !process.env.ROLLUP_WATCH
49+
50+
export default {
51+
input: 'src/main.js',
52+
output: {
53+
sourcemap: true,
54+
format: 'iife',
55+
name: 'app',
56+
file: 'public/bundle.js',
57+
},
58+
plugins: [
59+
svelte({
60+
+ preprocess: autoPreprocess(),
61+
// enable run-time checks when not in production
62+
dev: !production,
63+
// we'll extract any component CSS out into
64+
// a separate file — better for performance
65+
css: css => {
66+
css.write('public/bundle.css')
67+
},
68+
}),
69+
],
70+
}
71+
```
72+
73+
Now our app's code can be written in any of the syntaxes supported by `svelte-preprocess`: `SCSS`, `Stylus`, `Less`, `Coffeescript`, `TypeScript`, `Pug`, `PostCSS`, `Babel`.
74+
75+
_**Note:** If you use VS Code, check [its usage guide](/docs/usage.md#with-svelte-vs-code) to make the Svelte VS Code extension understand the content of your components._
76+
77+
## 3. Configuring preprocessors
78+
79+
Now let's assume our app markup is going to be written in `pug`, our scripts in `Typescript`, and our styles in `SCSS`. We also want our styles to be auto-prefixed, so we're also going to need `postcss`. Let's install these dependencies:
80+
81+
**Important:** `svelte-preprocess` only handles content passed to it by `svelte-loader`, `rollup-plugin-svelte` and similar tools. If our `typescript` component import a `typescript` file, the bundler will be the one responsible for handling it. We must make sure it knows how to handle it!
82+
83+
```shell
84+
$ npm i -D typescript sass postcss autoprefixer pug @rollup/plugin-typescript
85+
```
86+
87+
After the installation is complete, we still need to configure our `postcss` options and add `@rollup/plugin-typescript` to our config.
88+
89+
```diff
90+
import svelte from 'rollup-plugin-svelte'
91+
import autoPreprocess from 'svelte-preprocess';
92+
+ import typescript from '@rollup/plugin-typescript';
93+
94+
const production = !process.env.ROLLUP_WATCH
95+
96+
export default {
97+
input: 'src/main.js',
98+
output: {
99+
sourcemap: true,
100+
format: 'iife',
101+
name: 'app',
102+
file: 'public/bundle.js',
103+
},
104+
plugins: [
105+
svelte({
106+
+ preprocess: autoPreprocess({
107+
+ sourceMap: !production,
108+
+ postcss: {
109+
+ plugins: [require('autoprefixer')()]
110+
+ }
111+
+ }),
112+
+ // teach rollup how to handle typescript imports
113+
+ typescript({ sourceMap: !production }),
114+
// enable run-time checks when not in production
115+
dev: !production,
116+
// we'll extract any component CSS out into
117+
// a separate file — better for performance
118+
css: css => {
119+
css.write('public/bundle.css')
120+
},
121+
}),
122+
],
123+
}
124+
```
125+
126+
And we're done! Our components can now be written as:
127+
128+
```html
129+
<template lang="pug">
130+
h1 {world}
131+
</template>
132+
133+
<script lang="ts">
134+
export let name: string = 'world';
135+
</script>
136+
137+
<style lang="scss">
138+
h1 {
139+
color: red;
140+
}
141+
</style>
142+
```
143+
144+
### 3.1. Setting default languages
145+
146+
Ok, we now can write our entire app with `pug`, `typescript` and `scss`, but typing `lang="..."` in every file can become an obnoxious process. In [auto-preprocessing mode](/docs/preprocessing.md#auto-preprocessing), `svelte-preprocess` [lets us define the default languages](/docs/preprocessing.md#auto-preprocessing-options) of our components. It's setted by default to `html`, `javascript` and `css`. Let's change that so we don't need those `lang` attributes.
147+
148+
_**Disclaimer**: The Svelte VS Code extension uses the `lang` or `type` attribute to correctly highlight your code. At the time of writing, the extension doesn't support default languages. Doing this can lead to errors on your IDE._
149+
150+
```diff
151+
import svelte from 'rollup-plugin-svelte'
152+
import autoPreprocess from 'svelte-preprocess';
153+
154+
export default {
155+
input: 'src/main.js',
156+
output: {
157+
sourcemap: true,
158+
format: 'iife',
159+
name: 'app',
160+
file: 'public/bundle.js',
161+
},
162+
plugins: [
163+
svelte({
164+
preprocess: autoPreprocess({
165+
sourceMap: !production,
166+
+ defaults: {
167+
+ markup: 'pug',
168+
+ script: 'typescript',
169+
+ style: 'scss'
170+
+ },
171+
postcss: {
172+
plugins: [require('autoprefixer')()]
173+
}
174+
}),
175+
// enable run-time checks when not in production
176+
dev: !production,
177+
// we'll extract any component CSS out into
178+
// a separate file — better for performance
179+
css: css => {
180+
css.write('public/bundle.css')
181+
},
182+
}),
183+
],
184+
}
185+
```
186+
187+
Now our components are a bit leaner!
188+
189+
```html
190+
<template>
191+
h1 {world}
192+
</template>
193+
194+
<script>
195+
export let name: string = 'world';
196+
</script>
197+
198+
<style>
199+
h1 {
200+
color: red;
201+
}
202+
</style>
203+
```
204+
205+
_**Note**: If the `<template>` tag is not found and the default language is not `html`, `svelte-preprocess` expects the whole markup to be written in that language. In example, for `pug`, this means the `script` and `style` tags must be written following pug's syntax._
206+
207+
### 3.2 Prepending content
208+
209+
Now we're in need of a `scss` file to hold some variables. Let's assume it's created at `src/styles/variables.scss`.
210+
211+
```scss
212+
// src/styles/variables.scss
213+
$primary-color: red;
214+
```
215+
216+
As in any `scss` project, we could just `@use './path/to/variables.scss`, but that can also become boring. `svelte-preprocess` [accepts a `prependData`](/docs/preprocessing.md#preprocessors) for almost every processor. Let's use it to prepend our import!
217+
218+
```diff
219+
+ import path from 'path'
220+
import svelte from 'rollup-plugin-svelte'
221+
import autoPreprocess from 'svelte-preprocess';
222+
223+
export default {
224+
input: 'src/main.js',
225+
output: {
226+
sourcemap: true,
227+
format: 'iife',
228+
name: 'app',
229+
file: 'public/bundle.js',
230+
},
231+
plugins: [
232+
svelte({
233+
preprocess: autoPreprocess({
234+
sourceMap: !production,
235+
defaults: {
236+
markup: 'pug',
237+
script: 'typescript',
238+
style: 'scss'
239+
},
240+
+ scss: {
241+
+ // we use path.resolve because we need a absolute path
242+
+ // because every component style is preprocessed from its directory
243+
+ // so relative paths won't work.
244+
+ prependData: `@use '${path.resolve(process.cwd(), 'src/styles/variables.scss')}';`
245+
+ },
246+
postcss: {
247+
plugins: [require('autoprefixer')()]
248+
}
249+
}),
250+
// enable run-time checks when not in production
251+
dev: !production,
252+
// we'll extract any component CSS out into
253+
// a separate file — better for performance
254+
css: css => {
255+
css.write('public/bundle.css')
256+
},
257+
}),
258+
],
259+
}
260+
```
261+
262+
Voila! We can now reference a variable from our file without having to explicitly import it.
263+
264+
```html
265+
<template>
266+
h1 {world}
267+
</template>
268+
269+
<script>
270+
export let name: string = 'world';
271+
</script>
272+
273+
<style>
274+
h1 {
275+
color: $primary-color;
276+
}
277+
</style>
278+
```

0 commit comments

Comments
 (0)