Skip to content

Commit 7b8d7b4

Browse files
committed
Merge branch 'svelte5'
2 parents 5e56b87 + 25d3c12 commit 7b8d7b4

23 files changed

+3494
-5136
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Svelte preprocess CSS Modules, changelog
22

3-
## 2.2.5 (Sept 19, 2022)
3+
## 3.0.0 (Jan 17 2025)
4+
5+
### Update
6+
7+
- Support for svelte 5 [#124](https://github.com/micantoine/svelte-preprocess-cssmodules/issues/124)
8+
- Use modern AST
9+
10+
### Breaking Changes
11+
- Remove `linearPreprocess` util since it is not being needed anymore
12+
- Set peer dependencies to svelte 5 only
13+
14+
## 2.2.5 (Sept 19, 2024)
415

516
### Updates
617

README.md

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Generate CSS Modules classnames on Svelte components
66
npm 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
821820
import { typescript, scss } from 'svelte-preprocess';
822821
import { 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
851851
Pass 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*

example/webpack/App.svelte

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
<script>
22
import 'swiper/swiper.min.css';
33
import Time from './components/Time.svelte';
4+
import Body from './components/Body.svelte';
5+
46
import css from './app.module.css';
57
import { success, successSmall } from './app2.module.css';
68
import './app3.module.css';
79
810
let theme = { color: 'blue'};
911
</script>
10-
<div class="overlay" />
12+
<div class="overlay"></div>
1113
<div class="modal" style="display:block">
1214
<section>
13-
<header class="active" on:click={() => theme.color = 'red'}>My Modal title</header>
14-
<div class="body">
15-
<Time />
15+
<header
16+
class="active"
17+
role="button"
18+
tabindex="-1"
19+
on:click={() => theme.color = 'red'}
20+
on:keyup={() => theme.color = 'green'}
21+
>
22+
My Modal title
23+
</header>
24+
<Body>
25+
<Time class="time" />
1626
<p class="{css.error} {css.errorMessage} large"><strong>Lorem ipsum dolor sit</strong>, amet consectetur adipisicing elit. Placeat, deserunt.</p>
1727
<p class="{success} {successSmall}">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat, deserunt. Lorem ipsum dolor sit amet. </p>
18-
</div>
28+
</Body>
1929
<footer>
2030
<button class="active">Ok</button>
2131
<button class="cancel">Cancel</button>
@@ -61,15 +71,6 @@
6171
color: bind("theme.color");
6272
}
6373
64-
.body {
65-
padding: 1rem;
66-
flex: 1 0 0;
67-
min-height: 0;
68-
max-height: 100%;
69-
overflow: scroll;
70-
-webkit-overflow-scrolling: touch;
71-
}
72-
7374
:local(footer) {
7475
padding: 1rem;
7576
text-align: right;
@@ -90,4 +91,10 @@ button {
9091
.active {
9192
font-weight: 700;
9293
}
94+
95+
@media screen {
96+
.time {
97+
color: red;
98+
}
99+
}
93100
</style>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
let { children } = $props();
3+
</script>
4+
5+
<div class="body">
6+
{@render children?.()}
7+
</div>
8+
9+
<style module="mixed">
10+
.body {
11+
padding: 1rem;
12+
flex: 1 0 0;
13+
min-height: 0;
14+
max-height: 100%;
15+
overflow: scroll;
16+
-webkit-overflow-scrolling: touch;
17+
}
18+
</style>

example/webpack/components/Time.svelte

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
<script>
2-
import { onDestroy } from 'svelte';
2+
let { class: className, ...props} = $props();
33
4-
let className;
5-
export { className as class };
4+
let date = $state(new Date());
5+
let time = $derived(`${date.getHours()}:${twoDigits(date.getMinutes())}:${twoDigits(date.getSeconds())}`);
66
7-
let date = new Date();
8-
const active = true;
9-
$: time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
7+
function twoDigits(str) {
8+
return str.toLocaleString('en', { minimumIntegerDigits: 2 });
9+
}
1010
11-
const interval = setInterval(() => date = new Date(), 1000);
11+
$effect(() => {
12+
const interval = setInterval(() => {
13+
date = new Date();
14+
}, 1000);
1215
13-
onDestroy(() => {
14-
clearInterval(interval);
15-
})
16+
return () => {
17+
clearInterval(interval);
18+
};
19+
});
1620
</script>
1721

22+
<div
23+
class="time bolder light { true ? 'lighter red' : ''} {className}"
24+
class:bold={true}
25+
{...props}
26+
>{time}</div>
27+
1828
<style module="mixed">
1929
:local(div) {
2030
text-align: right;
@@ -51,17 +61,11 @@
5161
5262
:local(div) *.bold { font-size: 12px;}
5363
54-
.btn {
64+
.time {
5565
float:right;
5666
animation: opacity 4s infinite alternate;
5767
}
5868
59-
@media screen {
60-
.btn {
61-
color: red;
62-
}
63-
}
64-
6569
@keyframes opacity {
6670
0% {
6771
opacity: 0;
@@ -70,8 +74,12 @@
7074
opacity: 1;
7175
}
7276
}
73-
</style>
74-
<div
75-
class="btn bolder light { true ? 'lighter red' : ''}"
76-
class:bold={true}
77-
>{time}</div>
77+
@keyframes opacity2 {
78+
from {
79+
opacity: 0;
80+
}
81+
to {
82+
opacity: 1;
83+
}
84+
}
85+
</style>

example/webpack/main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1+
import { mount } from 'svelte';
12
import App from './App.svelte'
23

3-
new App({
4-
target: document.getElementById('app')
5-
})
4+
const app = mount(App, { target: document.getElementById("app") });

0 commit comments

Comments
 (0)