@@ -6,6 +6,8 @@ Generate CSS Modules classnames on Svelte components
6
6
npm install --save-dev svelte-preprocess-cssmodules
7
7
```
8
8
9
+ for ` svelte 4 ` and below, use version 2 of the preprocessor.
10
+
9
11
## Table of Content
10
12
11
13
- [ Usage] ( #usage )
@@ -30,8 +32,8 @@ npm install --save-dev svelte-preprocess-cssmodules
30
32
- [ Webpack] ( #webpack )
31
33
- [ SvelteKit] ( #sveltekit )
32
34
- [ Svelte Preprocess] ( #svelte-preprocess )
35
+ - [ Vite] ( #vite )
33
36
- [ Options] ( #options )
34
- - [ Migrating from v1] ( #migrating-from-v1 )
35
37
- [ Code example] ( #code-example )
36
38
37
39
## Usage
@@ -113,8 +115,8 @@ Toggle a class on an element.
113
115
114
116
``` html
115
117
<script >
116
- let route = ' home' ;
117
- $ : isActive = route === ' home' ;
118
+ let route = $state ( ' home' ) ;
119
+ let isActive = $derived ( route === ' home' ) ;
118
120
</script >
119
121
120
122
<style module >
@@ -140,8 +142,8 @@ _generating_
140
142
141
143
``` html
142
144
<script >
143
- let route = ' home' ;
144
- $ : active = route === ' home' ;
145
+ let route = $state ( ' home' ) ;
146
+ let active = $derived ( route === ' home' ) ;
145
147
</script >
146
148
147
149
<style module >
@@ -264,7 +266,7 @@ Link the value of a CSS property to a dynamic variable by using `bind()`.
264
266
265
267
``` html
266
268
<script >
267
- let color = ' red' ;
269
+ let color = $state ( ' red' ) ;
268
270
</script >
269
271
270
272
<p class =" text" >My lorem ipsum text</p >
@@ -302,9 +304,9 @@ An object property can also be targetted and must be wrapped with quotes.
302
304
303
305
``` html
304
306
<script >
305
- const style = {
307
+ const style = $state ( {
306
308
opacity: 0 ;
307
- };
309
+ }) ;
308
310
</script >
309
311
310
312
<div class =" content" >
@@ -327,9 +329,9 @@ _generating_
327
329
328
330
``` html
329
331
<script >
330
- const style = {
332
+ const style = $state ( {
331
333
opacity: 0 ;
332
- };
334
+ }) ;
333
335
</script >
334
336
335
337
<div class =" content-dhye8T" style =" --opacity-r1gf51 :{style .opacity}" >
@@ -355,8 +357,7 @@ CSS Modules allows you to pass a scoped classname to a child component giving th
355
357
``` html
356
358
<!-- Child Component Button.svelte -->
357
359
<script >
358
- let className;
359
- export { className as class };
360
+ let { class: className } = $props ();
360
361
</script >
361
362
362
363
<button class =" btn {className}" >
@@ -579,8 +580,8 @@ Use the Svelte's builtin `class:` directive or javascript template to display a
579
580
<script >
580
581
import { success , error } from ' ./style.module.css' ;
581
582
582
- let isSuccess = true ;
583
- $ : notice = isSuccess ? success : error;
583
+ let isSuccess = $state ( true ) ;
584
+ let notice = $derived ( isSuccess ? success : error) ;
584
585
</script >
585
586
586
587
<button on:click ={() => isSuccess = !isSuccess}>Toggle</button >
@@ -813,40 +814,39 @@ export default config;
813
814
814
815
### Svelte Preprocess
815
816
816
- Svelte is running the preprocessors by phases, going through all * markup* first, followed by * script* and then * style* .
817
-
818
- The CSS Modules preprocessor is doing all its work on the markup phase via ` svelte.parse() ` which requires the compoment to be a valid standard svelte component (using vanilla js and vanilla css). if any other code (such as typescript or sass) is encountered, an error will be thrown.
817
+ The CSS Modules preprocessor requires the compoment to be a standard svelte component (using vanilla js and vanilla css). if any other code, such as Typescript or Sass, is encountered, an error will be thrown. Therefore CSS Modules needs to be run at the very end.
819
818
820
819
``` js
821
820
import { typescript , scss } from ' svelte-preprocess' ;
822
821
import { cssModules } from ' svelte-preprocess-cssmodules' ;
823
822
824
823
...
825
- // svelte config: NOT working!
824
+ // svelte config:
826
825
preprocess: [
827
- typescript (), // 2 run second on script phase
828
- scss (), // 3 run last on style phase
829
- cssModules (), // 1 run first on markup phase
826
+ typescript (),
827
+ scss (),
828
+ cssModules (), // run last
830
829
],
831
830
...
832
831
```
833
832
834
- As it is extremely common for developers to use ` svelte-preprocess ` in their application, CSS Modules provides a small utility to easily be incorporated with. ` linearPreprocess ` will ensure a linear process with the list of preprocessors.
833
+ ### Vite
834
+
835
+ Set the ` svelte.config.js ` accordingly.
835
836
836
837
``` js
837
- import { typescript , scss } from ' svelte-preprocess ' ;
838
- import { cssModules , linearPreprocess } from ' svelte-preprocess-cssmodules' ;
838
+ import { vitePreprocess } from ' @sveltejs/vite-plugin-svelte ' ;
839
+ import { cssModules } from ' svelte-preprocess-cssmodules' ;
839
840
840
- ...
841
- // svelte config: OK, processing one after another!
842
- preprocess: linearPreprocess ([
843
- typescript (), // 1 run first
844
- scss (), // 2 run second
845
- cssModules (), // 3 run last
846
- ]),
847
- ...
841
+ export default {
842
+ preprocess: [
843
+ vitePreprocess (),
844
+ cssModules ()
845
+ ]
846
+ };
848
847
```
849
848
849
+
850
850
### Options
851
851
Pass an object of the following properties
852
852
@@ -1062,28 +1062,6 @@ preprocess: [
1062
1062
...
1063
1063
```
1064
1064
1065
- ## Migrating from v1
1066
- If you want to migrate an existing project to ` v2 ` keeping the approach of the 1st version, follow the steps below:
1067
-
1068
- - Set the ` mixed ` mode from the global settings.
1069
- ``` js
1070
- // Preprocess config
1071
- ...
1072
- preprocess: [
1073
- cssModules ({
1074
- mode: ' mixed' ,
1075
- }),
1076
- ],
1077
- ...
1078
- ```
1079
- - Remove all ` $style. ` prefix from the html markup
1080
- - Add the attribute ` module ` to ` <style> ` within your components.
1081
- ``` html
1082
- <style module >
1083
- ...
1084
- </style >
1085
- ```
1086
-
1087
1065
## Code Example
1088
1066
1089
1067
* Rollup Config*
0 commit comments