Skip to content

Commit b0eaf61

Browse files
Update getLocalIdent.js
1 parent 1703721 commit b0eaf61

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

lib/getLocalIdent.js

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,64 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
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");
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");
23+
// };
24+
25+
import incstr from 'incstr';
26+
27+
//region CSS Scope Minify
28+
const createUniqueIdGenerator = () => {
29+
const index = {};
30+
31+
const generateNextId = incstr.idGenerator({
32+
// Removed "d" letter to avoid accidental "ad" construct.
33+
// @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
34+
// NOTE: allow "d" letter due to combination of UPPERCASES-lowercases
35+
alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789_-'
36+
});
37+
38+
return (name) => {
39+
if (index[name]) {
40+
return index[name];
41+
}
42+
43+
let nextId;
44+
45+
do {
46+
// Class name cannot start with a number.
47+
nextId = generateNextId();
48+
} while (/^[0-9_-]/.test(nextId));
49+
50+
index[name] = generateNextId();
51+
// console.log(`${name} has id = ${index[name]}`);
52+
53+
return index[name];
54+
};
2355
};
56+
57+
const idLocal = createUniqueIdGenerator(), idComponent = createUniqueIdGenerator();
58+
const generateScopedName = (localName, resourcePath) => {
59+
const componentName = resourcePath.split('/').slice(-2).join('/');
60+
return idComponent(componentName).toUpperCase() + idLocal(localName);
61+
};
62+
63+
const getLocalIdent = (context, localIdentName, localName) => generateScopedName(localName, context.resourcePath);
64+
global.getLocalIdent = getLocalIdent;
65+
//endregion

0 commit comments

Comments
 (0)