Skip to content
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

refactor: better export escaped locals #920

Merged
merged 1 commit into from Apr 10, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"webpack": "^4.0.0"
},
"dependencies": {
"cssesc": "^3.0.0",
"icss-utils": "^4.1.0",
"loader-utils": "^1.2.3",
"camelcase": "^5.2.0",
Expand Down
66 changes: 10 additions & 56 deletions src/utils.js
Expand Up @@ -7,6 +7,7 @@ import path from 'path';
import cc from 'camelcase';
import loaderUtils from 'loader-utils';
import normalizePath from 'normalize-path';
import cssesc from 'cssesc';

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

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

// eslint-disable-next-line no-param-reassign
localIdentName = localIdentName.replace(/\[local\]/gi, localName);

const hash = loaderUtils.interpolateName(
loaderContext,
localIdentName,
options
);

return normalizeIdentifier(hash);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line makes the replacing the / in path to - no longer working.
Is that on purpose?

// Using `[path]` placeholder outputs `/` we need escape their
// Also directories can contains invalid characters for css we need escape their too
return cssesc(
loaderUtils
.interpolateName(loaderContext, localIdentName, options)
// For `[hash]` placeholder
.replace(/^((-?[0-9])|--)/, '_$1'),
{ isIdentifier: true }
).replace(/\\\[local\\\]/gi, localName);
}

function getFilter(filter, resourcePath, defaultFilter = null) {
Expand All @@ -105,52 +105,6 @@ function getFilter(filter, resourcePath, defaultFilter = null) {
};
}

function normalizeIdentifier(value) {
const escapedSymbols = [
'~',
'!',
'@',
'#',
'$',
'%',
'&',
'^',
'*',
'(',
')',
'{',
'}',
'[',
']',
'`',
'/',
'=',
'?',
'+',
'\\',
'|',
'-',
'_',
':',
';',
"'",
'"',
',',
'<',
'.',
'>',
];

const identifiersRegExp = new RegExp(
`[^a-zA-Z0-9${escapedSymbols.join('\\')}\\-_\u00A0-\uFFFF]`,
'g'
);

return value
.replace(identifiersRegExp, '-')
.replace(/^((-?[0-9])|--)/, '_$1');
}

export {
getImportPrefix,
getLocalIdent,
Expand Down