* 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
(
cssOptions: Array<string | RawCSS>,
selectors: ExtractorResultSets,
)
| 646 | * @param selectors - set of extracted css selectors |
| 647 | */ |
| 648 | public async getPurgedCSS( |
| 649 | cssOptions: Array<string | RawCSS>, |
| 650 | selectors: ExtractorResultSets, |
| 651 | ): Promise<ResultPurge[]> { |
| 652 | const sources = []; |
| 653 | |
| 654 | // resolve any globs |
| 655 | const processedOptions: Array<string | RawCSS> = []; |
| 656 | for (const option of cssOptions) { |
| 657 | if (typeof option === "string") { |
| 658 | processedOptions.push( |
| 659 | ...glob.sync(option, { |
| 660 | onlyFiles: true, |
| 661 | ignore: this.options.skippedContentGlobs, |
| 662 | }), |
| 663 | ); |
| 664 | } else { |
| 665 | processedOptions.push(option); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | for (const option of processedOptions) { |
| 670 | const cssContent = |
| 671 | typeof option === "string" |
| 672 | ? this.options.stdin |
| 673 | ? option |
| 674 | : await asyncFs.readFile(option, "utf-8") |
| 675 | : option.raw; |
| 676 | const isFromFile = typeof option === "string" && !this.options.stdin; |
| 677 | const root = postcss.parse(cssContent, { |
| 678 | from: isFromFile ? option : undefined, |
| 679 | }); |
| 680 | |
| 681 | // purge unused selectors |
| 682 | this.walkThroughCSS(root, selectors); |
| 683 | |
| 684 | if (this.options.fontFace) this.removeUnusedFontFaces(); |
| 685 | if (this.options.keyframes) this.removeUnusedKeyframes(); |
| 686 | if (this.options.variables) this.removeUnusedCSSVariables(); |
| 687 | |
| 688 | const postCSSResult = root.toResult({ |
| 689 | map: this.options.sourceMap, |
| 690 | to: |
| 691 | typeof this.options.sourceMap === "object" && |
| 692 | this.options.sourceMap.to |
| 693 | ? this.options.sourceMap.to |
| 694 | : isFromFile |
| 695 | ? option |
| 696 | : undefined, |
| 697 | }); |
| 698 | const result: ResultPurge = { |
| 699 | css: postCSSResult.toString(), |
| 700 | file: typeof option === "string" ? option : option.name, |
| 701 | }; |
| 702 | |
| 703 | if (this.options.sourceMap) { |
| 704 | result.sourceMap = postCSSResult.map?.toString(); |
| 705 | } |
no test coverage detected