Skip to content

Commit 2b74e17

Browse files
committed
Merge branch 'fast-io'
2 parents 2421737 + c43c169 commit 2b74e17

1 file changed

Lines changed: 97 additions & 86 deletions

File tree

packages/purgecss/src/index.ts

Lines changed: 97 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import * as fs from "fs";
10-
import * as glob from "fast-glob";
10+
import glob from "fast-glob";
1111
import * as path from "path";
1212
import * as postcss from "postcss";
1313
import selectorParser from "postcss-selector-parser";
@@ -443,31 +443,36 @@ class PurgeCSS {
443443
): Promise<ExtractorResultSets> {
444444
const selectors = new ExtractorResultSets([]);
445445
const filesNames: string[] = [];
446-
for (const globFile of files) {
447-
try {
448-
await asyncFs.access(globFile, fs.constants.F_OK);
449-
filesNames.push(globFile);
450-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
451-
} catch (err) {
452-
filesNames.push(
453-
...glob.sync(globFile, {
454-
onlyFiles: true,
455-
ignore: this.options.skippedContentGlobs.map((glob) =>
456-
glob.replace(/^\.\//, ""),
457-
),
458-
}),
459-
);
460-
}
461-
}
446+
await Promise.all(
447+
files.map(async (globFile) => {
448+
try {
449+
await asyncFs.access(globFile, fs.constants.F_OK);
450+
filesNames.push(globFile);
451+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
452+
} catch (err) {
453+
filesNames.push(
454+
...(await glob(globFile, {
455+
onlyFiles: true,
456+
ignore: this.options.skippedContentGlobs.map((glob) =>
457+
glob.replace(/^\.\//, ""),
458+
),
459+
})),
460+
);
461+
}
462+
}),
463+
);
464+
462465
if (files.length > 0 && filesNames.length === 0) {
463466
console.warn("No files found from the passed PurgeCSS option 'content'.");
464467
}
465-
for (const file of filesNames) {
466-
const content = await asyncFs.readFile(file, "utf-8");
467-
const extractor = this.getFileExtractor(file, extractors);
468-
const extractedSelectors = await extractSelectors(content, extractor);
469-
selectors.merge(extractedSelectors);
470-
}
468+
await Promise.all(
469+
filesNames.map(async (file) => {
470+
const content = await asyncFs.readFile(file, "utf-8");
471+
const extractor = this.getFileExtractor(file, extractors);
472+
const extractedSelectors = await extractSelectors(content, extractor);
473+
selectors.merge(extractedSelectors);
474+
}),
475+
);
471476
return selectors;
472477
}
473478

@@ -482,11 +487,13 @@ class PurgeCSS {
482487
extractors: Extractors[],
483488
): Promise<ExtractorResultSets> {
484489
const selectors = new ExtractorResultSets([]);
485-
for (const { raw, extension } of content) {
486-
const extractor = this.getFileExtractor(`.${extension}`, extractors);
487-
const extractedSelectors = await extractSelectors(raw, extractor);
488-
selectors.merge(extractedSelectors);
489-
}
490+
await Promise.all(
491+
content.map(async ({ raw, extension }) => {
492+
const extractor = this.getFileExtractor(`.${extension}`, extractors);
493+
const extractedSelectors = await extractSelectors(raw, extractor);
494+
selectors.merge(extractedSelectors);
495+
}),
496+
);
490497
return selectors;
491498
}
492499

@@ -637,71 +644,75 @@ class PurgeCSS {
637644
cssOptions: Array<string | RawCSS>,
638645
selectors: ExtractorResultSets,
639646
): Promise<ResultPurge[]> {
640-
const sources = [];
647+
const sources: ResultPurge[] = [];
641648

642649
// resolve any globs
643650
const processedOptions: Array<string | RawCSS> = [];
644-
for (const option of cssOptions) {
645-
if (typeof option === "string") {
646-
processedOptions.push(
647-
...glob.sync(option, {
648-
onlyFiles: true,
649-
ignore: this.options.skippedContentGlobs,
650-
}),
651-
);
652-
} else {
653-
processedOptions.push(option);
654-
}
655-
}
656-
657-
for (const option of processedOptions) {
658-
const cssContent =
659-
typeof option === "string"
660-
? this.options.stdin
661-
? option
662-
: await asyncFs.readFile(option, "utf-8")
663-
: option.raw;
664-
const isFromFile = typeof option === "string" && !this.options.stdin;
665-
const root = postcss.parse(cssContent, {
666-
from: isFromFile ? option : undefined,
667-
});
668-
669-
// purge unused selectors
670-
this.walkThroughCSS(root, selectors);
671-
672-
if (this.options.fontFace) this.removeUnusedFontFaces();
673-
if (this.options.keyframes) this.removeUnusedKeyframes();
674-
if (this.options.variables) this.removeUnusedCSSVariables();
675-
676-
const postCSSResult = root.toResult({
677-
map: this.options.sourceMap,
678-
to:
679-
typeof this.options.sourceMap === "object"
680-
? this.options.sourceMap.to
681-
: undefined,
682-
});
683-
const result: ResultPurge = {
684-
css: postCSSResult.toString(),
685-
file: typeof option === "string" ? option : option.name,
686-
};
651+
await Promise.all(
652+
cssOptions.map(async (option) => {
653+
if (typeof option === "string") {
654+
processedOptions.push(
655+
...(await glob(option, {
656+
onlyFiles: true,
657+
ignore: this.options.skippedContentGlobs,
658+
})),
659+
);
660+
} else {
661+
processedOptions.push(option);
662+
}
663+
}),
664+
);
687665

688-
if (this.options.sourceMap) {
689-
result.sourceMap = postCSSResult.map?.toString();
690-
}
666+
await Promise.all(
667+
processedOptions.map(async (option) => {
668+
const cssContent =
669+
typeof option === "string"
670+
? this.options.stdin
671+
? option
672+
: await asyncFs.readFile(option, "utf-8")
673+
: option.raw;
674+
const isFromFile = typeof option === "string" && !this.options.stdin;
675+
const root = postcss.parse(cssContent, {
676+
from: isFromFile ? option : undefined,
677+
});
678+
679+
// purge unused selectors
680+
this.walkThroughCSS(root, selectors);
681+
682+
if (this.options.fontFace) this.removeUnusedFontFaces();
683+
if (this.options.keyframes) this.removeUnusedKeyframes();
684+
if (this.options.variables) this.removeUnusedCSSVariables();
685+
686+
const postCSSResult = root.toResult({
687+
map: this.options.sourceMap,
688+
to:
689+
typeof this.options.sourceMap === "object"
690+
? this.options.sourceMap.to
691+
: undefined,
692+
});
693+
const result: ResultPurge = {
694+
css: postCSSResult.toString(),
695+
file: typeof option === "string" ? option : option.name,
696+
};
697+
698+
if (this.options.sourceMap) {
699+
result.sourceMap = postCSSResult.map?.toString();
700+
}
691701

692-
if (this.options.rejected) {
693-
result.rejected = Array.from(this.selectorsRemoved);
694-
this.selectorsRemoved.clear();
695-
}
702+
if (this.options.rejected) {
703+
result.rejected = Array.from(this.selectorsRemoved);
704+
this.selectorsRemoved.clear();
705+
}
696706

697-
if (this.options.rejectedCss) {
698-
result.rejectedCss = postcss
699-
.root({ nodes: this.removedNodes })
700-
.toString();
701-
}
707+
if (this.options.rejectedCss) {
708+
result.rejectedCss = postcss
709+
.root({ nodes: this.removedNodes })
710+
.toString();
711+
}
702712

703-
sources.push(result);
704-
}
713+
sources.push(result);
714+
}),
715+
);
705716
return sources;
706717
}
707718

0 commit comments

Comments
 (0)