@@ -6,6 +6,8 @@ Generate CSS Modules classnames on Svelte components
66npm install --save-dev svelte-preprocess-cssmodules
77```
88
9+ for ` svelte 4 ` and below, use version 2 of the preprocessor.
10+
911## Table of Content
1012
1113- [ Usage] ( #usage )
@@ -30,8 +32,8 @@ npm install --save-dev svelte-preprocess-cssmodules
3032 - [ Webpack] ( #webpack )
3133 - [ SvelteKit] ( #sveltekit )
3234 - [ Svelte Preprocess] ( #svelte-preprocess )
35+ - [ Vite] ( #vite )
3336 - [ Options] ( #options )
34- - [ Migrating from v1] ( #migrating-from-v1 )
3537- [ Code example] ( #code-example )
3638
3739## Usage
@@ -113,8 +115,8 @@ Toggle a class on an element.
113115
114116``` html
115117<script >
116- let route = ' home' ;
117- $ : isActive = route === ' home' ;
118+ let route = $state ( ' home' ) ;
119+ let isActive = $derived ( route === ' home' ) ;
118120 </script >
119121
120122<style module >
@@ -140,8 +142,8 @@ _generating_
140142
141143``` html
142144<script >
143- let route = ' home' ;
144- $ : active = route === ' home' ;
145+ let route = $state ( ' home' ) ;
146+ let active = $derived ( route === ' home' ) ;
145147 </script >
146148
147149<style module >
@@ -264,7 +266,7 @@ Link the value of a CSS property to a dynamic variable by using `bind()`.
264266
265267``` html
266268<script >
267- let color = ' red' ;
269+ let color = $state ( ' red' ) ;
268270 </script >
269271
270272<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.
302304
303305``` html
304306<script >
305- const style = {
307+ const style = $state ( {
306308 opacity: 0 ;
307- };
309+ }) ;
308310 </script >
309311
310312<div class =" content" >
@@ -327,9 +329,9 @@ _generating_
327329
328330``` html
329331<script >
330- const style = {
332+ const style = $state ( {
331333 opacity: 0 ;
332- };
334+ }) ;
333335 </script >
334336
335337<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
355357``` html
356358<!-- Child Component Button.svelte -->
357359<script >
358- let className;
359- export { className as class };
360+ let { class: className } = $props ();
360361 </script >
361362
362363<button class =" btn {className}" >
@@ -579,8 +580,8 @@ Use the Svelte's builtin `class:` directive or javascript template to display a
579580<script >
580581 import { success , error } from ' ./style.module.css' ;
581582
582- let isSuccess = true ;
583- $ : notice = isSuccess ? success : error;
583+ let isSuccess = $state ( true ) ;
584+ let notice = $derived ( isSuccess ? success : error) ;
584585 </script >
585586
586587<button on:click ={() => isSuccess = !isSuccess}>Toggle</button >
@@ -813,40 +814,39 @@ export default config;
813814
814815### Svelte Preprocess
815816
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.
819818
820819``` js
821820import { typescript , scss } from ' svelte-preprocess' ;
822821import { cssModules } from ' svelte-preprocess-cssmodules' ;
823822
824823...
825- // svelte config: NOT working!
824+ // svelte config:
826825 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
830829 ],
831830...
832831```
833832
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.
835836
836837``` 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' ;
839840
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+ };
848847```
849848
849+
850850### Options
851851Pass an object of the following properties
852852
@@ -1062,28 +1062,6 @@ preprocess: [
10621062...
10631063```
10641064
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-
10871065## Code Example
10881066
10891067* Rollup Config*
0 commit comments