-
-
Notifications
You must be signed in to change notification settings - Fork 597
/
one-letter-css.js
92 lines (76 loc) · 2.33 KB
/
one-letter-css.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @author denisx <github.com@denisx.com>
*/
const loaderUtils = require('loader-utils');
export default class OneLetterCss {
constructor() {
// Save char symbol start positions
this.a = 'a'.charCodeAt(0);
this.A = 'A'.charCodeAt(0);
// file hashes cache
this.files = {};
/** encoding [a-zA-Z] */
this.symbols = 52;
/** a half of encoding */
this.half = 26;
/** prevent loop-hell */
this.maxLoop = 10;
}
/** encoding by rule count at file, 0 - a, 1 - b, 51 - Z, 52 - ba, etc */
getName(lastUsed) {
const { a, A, symbols, maxLoop, half } = this;
let name = '';
let loop = 0;
let main = lastUsed;
let tail = 0;
while (
((main > 0 && tail >= 0) ||
// first step anyway needed
loop === 0) &&
loop < maxLoop
) {
const newMain = Math.floor(main / symbols);
tail = main % symbols;
name = String.fromCharCode((tail >= half ? A - half : a) + tail) + name;
main = newMain;
loop += 1;
}
return name;
}
getLocalIdent(context, localIdentName, localName) {
const { resourcePath } = context;
const { files } = this;
// check file data at cache by absolute path
let fileShort = files[resourcePath];
// no file data, lets generate and save
if (!fileShort) {
// if we know file position, we must use base52 encoding with '_'
// between rule position and file position
// to avoid collapse hash combination. a_ab vs aa_b
const fileShortName = loaderUtils.interpolateName(
context,
'[hash:base64:8]',
{
content: resourcePath,
}
);
fileShort = { name: fileShortName, lastUsed: -1, ruleNames: {} };
files[resourcePath] = fileShort;
}
// Get generative rule name from this file
let newRuleName = fileShort.ruleNames[localName];
// If no rule - renerate new, and save
if (!newRuleName) {
// Count +1
fileShort.lastUsed += 1;
// Generate new rule name
newRuleName = this.getName(fileShort.lastUsed) + fileShort.name;
// Saved
fileShort.ruleNames[localName] = newRuleName;
}
// If has "local" at webpack settings
const hasLocal = /\[local]/.test(localIdentName);
// If has - add prefix
return hasLocal ? `${localName}__${newRuleName}` : newRuleName;
}
}