| 
 | 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 hypothetical `rollup.config.js` as if we were configuring a simple Svelte application, but `svelte-preprocess` can be used in multiple scenarios. See [Usage](docs/usage.md)._  | 
 | 16 | + | 
 | 17 | +## 1. Installation  | 
 | 18 | + | 
 | 19 | +```shell  | 
 | 20 | +$ npm install -D svelte-preprocess  | 
 | 21 | +```  | 
 | 22 | + | 
 | 23 | +`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:  | 
 | 24 | + | 
 | 25 | +- `babel`: `npm install -D @babel/core @babel/preset-...`  | 
 | 26 | +- `coffeescript`: `npm install -D coffeescript`  | 
 | 27 | +- `typescript`: `npm install -D typescript`  | 
 | 28 | +- `postcss`: `npm install -D postcss postcss-load-config`  | 
 | 29 | +- `less`: `npm install -D less`  | 
 | 30 | +- `sass`: `npm install -D node-sass` or `npm install -D sass`  | 
 | 31 | +- `pug`: `npm install -D pug`  | 
 | 32 | +- `stylus`: `npm install -D stylus`  | 
 | 33 | + | 
 | 34 | +## 2. Adding `svelte-preprocess` to our build workflow  | 
 | 35 | + | 
 | 36 | +Let's add `svelte-preprocess` in [auto-preprocessing mode](/docs/preprocessing##auto-preprocessing) to our `rollup.config.js`:  | 
 | 37 | + | 
 | 38 | +```diff  | 
 | 39 | +import svelte from 'rollup-plugin-svelte'  | 
 | 40 | + | 
 | 41 | + | 
 | 42 | + | 
 | 43 | ++ import autoPreprocess from 'svelte-preprocess';  | 
 | 44 | + | 
 | 45 | +const production = !process.env.ROLLUP_WATCH  | 
 | 46 | + | 
 | 47 | +export default {  | 
 | 48 | +  input: 'src/main.js',  | 
 | 49 | +  output: {  | 
 | 50 | +    sourcemap: true,  | 
 | 51 | +    format: 'iife',  | 
 | 52 | +    name: 'app',  | 
 | 53 | +    file: 'public/bundle.js',  | 
 | 54 | +  },  | 
 | 55 | +  plugins: [  | 
 | 56 | +    svelte({  | 
 | 57 | ++      //  | 
 | 58 | ++      preprocess: autoPreprocess(),  | 
 | 59 | +      // enable run-time checks when not in production  | 
 | 60 | +      dev: !production,  | 
 | 61 | +      // we'll extract any component CSS out into  | 
 | 62 | +      // a separate file — better for performance  | 
 | 63 | +      css: css => {  | 
 | 64 | +        css.write('public/bundle.css')  | 
 | 65 | +      },  | 
 | 66 | +    }),  | 
 | 67 | +  ],  | 
 | 68 | +}  | 
 | 69 | +```  | 
 | 70 | + | 
 | 71 | +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`.  | 
 | 72 | + | 
 | 73 | +_**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._  | 
 | 74 | + | 
 | 75 | +## 3. Configuring preprocessors  | 
 | 76 | + | 
 | 77 | +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:  | 
 | 78 | + | 
 | 79 | +```shell  | 
 | 80 | +$ npm i -D typescript sass postcss autoprefixer pug  | 
 | 81 | +```  | 
 | 82 | + | 
 | 83 | +After the installation is complete, `pug`, `typescript` and `scss` are ready-to-go, but we still need to configure our `postcss` options. Let's add them to our `rollup.config.js`:  | 
 | 84 | + | 
 | 85 | +```diff  | 
 | 86 | +import svelte from 'rollup-plugin-svelte'  | 
 | 87 | +import autoPreprocess from 'svelte-preprocess';  | 
 | 88 | + | 
 | 89 | +const production = !process.env.ROLLUP_WATCH  | 
 | 90 | + | 
 | 91 | +export default {  | 
 | 92 | +  input: 'src/main.js',  | 
 | 93 | +  output: {  | 
 | 94 | +    sourcemap: true,  | 
 | 95 | +    format: 'iife',  | 
 | 96 | +    name: 'app',  | 
 | 97 | +    file: 'public/bundle.js',  | 
 | 98 | +  },  | 
 | 99 | +  plugins: [  | 
 | 100 | +    svelte({  | 
 | 101 | ++      preprocess: autoPreprocess({  | 
 | 102 | ++         postcss: {  | 
 | 103 | ++           plugins: [require('autoprefixer')()]  | 
 | 104 | ++         }  | 
 | 105 | ++      }),  | 
 | 106 | +      // enable run-time checks when not in production  | 
 | 107 | +      dev: !production,  | 
 | 108 | +      // we'll extract any component CSS out into  | 
 | 109 | +      // a separate file — better for performance  | 
 | 110 | +      css: css => {  | 
 | 111 | +        css.write('public/bundle.css')  | 
 | 112 | +      },  | 
 | 113 | +    }),  | 
 | 114 | +  ],  | 
 | 115 | +}  | 
 | 116 | +```  | 
 | 117 | + | 
 | 118 | +And we're done! Our components can now be written as:  | 
 | 119 | + | 
 | 120 | +```html  | 
 | 121 | +<template lang="pug">  | 
 | 122 | +  h1 {world}  | 
 | 123 | +</template>  | 
 | 124 | + | 
 | 125 | +<script lang="ts">  | 
 | 126 | +  export let name: string = 'world';  | 
 | 127 | +</script>  | 
 | 128 | + | 
 | 129 | +<style lang="scss">  | 
 | 130 | +  h1 {  | 
 | 131 | +    color: red;  | 
 | 132 | +  }  | 
 | 133 | +</style>  | 
 | 134 | +```  | 
 | 135 | + | 
 | 136 | +### 3.1. Setting default languages  | 
 | 137 | + | 
 | 138 | +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.  | 
 | 139 | + | 
 | 140 | +_**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._  | 
 | 141 | + | 
 | 142 | +```diff  | 
 | 143 | +import svelte from 'rollup-plugin-svelte'  | 
 | 144 | +import autoPreprocess from 'svelte-preprocess';  | 
 | 145 | + | 
 | 146 | +export default {  | 
 | 147 | +  input: 'src/main.js',  | 
 | 148 | +  output: {  | 
 | 149 | +    sourcemap: true,  | 
 | 150 | +    format: 'iife',  | 
 | 151 | +    name: 'app',  | 
 | 152 | +    file: 'public/bundle.js',  | 
 | 153 | +  },  | 
 | 154 | +  plugins: [  | 
 | 155 | +    svelte({  | 
 | 156 | +      preprocess: autoPreprocess({  | 
 | 157 | ++        defaults: {  | 
 | 158 | ++          markup: 'pug',  | 
 | 159 | ++          script: 'typescript',  | 
 | 160 | ++          style: 'scss'  | 
 | 161 | ++        },  | 
 | 162 | +         postcss: {  | 
 | 163 | +           plugins: [require('autoprefixer')()]  | 
 | 164 | +         }  | 
 | 165 | +      }),  | 
 | 166 | +      // enable run-time checks when not in production  | 
 | 167 | +      dev: !production,  | 
 | 168 | +      // we'll extract any component CSS out into  | 
 | 169 | +      // a separate file — better for performance  | 
 | 170 | +      css: css => {  | 
 | 171 | +        css.write('public/bundle.css')  | 
 | 172 | +      },  | 
 | 173 | +    }),  | 
 | 174 | +  ],  | 
 | 175 | +}  | 
 | 176 | +```  | 
 | 177 | + | 
 | 178 | +Now our components are a bit leaner!  | 
 | 179 | + | 
 | 180 | +```html  | 
 | 181 | +<template>  | 
 | 182 | +  h1 {world}  | 
 | 183 | +</template>  | 
 | 184 | + | 
 | 185 | +<script>  | 
 | 186 | +  export let name: string = 'world';  | 
 | 187 | +</script>  | 
 | 188 | + | 
 | 189 | +<style>  | 
 | 190 | +  h1 {  | 
 | 191 | +    color: red;  | 
 | 192 | +  }  | 
 | 193 | +</style>  | 
 | 194 | +```  | 
 | 195 | + | 
 | 196 | +_**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._  | 
 | 197 | + | 
 | 198 | +### 3.2 Prepending content  | 
 | 199 | + | 
 | 200 | +Now we're in need of a `scss` file to hold some variables. Let's assume it's created at `src/styles/variables.scss`.  | 
 | 201 | + | 
 | 202 | +```scss  | 
 | 203 | +// src/styles/variables.scss  | 
 | 204 | +$primary-color: red;  | 
 | 205 | +```  | 
 | 206 | + | 
 | 207 | +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!  | 
 | 208 | + | 
 | 209 | +```diff  | 
 | 210 | ++ import path from 'path'  | 
 | 211 | +import svelte from 'rollup-plugin-svelte'  | 
 | 212 | +import autoPreprocess from 'svelte-preprocess';  | 
 | 213 | + | 
 | 214 | +export default {  | 
 | 215 | +  input: 'src/main.js',  | 
 | 216 | +  output: {  | 
 | 217 | +    sourcemap: true,  | 
 | 218 | +    format: 'iife',  | 
 | 219 | +    name: 'app',  | 
 | 220 | +    file: 'public/bundle.js',  | 
 | 221 | +  },  | 
 | 222 | +  plugins: [  | 
 | 223 | +    svelte({  | 
 | 224 | +      preprocess: autoPreprocess({  | 
 | 225 | +         defaults: {  | 
 | 226 | +           markup: 'pug',  | 
 | 227 | +           script: 'typescript',  | 
 | 228 | +           style: 'scss'  | 
 | 229 | +         },  | 
 | 230 | ++        scss: {  | 
 | 231 | ++          // we use path.resolve because we need a absolute path  | 
 | 232 | ++          // because every component style is preprocessed from its directory  | 
 | 233 | ++          // so relative paths won't work.  | 
 | 234 | ++          prependData: `@use '${path.resolve(process.cwd(), 'src/styles/variables.scss')}';`  | 
 | 235 | ++        },  | 
 | 236 | +         postcss: {  | 
 | 237 | +           plugins: [require('autoprefixer')()]  | 
 | 238 | +         }  | 
 | 239 | +      }),  | 
 | 240 | +      // enable run-time checks when not in production  | 
 | 241 | +      dev: !production,  | 
 | 242 | +      // we'll extract any component CSS out into  | 
 | 243 | +      // a separate file — better for performance  | 
 | 244 | +      css: css => {  | 
 | 245 | +        css.write('public/bundle.css')  | 
 | 246 | +      },  | 
 | 247 | +    }),  | 
 | 248 | +  ],  | 
 | 249 | +}  | 
 | 250 | +```  | 
 | 251 | + | 
 | 252 | +Voila! We can now reference a variable from our file without having to explicitly import it.  | 
 | 253 | + | 
 | 254 | +```html  | 
 | 255 | +<template>  | 
 | 256 | +  h1 {world}  | 
 | 257 | +</template>  | 
 | 258 | + | 
 | 259 | +<script>  | 
 | 260 | +  export let name: string = 'world';  | 
 | 261 | +</script>  | 
 | 262 | + | 
 | 263 | +<style>  | 
 | 264 | +  h1 {  | 
 | 265 | +    color: $primary-color;  | 
 | 266 | +  }  | 
 | 267 | +</style>  | 
 | 268 | +```  | 
0 commit comments