Skip to content

Commit 84ba05c

Browse files
authored
Merge pull request #7 from micantoine/6-support-for-getlocalident
Support for getLocalIdent
2 parents c7f0b8a + 41c8cf6 commit 84ba05c

12 files changed

+62
-13
lines changed

example/webpack/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import Time from './Time.svelte';
2+
import Time from './components/Time.svelte';
33
</script>
44

55
<style>
File renamed without changes.

example/webpack/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
options: {
2222
preprocess: [
2323
cssModules({
24-
includePaths: ['./']
24+
includePaths: ['./'],
2525
})
2626
],
2727
emitCss: false

index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const { interpolateName } = require('loader-utils');
44

55
const pluginOptions = {
66
includePaths: [],
7-
localIdentName: '[local]-[hash:base64:6]'
7+
localIdentName: '[local]-[hash:base64:6]',
8+
getLocalIdent: getLocalIdent,
89
};
910

1011
const regex = {
@@ -18,14 +19,17 @@ const regex = {
1819

1920
let moduleClasses = {};
2021

22+
function getLocalIdent(context, localIdentName, localName, options) {
23+
return localIdentName.interpolatedName;
24+
}
25+
2126
function generateName(resourcePath, styles, className) {
2227
const filePath = resourcePath;
23-
const fileName = path.basename(filePath);
2428
const localName = pluginOptions.localIdentName.length
2529
? pluginOptions.localIdentName.replace(/\[local\]/gi, () => className)
2630
: className;
2731

28-
const content = `${styles}-${filePath}-${fileName}-${className}`;
32+
const content = `${styles}-${filePath}-${className}`;
2933

3034
let interpolatedName = cssesc(
3135
interpolateName({ resourcePath }, localName, { content })
@@ -77,8 +81,25 @@ const markup = async ({ content, filename }) => {
7781
styles[0],
7882
className
7983
);
80-
moduleClasses[filename][className] = interpolatedName;
81-
replacement = interpolatedName;
84+
85+
const customInterpolatedName = pluginOptions.getLocalIdent(
86+
{
87+
context: path.dirname(filename),
88+
resourcePath :filename,
89+
},
90+
{
91+
interpolatedName,
92+
template: pluginOptions.localIdentName,
93+
},
94+
className,
95+
{
96+
markup: code,
97+
style: styles[0],
98+
}
99+
);
100+
101+
moduleClasses[filename][className] = customInterpolatedName;
102+
replacement = customInterpolatedName;
82103
}
83104
}
84105
return replacement;

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.

test/compiler.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const svelte = require('svelte/compiler');
22
const cssModules = require('../index.js');
33

4-
module.exports = async ({ source, localIdentName }) => {
5-
const { code } = await svelte.preprocess(
4+
module.exports = async ({ source }, options) => {
5+
const { code } = await svelte.preprocess(
66
source,
77
[
8-
cssModules({
9-
localIdentName,
10-
})
8+
cssModules(options)
119
],
1210
{ filename : 'src/App.svelte' }
1311
);

test/getlocalident.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source = '<style>.red { color: red; }</style>\n<span class="$style.red">Red</span>';
4+
5+
test('Customize generated classname from getLocalIdent', async () => {
6+
const output = await compiler({
7+
source,
8+
}, {
9+
localIdentName: '[local]-123456MC',
10+
getLocalIdent: (context, { interpolatedName }) => {
11+
return interpolatedName.toLowerCase();
12+
}
13+
});
14+
15+
expect(output).toBe('<style>:global(.red-123456mc) { color: red; }</style>\n<span class="red-123456mc">Red</span>');
16+
});

test/path.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sourceReactiveClass = '<style>.red { color: red; }</style>\n<span class:$.
66
test('Replace path on regular class', async () => {
77
const output = await compiler({
88
source,
9+
}, {
910
localIdentName: '[path][name]__[local]',
1011
});
1112

@@ -15,6 +16,7 @@ test('Replace path on regular class', async () => {
1516
test('Replace path on reactive class', async () => {
1617
const output = await compiler({
1718
source: sourceReactiveClass,
19+
}, {
1820
localIdentName: '[path][name]__[local]',
1921
});
2022

test/remove.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const expectedOutput = '<style>.red { color: red; }</style>\n<span class="">Blue
77
test('Remove unused CSS Modules from HTML attribute', async () => {
88
const output = await compiler({
99
source,
10+
}, {
1011
localIdentName: '[local]-123456',
1112
});
1213

@@ -15,6 +16,7 @@ test('Remove unused CSS Modules from HTML attribute', async () => {
1516
test('[Shorthand] Remove unused CSS Modules from HTML attribute', async () => {
1617
const output = await compiler({
1718
source: sourceShorthand,
19+
}, {
1820
localIdentName: '[local]-123456',
1921
});
2022

test/replace.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.red
66
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
77
const output = await compiler({
88
source,
9+
}, {
910
localIdentName: '[local]-123456',
1011
});
1112

@@ -14,6 +15,7 @@ test('Generate CSS Modules from HTML attributes, Replace CSS className', async (
1415
test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
1516
const output = await compiler({
1617
source: sourceShorthand,
18+
}, {
1719
localIdentName: '[local]-123456',
1820
});
1921

@@ -23,13 +25,15 @@ test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS classNa
2325
test('Avoid generated class to start with a non character', async () => {
2426
const output = await compiler({
2527
source,
28+
}, {
2629
localIdentName: '1[local]',
2730
});
2831
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
2932
});
3033
test('[Shorthand] Avoid generated class to start with a non character', async () => {
3134
const output = await compiler({
3235
source: sourceShorthand,
36+
}, {
3337
localIdentName: '1[local]',
3438
});
3539
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
@@ -38,13 +42,15 @@ test('[Shorthand] Avoid generated class to start with a non character', async ()
3842
test('Avoid generated class to end with a hyphen', async () => {
3943
const output = await compiler({
4044
source,
45+
}, {
4146
localIdentName: '[local]-',
4247
});
4348
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
4449
});
4550
test('[Shorthand] Avoid generated class to end with a hyphen', async () => {
4651
const output = await compiler({
4752
source: sourceShorthand,
53+
}, {
4854
localIdentName: '[local]-',
4955
});
5056
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');

test/target.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const expectedOutput =
3333
test('Target proper className from lookalike classNames', async () => {
3434
const output = await compiler({
3535
source,
36+
}, {
3637
localIdentName: '[local]-123',
3738
});
3839

@@ -42,6 +43,7 @@ test('Target proper className from lookalike classNames', async () => {
4243
test('[Shorthand] Target proper className from lookalike classNames', async () => {
4344
const output = await compiler({
4445
source: sourceShorthand,
46+
}, {
4547
localIdentName: '[local]-123',
4648
});
4749

test/toggle.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const expectedOutput =
2323
test('Generate CSS Modules className on class binding', async () => {
2424
const output = await compiler({
2525
source,
26+
}, {
2627
localIdentName: '[local]-123456',
2728
});
2829

@@ -32,6 +33,7 @@ test('Generate CSS Modules className on class binding', async () => {
3233
test('[Shorthand] Generate CSS Modules className on class binding', async () => {
3334
const output = await compiler({
3435
source: sourceShorthand,
36+
}, {
3537
localIdentName: '[local]-123456',
3638
});
3739

0 commit comments

Comments
 (0)