/
index.ts
787 lines (708 loc) · 21.2 KB
/
index.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import * as postcss from "postcss";
import selectorParser from "postcss-selector-parser";
import * as fs from "fs";
import { promisify } from "util";
import glob from "glob";
import path from "path";
import { defaultOptions } from "./options";
export { defaultOptions } from "./options";
import {
Options,
ResultPurge,
RawContent,
Extractors,
ExtractorFunction,
IgnoreType,
AtRules,
RawCSS,
UserDefinedOptions,
ExtractorResultDetailed,
UserDefinedSafelist,
ComplexSafelist,
} from "./types";
import {
CONFIG_FILENAME,
ERROR_CONFIG_FILE_LOADING,
IGNORE_ANNOTATION_NEXT,
IGNORE_ANNOTATION_START,
IGNORE_ANNOTATION_END,
IGNORE_ANNOTATION_CURRENT,
} from "./constants";
import { CSS_SAFELIST } from "./internal-safelist";
import VariablesStructure from "./VariablesStructure";
import ExtractorResultSets from "./ExtractorResultSets";
const asyncFs = {
access: promisify(fs.access),
readFile: promisify(fs.readFile),
};
export function standardizeSafelist(
userDefinedSafelist: UserDefinedSafelist = []
): Required<ComplexSafelist> {
if (Array.isArray(userDefinedSafelist)) {
return {
...defaultOptions.safelist,
standard: userDefinedSafelist,
};
}
return {
...defaultOptions.safelist,
...userDefinedSafelist,
};
}
/**
* Load the configuration file from the path
* @param configFile Path of the config file
*/
export async function setOptions(
configFile: string = CONFIG_FILENAME
): Promise<Options> {
let options: Options;
try {
const t = path.join(process.cwd(), configFile);
options = await import(t);
} catch (err) {
throw new Error(`${ERROR_CONFIG_FILE_LOADING} ${err.message}`);
}
return {
...defaultOptions,
...options,
safelist: standardizeSafelist(options.safelist),
};
}
/**
* Use the extract function to get the list of selectors
* @param content content (e.g. html file)
* @param extractor PurgeCSS extractor used to extract the selectors
*/
async function extractSelectors(
content: string,
extractor: ExtractorFunction
): Promise<ExtractorResultSets> {
return new ExtractorResultSets(await extractor(content));
}
/**
* Check if the node is a css comment indication to ignore the selector rule
* @param node node of postcss AST
* @param type type of css comment
*/
function isIgnoreAnnotation(node: postcss.Comment, type: IgnoreType): boolean {
switch (type) {
case "next":
return node.text.includes(IGNORE_ANNOTATION_NEXT);
case "start":
return node.text.includes(IGNORE_ANNOTATION_START);
case "end":
return node.text.includes(IGNORE_ANNOTATION_END);
}
}
/**
* Check if the node correspond to an empty css rule
* @param node node of postcss AST
*/
function isRuleEmpty(node: postcss.Container): boolean {
if (
(node.type === "rule" && !node.selector) ||
(node.nodes && !node.nodes.length) ||
(node.type === "atrule" &&
((!node.nodes && !node.params) ||
(!node.params && node.nodes && !node.nodes.length)))
) {
return true;
}
return false;
}
/**
* Check if the node has a css comment indicating to ignore the current selector rule
* @param rule rule of postcss AST
*/
function hasIgnoreAnnotation(rule: postcss.Rule): boolean {
let found = false;
rule.walkComments((node) => {
if (
node &&
node.type === "comment" &&
node.text.includes(IGNORE_ANNOTATION_CURRENT)
) {
found = true;
node.remove();
}
});
return found;
}
/**
* Merge two extractor selectors
* @param extractorSelectorsA extractor selectors A
* @param extractorSelectorsB extractor selectors B
*/
export function mergeExtractorSelectors(
...extractors: (ExtractorResultDetailed | ExtractorResultSets)[]
): ExtractorResultSets {
const result = new ExtractorResultSets([]);
extractors.forEach(result.merge, result);
return result;
}
/**
* Strips quotes of a string
* @param str string to be stripped
*/
function stripQuotes(str: string): string {
return str.replace(/(^["'])|(["']$)/g, "");
}
/**
* Returns true if the attribute is found in the extractor selectors
* @param attributeNode node of type `attribute`
* @param selectors extractor selectors
*/
function isAttributeFound(
attributeNode: selectorParser.Attribute,
selectors: ExtractorResultSets
): boolean {
if (!selectors.hasAttrName(attributeNode.attribute)) {
return false;
}
if (typeof attributeNode.value === "undefined") {
return true;
}
switch (attributeNode.operator) {
case "$=":
return selectors.hasAttrSuffix(attributeNode.value);
case "~=":
case "*=":
return selectors.hasAttrSubstr(attributeNode.value);
case "=":
return selectors.hasAttrValue(attributeNode.value);
case "|=":
case "^=":
return selectors.hasAttrPrefix(attributeNode.value);
default:
return true;
}
}
/**
* Returns true if the class is found in the extractor selectors
* @param classNode node of type `class`
* @param selectors extractor selectors
*/
function isClassFound(
classNode: selectorParser.ClassName,
selectors: ExtractorResultSets
): boolean {
return selectors.hasClass(classNode.value);
}
/**
* Returns true if the identifier is found in the extractor selectors
* @param identifierNode node of type `identifier`
* @param selectors extractor selectors
*/
function isIdentifierFound(
identifierNode: selectorParser.Identifier,
selectors: ExtractorResultSets
): boolean {
return selectors.hasId(identifierNode.value);
}
/**
* Returns true if the tag is found in the extractor selectors
* @param tagNode node of type `tag`
* @param selectors extractor selectors
*/
function isTagFound(
tagNode: selectorParser.Tag,
selectors: ExtractorResultSets
): boolean {
return selectors.hasTag(tagNode.value);
}
/**
* Returns true if the selector is inside a pseudo class
* (e.g. :nth-child, :nth-of-type, :only-child, :not)
* @param selector selector
*/
function isInPseudoClass(selector: selectorParser.Node): boolean {
return (
(selector.parent &&
selector.parent.type === "pseudo" &&
selector.parent.value.startsWith(":")) ||
false
);
}
function matchAll(str: string, regexp: RegExp): RegExpMatchArray[] {
const matches: RegExpMatchArray[] = [];
str.replace(regexp, function () {
// eslint-disable-next-line prefer-rest-params
const args = arguments;
const match: RegExpMatchArray = Array.prototype.slice.call(args, 0, -2);
match.input = args[args.length - 1];
match.index = args[args.length - 2];
matches.push(match);
return str;
});
return matches;
}
class PurgeCSS {
private ignore = false;
private atRules: AtRules = {
fontFace: [],
keyframes: [],
};
private usedAnimations: Set<string> = new Set();
private usedFontFaces: Set<string> = new Set();
public selectorsRemoved: Set<string> = new Set();
private variablesStructure: VariablesStructure = new VariablesStructure();
public options: Options = defaultOptions;
private collectDeclarationsData(declaration: postcss.Declaration): void {
const { prop, value } = declaration;
// collect css properties data
if (this.options.variables) {
const usedVariablesMatchesInDeclaration = matchAll(
value,
/var\((.+?)[,)]/g
);
if (prop.startsWith("--")) {
this.variablesStructure.addVariable(declaration);
if (usedVariablesMatchesInDeclaration.length > 0) {
this.variablesStructure.addVariableUsage(
declaration,
usedVariablesMatchesInDeclaration
);
}
} else {
if (usedVariablesMatchesInDeclaration.length > 0) {
this.variablesStructure.addVariableUsageInProperties(
usedVariablesMatchesInDeclaration
);
}
}
}
// collect keyframes data
if (this.options.keyframes) {
if (prop === "animation" || prop === "animation-name") {
for (const word of value.split(/[\s,]+/)) {
this.usedAnimations.add(word);
}
return;
}
}
// collect font faces data
if (this.options.fontFace) {
if (prop === "font-family") {
for (const fontName of value.split(",")) {
const cleanedFontFace = stripQuotes(fontName.trim());
this.usedFontFaces.add(cleanedFontFace);
}
}
return;
}
}
/**
* Get the extractor corresponding to the extension file
* @param filename Name of the file
* @param extractors Array of extractors definition
*/
private getFileExtractor(
filename: string,
extractors: Extractors[]
): ExtractorFunction {
const extractorObj = extractors.find((extractor) =>
extractor.extensions.find((ext) => filename.endsWith(ext))
);
return typeof extractorObj === "undefined"
? this.options.defaultExtractor
: extractorObj.extractor;
}
/**
* Extract the selectors present in the files using a purgecss extractor
* @param files Array of files path or glob pattern
* @param extractors Array of extractors
*/
public async extractSelectorsFromFiles(
files: string[],
extractors: Extractors[]
): Promise<ExtractorResultSets> {
const selectors = new ExtractorResultSets([]);
for (const globfile of files) {
let filesNames: string[] = [];
try {
await asyncFs.access(globfile, fs.constants.F_OK);
filesNames.push(globfile);
} catch (err) {
filesNames = glob.sync(globfile, { nodir: true });
}
for (const file of filesNames) {
const content = await asyncFs.readFile(file, "utf-8");
const extractor = this.getFileExtractor(file, extractors);
const extractedSelectors = await extractSelectors(content, extractor);
selectors.merge(extractedSelectors);
}
}
return selectors;
}
/**
* Extract the selectors present in the passed string using a PurgeCSS extractor
* @param content Array of content
* @param extractors Array of extractors
*/
public async extractSelectorsFromString(
content: RawContent[],
extractors: Extractors[]
): Promise<ExtractorResultSets> {
const selectors = new ExtractorResultSets([]);
for (const { raw, extension } of content) {
const extractor = this.getFileExtractor(`.${extension}`, extractors);
const extractedSelectors = await extractSelectors(raw, extractor);
selectors.merge(extractedSelectors);
}
return selectors;
}
/**
* Evaluate at-rule and register it for future reference
* @param node node of postcss AST
*/
private evaluateAtRule(node: postcss.AtRule): void {
// keyframes
if (this.options.keyframes && node.name.endsWith("keyframes")) {
this.atRules.keyframes.push(node);
return;
}
// font-face
if (this.options.fontFace && node.name === "font-face" && node.nodes) {
for (const childnode of node.nodes) {
if (childnode.type === "decl" && childnode.prop === "font-family") {
this.atRules.fontFace.push({
name: stripQuotes(childnode.value),
node,
});
}
}
}
}
/**
* Evaluate css selector and decide if it should be removed or not
* @param node node of postcss AST
* @param selectors selectors used in content files
*/
private async evaluateRule(
node: postcss.Node,
selectors: ExtractorResultSets
): Promise<void> {
// exit if is in ignoring state activated by an ignore comment
if (this.ignore) {
return;
}
// exit if the previous annotation is a ignore next line comment
const annotation = node.prev();
if (
annotation &&
annotation.type === "comment" &&
isIgnoreAnnotation(annotation, "next")
) {
annotation.remove();
return;
}
// exit if it is inside a keyframes
if (
node.parent &&
node.parent.type === "atrule" &&
node.parent.name === "keyframes"
) {
return;
}
// exit if it is not a rule
if (node.type !== "rule") {
return;
}
// exit if it has an ignore rule comment inside
if (hasIgnoreAnnotation(node)) {
return;
}
let keepSelector = true;
node.selector = selectorParser((selectorsParsed) => {
selectorsParsed.walk((selector) => {
if (selector.type !== "selector") {
return;
}
keepSelector = this.shouldKeepSelector(selector, selectors);
if (!keepSelector) {
if (this.options.rejected)
this.selectorsRemoved.add(selector.toString());
selector.remove();
}
});
}).processSync(node.selector);
// declarations
if (keepSelector && typeof node.nodes !== "undefined") {
for (const childNode of node.nodes) {
if (childNode.type !== "decl") continue;
this.collectDeclarationsData(childNode);
}
}
// remove empty rules
const parent = node.parent;
if (!node.selector) node.remove();
if (isRuleEmpty(parent)) parent.remove();
}
/**
* Get the purged version of the css based on the files
* @param cssOptions css options, files or raw strings
* @param selectors set of extracted css selectors
*/
public async getPurgedCSS(
cssOptions: Array<string | RawCSS>,
selectors: ExtractorResultSets
): Promise<ResultPurge[]> {
const sources = [];
// resolve any globs
const processedOptions: Array<string | RawCSS> = [];
for (const option of cssOptions) {
if (typeof option === "string") {
processedOptions.push(...glob.sync(option, { nodir: true }));
} else {
processedOptions.push(option);
}
}
for (const option of processedOptions) {
const cssContent =
typeof option === "string"
? this.options.stdin
? option
: await asyncFs.readFile(option, "utf-8")
: option.raw;
const root = postcss.parse(cssContent);
// purge unused selectors
this.walkThroughCSS(root, selectors);
if (this.options.fontFace) this.removeUnusedFontFaces();
if (this.options.keyframes) this.removeUnusedKeyframes();
if (this.options.variables) this.removeUnusedCSSVariables();
const result: ResultPurge = {
css: root.toString(),
file: typeof option === "string" ? option : undefined,
};
if (typeof option === "string") {
result.file = option;
}
if (this.options.rejected) {
result.rejected = Array.from(this.selectorsRemoved);
this.selectorsRemoved.clear();
}
sources.push(result);
}
return sources;
}
/**
* Check if the keyframe is safelisted with the option safelist keyframes
* @param keyframesName name of the keyframe animation
*/
private isKeyframesSafelisted(keyframesName: string): boolean {
return this.options.safelist.keyframes.some((safelistItem) => {
return typeof safelistItem === "string"
? safelistItem === keyframesName
: safelistItem.test(keyframesName);
});
}
/**
* Check if the selector is safelisted with the option safelist standard
* @param selector css selector
*/
private isSelectorSafelisted(selector: string): boolean {
const isSafelisted = this.options.safelist.standard.some((safelistItem) => {
return typeof safelistItem === "string"
? safelistItem === selector
: safelistItem.test(selector);
});
return CSS_SAFELIST.includes(selector) || isSafelisted;
}
/**
* Check if the selector is safelisted with the option safelist deep
* @param selector selector
*/
private isSelectorSafelistedDeep(selector: string): boolean {
return this.options.safelist.deep.some((safelistItem) =>
safelistItem.test(selector)
);
}
/**
* Check if the selector is safelisted with the option safelist greedy
* @param selector selector
*/
private isSelectorSafelistedGreedy(selector: string): boolean {
return this.options.safelist.greedy.some((safelistItem) =>
safelistItem.test(selector)
);
}
/**
* Remove unused css
* @param userOptions PurgeCSS options
*/
public async purge(
userOptions: UserDefinedOptions | string | undefined
): Promise<ResultPurge[]> {
this.options =
typeof userOptions !== "object"
? await setOptions(userOptions)
: {
...defaultOptions,
...userOptions,
safelist: standardizeSafelist(userOptions.safelist),
};
const { content, css, extractors, safelist } = this.options;
if (this.options.variables) {
this.variablesStructure.safelist = safelist.variables || [];
}
const fileFormatContents = content.filter(
(o) => typeof o === "string"
) as string[];
const rawFormatContents = content.filter(
(o) => typeof o === "object"
) as RawContent[];
const cssFileSelectors = await this.extractSelectorsFromFiles(
fileFormatContents,
extractors
);
const cssRawSelectors = await this.extractSelectorsFromString(
rawFormatContents,
extractors
);
return this.getPurgedCSS(
css,
mergeExtractorSelectors(cssFileSelectors, cssRawSelectors)
);
}
/**
* Remove unused CSS variables
*/
public removeUnusedCSSVariables(): void {
this.variablesStructure.removeUnused();
}
/**
* Remove unused font-faces
*/
public removeUnusedFontFaces(): void {
for (const { name, node } of this.atRules.fontFace) {
if (!this.usedFontFaces.has(name)) {
node.remove();
}
}
}
/**
* Remove unused keyframes
*/
public removeUnusedKeyframes(): void {
for (const node of this.atRules.keyframes) {
if (
!this.usedAnimations.has(node.params) &&
!this.isKeyframesSafelisted(node.params)
) {
node.remove();
}
}
}
/**
* Transform a selector node into a string
*/
private getSelectorValue(selector: selectorParser.Node): string | undefined {
return (
(selector.type === "attribute" && selector.attribute) || selector.value
);
}
/**
* Determine if the selector should be kept, based on the selectors found in the files
* @param selector set of css selectors found in the content files or string
* @param selectorsFromExtractor selectors in the css rule
*/
private shouldKeepSelector(
selector: selectorParser.Selector,
selectorsFromExtractor: ExtractorResultSets
): boolean {
// ignore the selector if it is inside a pseudo class
if (isInPseudoClass(selector)) return true;
// if there is any greedy safelist pattern, run all the selector parts through them
// if there is any match, return true
if (this.options.safelist.greedy.length > 0) {
const selectorParts = selector.nodes.map(this.getSelectorValue);
if (
selectorParts.some(
(selectorPart) =>
selectorPart && this.isSelectorSafelistedGreedy(selectorPart)
)
) {
return true;
}
}
let isPresent = false;
for (const selectorNode of selector.nodes) {
const selectorValue = this.getSelectorValue(selectorNode);
// if the selector is safelisted with children
// returns true to keep all children selectors
if (selectorValue && this.isSelectorSafelistedDeep(selectorValue)) {
return true;
}
// The selector is found in the internal and user-defined safelist
if (
selectorValue &&
(CSS_SAFELIST.includes(selectorValue) ||
this.isSelectorSafelisted(selectorValue))
) {
isPresent = true;
continue;
}
switch (selectorNode.type) {
case "attribute":
// `value` is a dynamic attribute, highly used in input element
// the choice is to always leave `value` as it can change based on the user
// idem for `checked`, `selected`, `open`
isPresent = ["value", "checked", "selected", "open"].includes(
selectorNode.attribute
)
? true
: isAttributeFound(selectorNode, selectorsFromExtractor);
break;
case "class":
isPresent = isClassFound(selectorNode, selectorsFromExtractor);
break;
case "id":
isPresent = isIdentifierFound(selectorNode, selectorsFromExtractor);
break;
case "tag":
isPresent = isTagFound(selectorNode, selectorsFromExtractor);
break;
default:
continue;
}
// selector is not safelisted
// and it has not been found as an attribute/class/id/tag
if (!isPresent) {
return false;
}
}
return isPresent;
}
/**
* Walk through the CSS AST and remove unused CSS
* @param root root node of the postcss AST
* @param selectors selectors used in content files
*/
public walkThroughCSS(
root: postcss.Root,
selectors: ExtractorResultSets
): void {
root.walk((node) => {
if (node.type === "rule") {
return this.evaluateRule(node, selectors);
}
if (node.type === "atrule") {
return this.evaluateAtRule(node);
}
if (node.type === "comment") {
if (isIgnoreAnnotation(node, "start")) {
this.ignore = true;
// remove ignore annotation
node.remove();
} else if (isIgnoreAnnotation(node, "end")) {
this.ignore = false;
// remove ignore annotation
node.remove();
}
}
});
}
}
export { PurgeCSS };
export default PurgeCSS;