Skip to content

Commit 7a3cca5

Browse files
author
pussha
committed
changes to css-loader loader file
1 parent 1703721 commit 7a3cca5

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

lib/getLocalIdent.js

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra
4-
*/
5-
var loaderUtils = require("loader-utils");
6-
var path = require("path");
7-
8-
module.exports = function getLocalIdent(loaderContext, localIdentName, localName, options) {
9-
if(!options.context) {
10-
if (loaderContext.rootContext) {
11-
options.context = loaderContext.rootContext;
12-
} else if (loaderContext.options && typeof loaderContext.options.context === "string") {
13-
options.context = loaderContext.options.context;
14-
} else {
15-
options.context = loaderContext.context;
16-
}
17-
}
18-
var request = path.relative(options.context, loaderContext.resourcePath);
19-
options.content = options.hashPrefix + request + "+" + localName;
20-
localIdentName = localIdentName.replace(/\[local\]/gi, localName);
21-
var hash = loaderUtils.interpolateName(loaderContext, localIdentName, options);
22-
return hash.replace(new RegExp("[^a-zA-Z0-9\\-_\u00A0-\uFFFF]", "g"), "-").replace(/^((-?[0-9])|--)/, "_$1");
1+
import incstr from 'incstr';
2+
3+
//region CSS Scope Minify
4+
const createUniqueIdGenerator = () => {
5+
const index = {};
6+
7+
const generateNextId = incstr.idGenerator({
8+
// Removed "d" letter to avoid accidental "ad" construct.
9+
// @see https://medium.com/@mbrevda/just-make-sure-ad-isnt-being-used-as-a-class-name-prefix-or-you-might-suffer-the-wrath-of-the-558d65502793
10+
// NOTE: allow "d" letter due to combination of UPPERCASES-lowercases
11+
alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789_-'
12+
});
13+
14+
return (name) => {
15+
if (index[name]) {
16+
return index[name];
17+
}
18+
19+
let nextId;
20+
21+
do {
22+
// Class name cannot start with a number.
23+
nextId = generateNextId();
24+
} while (/^[0-9_-]/.test(nextId));
25+
26+
index[name] = generateNextId();
27+
// console.log(`${name} has id = ${index[name]}`);
28+
29+
return index[name];
30+
};
2331
};
32+
33+
const idLocal = createUniqueIdGenerator(), idComponent = createUniqueIdGenerator();
34+
const generateScopedName = (localName, resourcePath) => {
35+
const componentName = resourcePath.split('/').slice(-2).join('/');
36+
return idComponent(componentName).toUpperCase() + idLocal(localName);
37+
};
38+
39+
const getLocalIdent = (context, localIdentName, localName) => generateScopedName(localName, context.resourcePath);
40+
global.getLocalIdent = getLocalIdent;
41+
//endregion

lib/loader.js

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = function(content, map) {
1919
var sourceMap = query.sourceMap || false;
2020
var resolve = createResolver(query.alias);
2121

22+
if(global && global.getLocalIdent){
23+
query.getLocalIdent = global.getLocalIdent;
24+
}
25+
2226
if(sourceMap) {
2327
if (map) {
2428
if (typeof map === "string") {

0 commit comments

Comments
 (0)