Skip to content

Commit 9eb663f

Browse files
committed
Updates the class name generation logic to be independent of css-loader library
This change is necessary because the latest release, [email protected], closes the opportunity to access its default class name generation function. Thus, the necessary code is copy/pasted directly into this library. It should introduce no changes in the actual class name generation algo.
1 parent 0468a70 commit 9eb663f

File tree

4 files changed

+81
-129
lines changed

4 files changed

+81
-129
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"@dr.pogodin/postcss-modules-parser": "^1.2.2",
66
"ajv": "^6.12.6",
77
"ajv-keywords": "^3.2.0",
8-
"css-loader": "^4.3.0",
8+
"cssesc": "^3.0.0",
9+
"loader-utils": "^2.0.0",
910
"postcss": "^8.1.1",
1011
"postcss-modules": "^3.2.2",
1112
"postcss-modules-extract-imports": "^3.0.0",

src/getLocalIdent.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* getLocalIdent() function taken from [email protected].
3+
*/
4+
5+
import path from 'path';
6+
import cssesc from 'cssesc';
7+
import {
8+
interpolateName,
9+
} from 'loader-utils';
10+
11+
// eslint-disable-next-line no-control-regex
12+
const filenameReservedRegex = /["*/:<>?\\|]/g;
13+
// eslint-disable-next-line no-control-regex
14+
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g;
15+
16+
const normalizePath = (file) => {
17+
return path.sep === '\\' ? file.replace(/\\/g, '/') : file;
18+
};
19+
20+
const escapeLocalIdent = (localident) => {
21+
return cssesc(
22+
localident
23+
24+
// For `[hash]` placeholder
25+
.replace(/^((-?\d)|--)/, '_$1')
26+
.replace(filenameReservedRegex, '-')
27+
.replace(reControlChars, '-')
28+
.replace(/\./g, '-'),
29+
{isIdentifier: true},
30+
);
31+
};
32+
33+
export default function getLocalIdent (
34+
loaderContext,
35+
localIdentName,
36+
localName,
37+
options,
38+
) {
39+
const {context, hashPrefix} = options;
40+
const {resourcePath} = loaderContext;
41+
const request = normalizePath(path.relative(context, resourcePath));
42+
43+
// eslint-disable-next-line no-param-reassign
44+
options.content = `${hashPrefix + request}\u0000${localName}`;
45+
46+
const ident = interpolateName(loaderContext, localIdentName, options)
47+
.replace(/\[local]/gi, localName);
48+
49+
return escapeLocalIdent(ident);
50+
}

src/requireCssModule.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import {
88
resolve,
99
} from 'path';
1010
import Parser from '@dr.pogodin/postcss-modules-parser';
11-
import {
12-
getModulesOptions,
13-
} from 'css-loader/dist/utils';
1411
import postcss from 'postcss';
1512
import ExtractImports from 'postcss-modules-extract-imports';
1613
import LocalByDefault from 'postcss-modules-local-by-default';
1714
import newScopePlugin from 'postcss-modules-scope';
1815
import Values from 'postcss-modules-values';
16+
import getLocalIdent from './getLocalIdent';
1917
import optionsDefaults from './schemas/optionsDefaults';
2018
import type {
2119
GenerateScopedNameConfigurationType,
@@ -46,12 +44,6 @@ type OptionsType = {|
4644
context?: string,
4745
|};
4846

49-
// As of now CSS loader does not export its default getLocalIdent(..) function,
50-
// which we need to generate the classnames. However, this goofy way to get it
51-
// works fine. If one day internal changes in css-loader break this workaround,
52-
// it will be necessary just to commit them a patch which exports the function.
53-
const {getLocalIdent} = getModulesOptions({modules: true}, {});
54-
5547
const getFiletypeOptions = (cssSourceFilePath: string, filetypes: FiletypesConfigurationType): ?FiletypeOptionsType => {
5648
const extension = cssSourceFilePath.slice(cssSourceFilePath.lastIndexOf('.'));
5749
const filetype = filetypes ? filetypes[extension] : null;

0 commit comments

Comments
 (0)