Skip to content

Commit ae51f4d

Browse files
author
Michael Vurchio
committed
Merge branch 'release/1.2.0'
2 parents 84ba05c + c9295bb commit ae51f4d

File tree

3 files changed

+63
-30
lines changed

3 files changed

+63
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Svelte preprocess CSS Modules, changelog
22

3+
## 1.2.0 (Sept 21, 2020)
4+
- Add support for `getLocalIdent()` [issue #6](https://github.com/micantoine/svelte-preprocess-cssmodules/issues/6) - [pull request #7](https://github.com/micantoine/svelte-preprocess-cssmodules/pull/7)
5+
36
## 1.1.1
47
- Fix the use of `[path]` in the `localIdentName` rule.
58
- Escape css on the classname

README.md

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,65 @@ Pass an object of the following properties
6565
| ------------- | ------------- | ------------- | ------------- |
6666
| `localIdentName` | `{String}` | `"[local]-[hash:base64:6]"` | A rule using any available token from [webpack interpolateName](https://github.com/webpack/loader-utils#interpolatename) |
6767
| `includePaths` | `{Array}` | `[]` (Any) | An array of paths to be processed |
68+
| `getLocalIdent` | `Function` | `undefined` | Generate the classname by specifying a function instead of using the built-in interpolation |
6869

70+
#### `getLocalIdent`
71+
72+
Function to generate the classname instead of the built-in function.
73+
74+
```js
75+
function getLocalIdent(
76+
context: {
77+
context, // the context path
78+
resourcePath, // path + filename
79+
},
80+
localIdentName: {
81+
template, // the template rule
82+
interpolatedName, // the built-in generated classname
83+
},
84+
className, // the classname string
85+
content: {
86+
markup, // the markup content
87+
style, // the style content
88+
}
89+
) {
90+
return `your_generated_classname`;
91+
}
92+
```
93+
94+
*Example of use*
95+
96+
```bash
97+
# Directory
98+
SvelteApp
99+
└─ src
100+
├─ App.svelte
101+
└─ components
102+
└─ Button.svelte
103+
```
104+
```html
105+
<!-- Button.svelte -->
106+
<button class="$style.red">Ok</button>
107+
108+
<style>
109+
.red { background-color: red; }
110+
</style>
111+
```
112+
113+
```js
114+
// Preprocess config
115+
...
116+
preprocess: [
117+
cssModules({
118+
localIdentName: '[path][name]__[local]',
119+
getLocalIdent: (context, { interpolatedName }) => {
120+
return interpolatedName.toLowerCase().replace('src_', '');
121+
// svelteapp_components_button__red;
122+
}
123+
})
124+
],
125+
...
126+
```
69127

70128
## Usage on Svelte Component
71129

@@ -290,7 +348,6 @@ export default {
290348
background-color: #fff;
291349
transform: translateX(-50%) translateY(-50%);
292350
}
293-
294351
section {
295352
flex: 0 1 auto;
296353
flex-direction: column;
@@ -299,30 +356,17 @@ export default {
299356
}
300357
header {
301358
padding: 1rem;
302-
font-size: 1.2rem;
303-
font-weight: bold;
304359
border-bottom: 1px solid #d9d9d9;
305360
}
306-
307361
.body {
308362
padding: 1rem;
309363
flex: 1 0 0;
310-
min-height: 0;
311-
max-height: 100%;
312-
overflow: scroll;
313-
-webkit-overflow-scrolling: touch;
314364
}
315-
316365
footer {
317366
padding: 1rem;
318-
text-align: right;
319367
border-top: 1px solid #d9d9d9;
320368
}
321369
button {
322-
padding: .5em 1em;
323-
min-width: 100px;
324-
font-size: .8rem;
325-
font-weight: 600;
326370
background: #fff;
327371
border: 1px solid #ccc;
328372
border-radius: 5px;
@@ -360,7 +404,6 @@ export default {
360404
background-color: #fff;
361405
transform: translateX(-50%) translateY(-50%);
362406
}
363-
364407
section {
365408
flex: 0 1 auto;
366409
flex-direction: column;
@@ -369,30 +412,17 @@ export default {
369412
}
370413
header {
371414
padding: 1rem;
372-
font-size: 1.2rem;
373-
font-weight: bold;
374415
border-bottom: 1px solid #d9d9d9;
375416
}
376-
377417
._1HPUBXtzNG {
378418
padding: 1rem;
379419
flex: 1 0 0;
380-
min-height: 0;
381-
max-height: 100%;
382-
overflow: scroll;
383-
-webkit-overflow-scrolling: touch;
384420
}
385-
386421
footer {
387422
padding: 1rem;
388-
text-align: right;
389423
border-top: 1px solid #d9d9d9;
390424
}
391425
button {
392-
padding: .5em 1em;
393-
min-width: 100px;
394-
font-size: .8rem;
395-
font-weight: 600;
396426
background: #fff;
397427
border: 1px solid #ccc;
398428
border-radius: 5px;
@@ -420,7 +450,7 @@ export default {
420450

421451
## Why CSS Modules on Svelte
422452

423-
While the native CSS Scoped system should be largely enough to avoid class conflict, it could find its limit when working on a hybrid project. On a non full javascript front-end where Svelte is used to enhance pieces of the page, the thought on the class naming is no less different than the one on a regular html page. For example, on the modal component above, It would have been wiser to namespace some of the classes such as `.modal-body` and `.modal-cancel` to avoid inheriting styles from other `.body` and `.cancel`.
453+
While the native CSS Scoped system should be largely enough to avoid class conflict, it could find its limit when working on a hybrid project. On a non full Svelte application where it is used to enhance pieces of the page; the thought on the class naming is no less different than the one on a regular html page to avoid conflict and unwilling inheritance. For example, on the modal component above, It would have been wiser to namespace some of the classes such as `.modal-body` and `.modal-cancel` to avoid inheriting styles from other `.body` and `.cancel`.
424454

425455
## License
426456

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-preprocess-cssmodules",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Svelte preprocessor to generate CSS Modules classname on Svelte components",
55
"keywords": [
66
"svelte",

0 commit comments

Comments
 (0)