Skip to content

Commit

Permalink
feat: warn if no files are found #642
Browse files Browse the repository at this point in the history
  • Loading branch information
Ffloriel committed Mar 26, 2023
1 parent 7471c56 commit 6188505
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
27 changes: 27 additions & 0 deletions packages/purgecss/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,30 @@ describe("PurgeCSS with detailed extractor for html", () => {
notFindInCSS(expect, ["parent3", "d33ef1", "parent2"], purgedCSS);
});
});

describe("Warn if no files are found from the option 'content'", () => {
it("warns if no files are found", async () => {
const originalConsoleWarn = console.warn;
console.warn = jest.fn();
await new PurgeCSS().purge({
content: ["./__tests__/not-found.js", "./__tests__/not-found.html"],
css: [`${ROOT_TEST_EXAMPLES}others/remove_unused.css`]
});
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.warn).toHaveBeenCalledWith(
"No files found from the passed PurgeCSS option 'content'.");
console.warn = originalConsoleWarn;
})

it("does not warn if files are found", async () => {
const originalConsoleWarn = console.warn;
console.warn = jest.fn();
await new PurgeCSS().purge({
content: [`${ROOT_TEST_EXAMPLES}others/remove_unused.js`],
css: [`${ROOT_TEST_EXAMPLES}others/remove_unused.css`]
});
expect(console.warn).not.toHaveBeenCalled();
console.warn = originalConsoleWarn;
});

})
22 changes: 12 additions & 10 deletions packages/purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,27 @@ class PurgeCSS {
extractors: Extractors[]
): Promise<ExtractorResultSets> {
const selectors = new ExtractorResultSets([]);
const filesNames: string[] = [];
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, {
filesNames.push(...glob.sync(globFile, {
nodir: true,
ignore: this.options.skippedContentGlobs,
});
}
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);
}));
}
}
if (filesNames.length === 0) {
console.warn("No files found from the passed PurgeCSS option 'content'.")
}
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;
}

Expand Down

0 comments on commit 6188505

Please sign in to comment.