Skip to content

Commit e984c8a

Browse files
author
Michael Vurchio
committed
Merge branch 'release/1.1.0'
2 parents 9224112 + 3dccaf6 commit e984c8a

11 files changed

+136
-24
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.1.0
4+
- Add optional shortand syntax `$.MY_CLASSNAME` to increase development speed and remove verbosity.
5+
36
# 1.0.1
47
- Fix style parsing on empty cssModules class
58

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ Pass an object of the following properties
6767
| `includePaths` | `{Array}` | `[]` (Any) | An array of paths to be processed |
6868

6969

70-
7170
## Usage on Svelte Component
7271

73-
**On the HTML markup** (not the CSS), Prefix any class name that require CSS Modules by *$style.* => `$style.My_CLASSNAME`
72+
**On the HTML markup** (not the CSS), Prefix any class name that require CSS Modules by *$style.* => `$style.MY_CLASSNAME`
7473

7574
```html
7675
<style>
@@ -209,6 +208,10 @@ A class can be naturally used on multiple elements.
209208
Toggling a class on an element.
210209

211210
```html
211+
<script>
212+
let isActive = true;
213+
</script>
214+
212215
<style>
213216
.bold { font-weight: bold; }
214217
</style>
@@ -217,6 +220,41 @@ Toggling a class on an element.
217220
<p class="{isActive ? '$style.bold' : ''}">My blue text</p>
218221
```
219222

223+
### Shorthand
224+
To remove verbosity the shorthand `$.MY_CLASSNAME` can be used instead of the regular `$style.MY_CLASSNAME`.
225+
226+
*before*
227+
228+
```html
229+
<script>
230+
let isActive = true;
231+
</script>
232+
233+
<style>
234+
.red { color: red; }
235+
.blue { color: blue; }
236+
.bold { font-weight: bold; }
237+
</style>
238+
239+
<p
240+
class:$.bold={isActive}
241+
class="$.red">My red text</p>
242+
<p class="{isActive ? '$.bold' : ''} $.blue">My blue text</p>
243+
```
244+
245+
*After*
246+
247+
```html
248+
<style>
249+
.red-en-6pb { color: red; }
250+
.blue-oVk-n1 { color: blue; }
251+
.bold-2jIMhI { font-weight: bold; }
252+
</style>
253+
254+
<p class="red-en-6pb bold-2jIMhI">My red text</p>
255+
<p class="bold-2jIMhI blue-oVk-n1">My blue text</p>
256+
```
257+
220258
## Example
221259

222260
*Rollup Config*

example/webpack/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ button {
7070
}
7171
</style>
7272

73-
<div class="$style.overlay" />
73+
<div class="$.overlay" />
7474
<div class="$style.modal">
7575
<section>
7676
<header class="$style.active">My Modal title</header>

example/webpack/Time.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
</style>
2525

2626
<div
27-
class="$style.datetime"
28-
class:$style.bold={true}>{time}</div>
27+
class="$.datetime"
28+
class:$.bold={true}>{time}</div>

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pluginOptions = {
77
}
88

99
const regex = {
10-
module: /\$style\.(:?[\w\d-]*)/gm,
10+
module: /\$(style)?\.(:?[\w\d-]*)/gm,
1111
style: /<style(\s[^]*?)?>([^]*?)<\/style>/gi,
1212
class: (className) => {
1313
return new RegExp(`\\.(${className})\\b(?![-_])`, 'gm')
@@ -58,7 +58,7 @@ const markup = async ({ content, filename }) => {
5858
const styles = content.match(regex.style);
5959
moduleClasses[filename] = {};
6060

61-
return { code: content.replace(regex.module, (match, className) => {
61+
return { code: content.replace(regex.module, (match, key, className) => {
6262
let replacement = '';
6363
if (styles.length) {
6464
if (regex.class(className).test(styles[0])) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.0.1",
3+
"version": "1.1.0",
44
"description": "Svelte preprocessor to generate CSS Modules classname on Svelte components",
55
"keywords": [
66
"svelte",

test/remove.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
const compiler = require('./compiler.js');
22

33
const source = '<style>.red { color: red; }</style>\n<span class="$style.blue">Blue</span>';
4+
const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.blue">Blue</span>';
5+
const expectedOutput = '<style>.red { color: red; }</style>\n<span class="">Blue</span>';
46

57
test('Remove unused CSS Modules from HTML attribute', async () => {
68
const output = await compiler({
79
source,
810
localIdentName: '[local]-123456',
911
});
1012

11-
expect(output).toBe('<style>.red { color: red; }</style>\n<span class="">Blue</span>');
12-
});
13+
expect(output).toBe(expectedOutput);
14+
});
15+
test('[Shorthand] Remove unused CSS Modules from HTML attribute', async () => {
16+
const output = await compiler({
17+
source: sourceShorthand,
18+
localIdentName: '[local]-123456',
19+
});
20+
21+
expect(output).toBe(expectedOutput);
22+
});

test/replace.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const compiler = require('./compiler.js');
22

33
const source = '<style>.red { color: red; }</style>\n<span class="$style.red">Red</span>';
4+
const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.red">Red</span>';
45

56
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
67
const output = await compiler({
@@ -10,6 +11,14 @@ test('Generate CSS Modules from HTML attributes, Replace CSS className', async (
1011

1112
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
1213
});
14+
test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
15+
const output = await compiler({
16+
source: sourceShorthand,
17+
localIdentName: '[local]-123456',
18+
});
19+
20+
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
21+
});
1322

1423
test('Avoid generated class to start with a non character', async () => {
1524
const output = await compiler({
@@ -18,6 +27,13 @@ test('Avoid generated class to start with a non character', async () => {
1827
});
1928
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
2029
});
30+
test('[Shorthand] Avoid generated class to start with a non character', async () => {
31+
const output = await compiler({
32+
source: sourceShorthand,
33+
localIdentName: '1[local]',
34+
});
35+
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
36+
});
2137

2238
test('Avoid generated class to end with a hyphen', async () => {
2339
const output = await compiler({
@@ -26,3 +42,10 @@ test('Avoid generated class to end with a hyphen', async () => {
2642
});
2743
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
2844
});
45+
test('[Shorthand] Avoid generated class to end with a hyphen', async () => {
46+
const output = await compiler({
47+
source: sourceShorthand,
48+
localIdentName: '[local]-',
49+
});
50+
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
51+
});

test/target.test.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,40 @@ const source =
1010
<span class="$style.red-crimson">Crimson</span>
1111
<span class="$style.redMajenta">Majenta</span>`;
1212

