MCPcopy
hub / github.com/FullHuman/purgecss / getPurgedCSS

Method getPurgedCSS

packages/purgecss/src/index.ts:648–721  ·  view source on GitHub ↗

* 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,
  )

Source from the content-addressed store, hash-verified

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 }

Callers 1

purgeMethod · 0.95

Calls 4

walkThroughCSSMethod · 0.95
removeUnusedFontFacesMethod · 0.95
removeUnusedKeyframesMethod · 0.95

Tested by

no test coverage detected