-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathaccumulate.js
64 lines (52 loc) · 900 Bytes
/
accumulate.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
const dropcss = require('../dist/dropcss.cjs.js');
// super mega-huge combined stylesheet
let css = `
em {
color: red;
}
p {
font-weight: bold;
}
.foo {
font-size: 10pt;
}
`;
// html of page (or state) A
let htmlA = `
<html>
<head></head>
<body>
<em>Hello World!</em>
</body>
</html>
`;
// html of page (or state) B
let htmlB = `
<html>
<head></head>
<body>
<p>Soft Kitties!</p>
</body>
</html>
`;
// whitelist
let whitelist = new Set();
let resA = dropcss({
css,
html: htmlA,
});
// accumulate retained A selectors
resA.sels.forEach(sel => whitelist.add(sel));
let resB = dropcss({
css,
html: htmlB,
});
// accumulate retained B selectors
resB.sels.forEach(sel => whitelist.add(sel));
// final purge relying only on accumulated whitelist
let cleaned = dropcss({
html: '',
css,
shouldDrop: sel => !whitelist.has(sel),
});
console.log(cleaned.css);