Skip to content

Commit a380d22

Browse files
author
Michael Vurchio
committed
Replace unwanted characters from [path]
`[path]` is now working properly
1 parent a8bbe26 commit a380d22

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const path = require('path')
2-
const { interpolateName } = require('loader-utils')
1+
const path = require('path');
2+
const cssesc = require('cssesc');
3+
const { interpolateName } = require('loader-utils');
34

45
const pluginOptions = {
56
includePaths: [],
67
localIdentName: '[local]-[hash:base64:6]'
7-
}
8+
};
89

910
const regex = {
1011
module: /\$(style)?\.(:?[\w\d-]*)/gm,
@@ -17,27 +18,35 @@ const regex = {
1718
let moduleClasses = {};
1819

1920
function generateName(resourcePath, styles, className) {
20-
const filePath = resourcePath
21-
const fileName = path.basename(filePath)
21+
const filePath = resourcePath;
22+
const fileName = path.basename(filePath);
2223
const localName = pluginOptions.localIdentName.length
2324
? pluginOptions.localIdentName.replace(/\[local\]/gi, () => className)
24-
: className
25+
: className;
2526

26-
const content = `${styles}-${filePath}-${fileName}-${className}`
27+
const content = `${styles}-${filePath}-${fileName}-${className}`;
2728

28-
let interpolatedName = interpolateName({ resourcePath }, localName, { content })
29+
let interpolatedName = cssesc(
30+
interpolateName({ resourcePath }, localName, { content })
31+
.replace(/\./g, '-')
32+
);
33+
34+
// replace unwanted characters from [path]
35+
if (/[<>:"/\\|?*]/g.test(interpolatedName)) {
36+
interpolatedName = interpolatedName.replace(regex.pathUnallowed, '_');
37+
}
2938

3039
// prevent class error when the generated classname starts from a non word charater
3140
if (/^(?![a-zA-Z_])/.test(interpolatedName)) {
32-
interpolatedName = `_${interpolatedName}`
41+
interpolatedName = `_${interpolatedName}`;
3342
}
3443

3544
// prevent svelte "Unused CSS selector" warning when the generated classname ends by `-`
3645
if (interpolatedName.slice(-1) === '-') {
37-
interpolatedName = interpolatedName.slice(0, -1)
46+
interpolatedName = interpolatedName.slice(0, -1);
3847
}
3948

40-
return interpolatedName
49+
return interpolatedName;
4150
}
4251

4352
const markup = async ({ content, filename }) => {

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"url": "https://github.com/micantoine/svelte-preprocess-cssmodules.git"
2828
},
2929
"dependencies": {
30+
"cssesc": "^3.0.0",
3031
"loader-utils": "^2.0.0"
3132
},
3233
"devDependencies": {

0 commit comments

Comments
 (0)