Skip to content

Commit d3a0a3c

Browse files
authoredApr 10, 2019
fix: property handle non css characters in localIdentName (#920)
1 parent fe10758 commit d3a0a3c

File tree

5 files changed

+1248
-184
lines changed

5 files changed

+1248
-184
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"webpack": "^4.0.0"
4343
},
4444
"dependencies": {
45+
"cssesc": "^3.0.0",
4546
"icss-utils": "^4.1.0",
4647
"loader-utils": "^1.2.3",
4748
"camelcase": "^5.2.0",

‎src/utils.js

+10-56
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from 'path';
77
import cc from 'camelcase';
88
import loaderUtils from 'loader-utils';
99
import normalizePath from 'normalize-path';
10+
import cssesc from 'cssesc';
1011

1112
/* eslint-disable line-comment-position */
1213

@@ -79,16 +80,15 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
7980
// eslint-disable-next-line no-param-reassign
8081
options.content = `${options.hashPrefix + request}+${unescape(localName)}`;
8182

82-
// eslint-disable-next-line no-param-reassign
83-
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
84-
85-
const hash = loaderUtils.interpolateName(
86-
loaderContext,
87-
localIdentName,
88-
options
89-
);
90-
91-
return normalizeIdentifier(hash);
83+
// Using `[path]` placeholder outputs `/` we need escape their
84+
// Also directories can contains invalid characters for css we need escape their too
85+
return cssesc(
86+
loaderUtils
87+
.interpolateName(loaderContext, localIdentName, options)
88+
// For `[hash]` placeholder
89+
.replace(/^((-?[0-9])|--)/, '_$1'),
90+
{ isIdentifier: true }
91+
).replace(/\\\[local\\\]/gi, localName);
9292
}
9393

9494
function getFilter(filter, resourcePath, defaultFilter = null) {
@@ -105,52 +105,6 @@ function getFilter(filter, resourcePath, defaultFilter = null) {
105105
};
106106
}
107107

108-
function normalizeIdentifier(value) {
109-
const escapedSymbols = [
110-
'~',
111-
'!',
112-
'@',
113-
'#',
114-
'$',
115-
'%',
116-
'&',
117-
'^',
118-
'*',
119-
'(',
120-
')',
121-
'{',
122-
'}',
123-
'[',
124-
']',
125-
'`',
126-
'/',
127-
'=',
128-
'?',
129-
'+',
130-
'\\',
131-
'|',
132-
'-',
133-
'_',
134-
':',
135-
';',
136-
"'",
137-
'"',
138-
',',
139-
'<',
140-
'.',
141-
'>',
142-
];
143-
144-
const identifiersRegExp = new RegExp(
145-
`[^a-zA-Z0-9${escapedSymbols.join('\\')}\\-_\u00A0-\uFFFF]`,
146-
'g'
147-
);
148-
149-
return value
150-
.replace(identifiersRegExp, '-')
151-
.replace(/^((-?[0-9])|--)/, '_$1');
152-
}
153-
154108
export {
155109
getImportPrefix,
156110
getLocalIdent,

0 commit comments

Comments
 (0)