Skip to content

Support for getLocalIdent #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/webpack/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import Time from './Time.svelte';
import Time from './components/Time.svelte';
</script>

<style>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion example/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
options: {
preprocess: [
cssModules({
includePaths: ['./']
includePaths: ['./'],
})
],
emitCss: false
Expand Down
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const { interpolateName } = require('loader-utils');

const pluginOptions = {
includePaths: [],
localIdentName: '[local]-[hash:base64:6]'
localIdentName: '[local]-[hash:base64:6]',
getLocalIdent: getLocalIdent,
};

const regex = {
Expand All @@ -18,14 +19,17 @@ const regex = {

let moduleClasses = {};

function getLocalIdent(context, localIdentName, localName, options) {
return localIdentName.interpolatedName;
}

function generateName(resourcePath, styles, className) {
const filePath = resourcePath;
const fileName = path.basename(filePath);
const localName = pluginOptions.localIdentName.length
? pluginOptions.localIdentName.replace(/\[local\]/gi, () => className)
: className;

const content = `${styles}-${filePath}-${fileName}-${className}`;
const content = `${styles}-${filePath}-${className}`;

let interpolatedName = cssesc(
interpolateName({ resourcePath }, localName, { content })
Expand Down Expand Up @@ -77,8 +81,25 @@ const markup = async ({ content, filename }) => {
styles[0],
className
);
moduleClasses[filename][className] = interpolatedName;
replacement = interpolatedName;

const customInterpolatedName = pluginOptions.getLocalIdent(
{
context: path.dirname(filename),
resourcePath :filename,
},
{
interpolatedName,
template: pluginOptions.localIdentName,
},
className,
{
markup: code,
style: styles[0],
}
);

moduleClasses[filename][className] = customInterpolatedName;
replacement = customInterpolatedName;
}
}
return replacement;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions test/compiler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
const svelte = require('svelte/compiler');
const cssModules = require('../index.js');

module.exports = async ({ source, localIdentName }) => {
const { code } = await svelte.preprocess(
module.exports = async ({ source }, options) => {
const { code } = await svelte.preprocess(
source,
[
cssModules({
localIdentName,
})
cssModules(options)
],
{ filename : 'src/App.svelte' }
);
Expand Down
16 changes: 16 additions & 0 deletions test/getlocalident.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const compiler = require('./compiler.js');

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

test('Customize generated classname from getLocalIdent', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-123456MC',
getLocalIdent: (context, { interpolatedName }) => {
return interpolatedName.toLowerCase();
}
});

expect(output).toBe('<style>:global(.red-123456mc) { color: red; }</style>\n<span class="red-123456mc">Red</span>');
});
2 changes: 2 additions & 0 deletions test/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sourceReactiveClass = '<style>.red { color: red; }</style>\n<span class:$.
test('Replace path on regular class', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[path][name]__[local]',
});

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

Expand Down
2 changes: 2 additions & 0 deletions test/remove.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const expectedOutput = '<style>.red { color: red; }</style>\n<span class="">Blue
test('Remove unused CSS Modules from HTML attribute', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-123456',
});

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

Expand Down
6 changes: 6 additions & 0 deletions test/replace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.red
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-123456',
});

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

Expand All @@ -23,13 +25,15 @@ test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS classNa
test('Avoid generated class to start with a non character', async () => {
const output = await compiler({
source,
}, {
localIdentName: '1[local]',
});
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
});
test('[Shorthand] Avoid generated class to start with a non character', async () => {
const output = await compiler({
source: sourceShorthand,
}, {
localIdentName: '1[local]',
});
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
Expand All @@ -38,13 +42,15 @@ test('[Shorthand] Avoid generated class to start with a non character', async ()
test('Avoid generated class to end with a hyphen', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-',
});
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
});
test('[Shorthand] Avoid generated class to end with a hyphen', async () => {
const output = await compiler({
source: sourceShorthand,
}, {
localIdentName: '[local]-',
});
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
Expand Down
2 changes: 2 additions & 0 deletions test/target.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const expectedOutput =
test('Target proper className from lookalike classNames', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-123',
});

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

Expand Down
2 changes: 2 additions & 0 deletions test/toggle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const expectedOutput =
test('Generate CSS Modules className on class binding', async () => {
const output = await compiler({
source,
}, {
localIdentName: '[local]-123456',
});

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

Expand Down