13-
test('Target proper className from lookalike classNames', async () => {
14-
const output = await compiler({
15-
source,
16-
localIdentName: '[local]-123',
17-
});
13+
const sourceShorthand =
14+
`<style>
15+
.red { color: red; }
16+
.red-crimson { color: crimson; }
17+
.redMajenta { color: magenta; }
18+
</style>
19+
<span class="$.red">Red</span>
20+
<span class="$.red-crimson">Crimson</span>
21+
<span class="$.redMajenta">Majenta</span>`;
1822

19-
expect(output).toBe(
23+
const expectedOutput =
2024
`<style>
2125
:global(.red-123) { color: red; }
2226
:global(.red-crimson-123) { color: crimson; }
2327
:global(.redMajenta-123) { color: magenta; }
2428
</style>
2529
<span class="red-123">Red</span>
2630
<span class="red-crimson-123">Crimson</span>
27-
<span class="redMajenta-123">Majenta</span>`);
31+
<span class="redMajenta-123">Majenta</span>`;
32+
33+
test('Target proper className from lookalike classNames', async () => {
34+
const output = await compiler({
35+
source,
36+
localIdentName: '[local]-123',
37+
});
38+
39+
expect(output).toBe(expectedOutput);
40+
});
41+
42+
test('[Shorthand] Target proper className from lookalike classNames', async () => {
43+
const output = await compiler({
44+
source: sourceShorthand,
45+
localIdentName: '[local]-123',
46+
});
47+
48+
expect(output).toBe(expectedOutput);
2849
});

test/toggle.test.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,35 @@ const source =
66
class:$style.red={true}
77
class="{true ? '$style.red' : ''}">Red</span>
88
`;
9+
const sourceShorthand =
10+
`<style>.red { color: red; }</style>
11+
<span
12+
class:$.red={true}
13+
class="{true ? '$.red' : ''}">Red</span>
14+
`;
15+
16+
const expectedOutput =
17+
`<style>:global(.red-123456) { color: red; }</style>
18+
<span
19+
class:red-123456={true}
20+
class="{true ? 'red-123456' : ''}">Red</span>
21+
`;
922

1023
test('Generate CSS Modules className on class binding', async () => {
1124
const output = await compiler({
1225
source,
1326
localIdentName: '[local]-123456',
1427
});
1528

16-
expect(output).toBe(
17-
`<style>:global(.red-123456) { color: red; }</style>
18-
<span
19-
class:red-123456={true}
20-
class="{true ? 'red-123456' : ''}">Red</span>
21-
`);
29+
expect(output).toBe(expectedOutput);
30+
});
31+
32+
test('[Shorthand] Generate CSS Modules className on class binding', async () => {
33+
const output = await compiler({
34+
source: sourceShorthand,
35+
localIdentName: '[local]-123456',
36+
});
37+
38+
expect(output).toBe(expectedOutput);
2239
});
2340

0 commit comments

Comments
 (0)