-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (47 loc) · 1.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use strict";
const allowedCharactersFirst = "abcdefhijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const allowedCharactersAfter = allowedCharactersFirst + "0123456789-_";
class Minifier {
constructor(options = {}) {
options.blacklist = options.blacklist || [/^ad$/];
this.options = options;
this.idents = new Map();
this.indexes = [0];
// Fix scope when called by css-loader.
this.getLocalIdent = this.getLocalIdent.bind(this);
}
getNextIdent(key) {
const { idents, indexes } = this;
const { blacklist } = this.options;
const usedIdents = Array.from(this.idents.values());
let ident = "";
do {
ident = indexes
.map((i, arrIndex) => {
// Limit the index for allowedCharactersFirst to it's maximum index.
const maxIndexFirst = Math.min(i, allowedCharactersFirst.length - 1);
return arrIndex === 0 ? allowedCharactersFirst[maxIndexFirst] : allowedCharactersAfter[i]
})
.join("");
let i = indexes.length;
while (i--) {
indexes[i] += 1;
if (indexes[i] === allowedCharactersAfter.length) {
indexes[i] = 0;
if (i === 0) indexes.push(0);
} else break;
}
} while (usedIdents.includes(ident) || blacklist.some(regex => ident.match(regex)));
idents.set(key, ident);
return ident;
}
getLocalIdent(context, _, localName) {
const prefix = this.options.prefix || "";
const key = [context.resourcePath, localName].join("-");
const ident = this.idents.get(key) || this.getNextIdent(key);
return prefix + ident;
}
}
const createMinifier = (options) => new Minifier(options).getLocalIdent;
module.exports = createMinifier;
module.exports.Minifier = Minifier;