Skip to content

Commit 6188505

Browse files
committed
feat: warn if no files are found #642
1 parent 7471c56 commit 6188505

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

packages/purgecss/__tests__/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,30 @@ describe("PurgeCSS with detailed extractor for html", () => {
9191
notFindInCSS(expect, ["parent3", "d33ef1", "parent2"], purgedCSS);
9292
});
9393
});
94+
95+
describe("Warn if no files are found from the option 'content'", () => {
96+
it("warns if no files are found", async () => {
97+
const originalConsoleWarn = console.warn;
98+
console.warn = jest.fn();
99+
await new PurgeCSS().purge({
100+
content: ["./__tests__/not-found.js", "./__tests__/not-found.html"],
101+
css: [`${ROOT_TEST_EXAMPLES}others/remove_unused.css`]
102+
});
103+
expect(console.warn).toHaveBeenCalledTimes(1);
104+
expect(console.warn).toHaveBeenCalledWith(
105+
"No files found from the passed PurgeCSS option 'content'.");
106+
console.warn = originalConsoleWarn;
107+
})
108+
109+
it("does not warn if files are found", async () => {
110+
const originalConsoleWarn = console.warn;
111+
console.warn = jest.fn();
112+
await new PurgeCSS().purge({
113+
content: [`${ROOT_TEST_EXAMPLES}others/remove_unused.js`],
114+
css: [`${ROOT_TEST_EXAMPLES}others/remove_unused.css`]
115+
});
116+
expect(console.warn).not.toHaveBeenCalled();
117+
console.warn = originalConsoleWarn;
118+
});
119+
120+
})

packages/purgecss/src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,25 +415,27 @@ class PurgeCSS {
415415
extractors: Extractors[]
416416
): Promise<ExtractorResultSets> {
417417
const selectors = new ExtractorResultSets([]);
418+
const filesNames: string[] = [];
418419
for (const globFile of files) {
419-
let filesNames: string[] = [];
420-
421420
try {
422421
await asyncFs.access(globFile, fs.constants.F_OK);
423422
filesNames.push(globFile);
424423
} catch (err) {
425-
filesNames = glob.sync(globFile, {
424+
filesNames.push(...glob.sync(globFile, {
426425
nodir: true,
427426
ignore: this.options.skippedContentGlobs,
428-
});
429-
}
430-
for (const file of filesNames) {
431-
const content = await asyncFs.readFile(file, "utf-8");
432-
const extractor = this.getFileExtractor(file, extractors);
433-
const extractedSelectors = await extractSelectors(content, extractor);
434-
selectors.merge(extractedSelectors);
427+
}));
435428
}
436429
}
430+
if (filesNames.length === 0) {
431+
console.warn("No files found from the passed PurgeCSS option 'content'.")
432+
}
433+
for (const file of filesNames) {
434+
const content = await asyncFs.readFile(file, "utf-8");
435+
const extractor = this.getFileExtractor(file, extractors);
436+
const extractedSelectors = await extractSelectors(content, extractor);
437+
selectors.merge(extractedSelectors);
438+
}
437439
return selectors;
438440
}
439441

0 commit comments

Comments
 (0)