Skip to content

Commit 2a27663

Browse files
bdkjonesBryan JonesFfloriel
authored
Add Ability to Ignore Files/Folders (#638)
* Add Skiplist Option Adds support for the "ignore" option of the glob dependency to prevent PurgeCSS from scanning specific folders (such as node_modules, etc.) * Update Readme Add description for new "skippedlist" option * Rename Option, Fix Type Replaced "skiplist" with "skippedContentGlobs" and changed the type to Array<String> * Rename Option, Fix Type Replaced "skiplist" with "skippedContentGlobs" and changed the type to Array<String> * Add Skipped-Content Test test for "skippedContentGlobs" added, casing on 'string' type fixed. Test passes, but only with full path to file to skip right now. * Fix test Use the correct path for the test environment * Update Docs Minor documentation tweaks Co-authored-by: Bryan Jones <bryan@codekitapp.com> Co-authored-by: Floriel <Ffloriel@users.noreply.github.com>
1 parent 9b0fdc3 commit 2a27663

File tree

10 files changed

+67
-4
lines changed

10 files changed

+67
-4
lines changed

packages/postcss-purgecss/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ blocklist: ['usedClass', /^nav-/]
9494
```
9595
Even if nav-links and usedClass are found by an extractor, they will be removed.
9696

97+
### `skippedContentGlobs`
98+
99+
If you provide globs for the `content` parameter, you can use this option to exclude certain files or folders that would otherwise be scanned. Pass an array of globs matching items that should be excluded. (Note: this option has no effect if `content` is not globs.)
100+
101+
```ts
102+
skippedContentGlobs: ['node_modules/**', 'components/**']
103+
```
104+
Here, PurgeCSS will not scan anything in the "node_modules" and "components" folders.
105+
97106

98107
### `rejected`
99108
Type: `boolean`

packages/postcss-purgecss/src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export interface UserDefinedOptions {
3030
variables?: boolean;
3131
safelist?: UserDefinedSafelist;
3232
blocklist?: StringRegExpArray;
33+
skippedContentGlobs?: Array<string>;
3334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import PurgeCSS from "./../src/index";
2+
3+
import { ROOT_TEST_EXAMPLES } from "./utils";
4+
5+
describe("skipped-content", () => {
6+
let purgedCSS: string;
7+
8+
beforeAll(async () => {
9+
const resultsPurge = await new PurgeCSS().purge({
10+
content: [`${ROOT_TEST_EXAMPLES}skipped-content/**/*.html`],
11+
css: [`${ROOT_TEST_EXAMPLES}skipped-content/simple.css`],
12+
skippedContentGlobs: [
13+
`${ROOT_TEST_EXAMPLES}skipped-content/skippedFolder/**`,
14+
],
15+
});
16+
purgedCSS = resultsPurge[0].css;
17+
});
18+
19+
it("purges appropriate CSS rules when skippedContentGlobs is set", () => {
20+
expect(purgedCSS.includes(".red")).toBe(true);
21+
expect(purgedCSS.includes(".black")).toBe(true);
22+
expect(purgedCSS.includes(".green")).toBe(false);
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.black {
2+
color: black;
3+
}
4+
5+
.red {
6+
color: red;
7+
}
8+
9+
.green {
10+
color: green;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p class="green">anything</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p class="black">anything</p>
2+
<p class="red">anything</p>

packages/purgecss/bin/purgecss.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ program
3535
.option(
3636
"-b, --blocklist <list...>",
3737
"list of selectors that should be removed"
38+
)
39+
.option(
40+
"-k, --skippedContentGlobs <list...>",
41+
"list of glob patterns for folders/files that should not be scanned"
3842
);
3943

4044
program.parse(process.argv);
@@ -58,9 +62,9 @@ const run = async () => {
5862
if (program.keyframes) options.keyframes = program.keyframes;
5963
if (program.rejected) options.rejected = program.rejected;
6064
if (program.variables) options.variables = program.variables;
61-
if (program.safelist)
62-
options.safelist = standardizeSafelist(program.safelist);
65+
if (program.safelist) options.safelist = standardizeSafelist(program.safelist);
6366
if (program.blocklist) options.blocklist = program.blocklist;
67+
if (program.skippedContentGlobs) options.skippedContentGlobs = program.skippedContentGlobs;
6468

6569
const purged = await new PurgeCSS().purge(options);
6670
const output = options.output || program.output;

packages/purgecss/src/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ class PurgeCSS {
368368
await asyncFs.access(globfile, fs.constants.F_OK);
369369
filesNames.push(globfile);
370370
} catch (err) {
371-
filesNames = glob.sync(globfile, { nodir: true });
371+
filesNames = glob.sync(globfile, {
372+
nodir: true,
373+
ignore: this.options.skippedContentGlobs,
374+
});
372375
}
373376
for (const file of filesNames) {
374377
const content = await asyncFs.readFile(file, "utf-8");
@@ -510,7 +513,12 @@ class PurgeCSS {
510513
const processedOptions: Array<string | RawCSS> = [];
511514
for (const option of cssOptions) {
512515
if (typeof option === "string") {
513-
processedOptions.push(...glob.sync(option, { nodir: true }));
516+
processedOptions.push(
517+
...glob.sync(option, {
518+
nodir: true,
519+
ignore: this.options.skippedContentGlobs,
520+
})
521+
);
514522
} else {
515523
processedOptions.push(option);
516524
}

packages/purgecss/src/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ export const defaultOptions: Options = {
2020
keyframes: [],
2121
},
2222
blocklist: [],
23+
skippedContentGlobs: [],
2324
dynamicAttributes: [],
2425
};

packages/purgecss/src/types/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface UserDefinedOptions {
6666
variables?: boolean;
6767
safelist?: UserDefinedSafelist;
6868
blocklist?: StringRegExpArray;
69+
skippedContentGlobs?: Array<string>;
6970
dynamicAttributes?: string[];
7071
}
7172

@@ -83,6 +84,7 @@ export interface Options {
8384
variables: boolean;
8485
safelist: Required<ComplexSafelist>;
8586
blocklist: StringRegExpArray;
87+
skippedContentGlobs: Array<string>;
8688
dynamicAttributes: string[];
8789
}
8890

0 commit comments

Comments
 (0)