Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to Ignore Files/Folders #638

Merged
merged 9 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/postcss-purgecss/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ blocklist: ['usedClass', /^nav-/]
```
Even if nav-links and usedClass are found by an extractor, they will be removed.

### `skippedContentGlobs`

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.)

```ts
skippedContentGlobs: ['node_modules/**', 'components/**']
```
Here, PurgeCSS will not scan anything in the "node_modules" and "components" folders.


### `rejected`
Type: `boolean`
Expand Down
1 change: 1 addition & 0 deletions packages/postcss-purgecss/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface UserDefinedOptions {
variables?: boolean;
safelist?: UserDefinedSafelist;
blocklist?: StringRegExpArray;
skippedContentGlobs?: Array<string>;
}
24 changes: 24 additions & 0 deletions packages/purgecss/__tests__/skipped-content.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import PurgeCSS from "./../src/index";

import { ROOT_TEST_EXAMPLES } from "./utils";

describe("skipped-content", () => {
let purgedCSS: string;

beforeAll(async () => {
const resultsPurge = await new PurgeCSS().purge({
content: [`${ROOT_TEST_EXAMPLES}skipped-content/**/*.html`],
css: [`${ROOT_TEST_EXAMPLES}skipped-content/simple.css`],
skippedContentGlobs: [
`${ROOT_TEST_EXAMPLES}skipped-content/skippedFolder/**`,
],
});
purgedCSS = resultsPurge[0].css;
});

it("purges appropriate CSS rules when skippedContentGlobs is set", () => {
expect(purgedCSS.includes(".red")).toBe(true);
expect(purgedCSS.includes(".black")).toBe(true);
expect(purgedCSS.includes(".green")).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.black {
color: black;
}

.red {
color: red;
}

.green {
color: green;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="green">anything</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p class="black">anything</p>
<p class="red">anything</p>
8 changes: 6 additions & 2 deletions packages/purgecss/bin/purgecss.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ program
.option(
"-b, --blocklist <list...>",
"list of selectors that should be removed"
)
.option(
"-k, --skippedContentGlobs <list...>",
"list of glob patterns for folders/files that should not be scanned"
);

program.parse(process.argv);
Expand All @@ -58,9 +62,9 @@ const run = async () => {
if (program.keyframes) options.keyframes = program.keyframes;
if (program.rejected) options.rejected = program.rejected;
if (program.variables) options.variables = program.variables;
if (program.safelist)
options.safelist = standardizeSafelist(program.safelist);
if (program.safelist) options.safelist = standardizeSafelist(program.safelist);
if (program.blocklist) options.blocklist = program.blocklist;
if (program.skippedContentGlobs) options.skippedContentGlobs = program.skippedContentGlobs;

const purged = await new PurgeCSS().purge(options);
const output = options.output || program.output;
Expand Down
12 changes: 10 additions & 2 deletions packages/purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ class PurgeCSS {
await asyncFs.access(globfile, fs.constants.F_OK);
filesNames.push(globfile);
} catch (err) {
filesNames = glob.sync(globfile, { nodir: true });
filesNames = glob.sync(globfile, {
nodir: true,
ignore: this.options.skippedContentGlobs,
});
}
for (const file of filesNames) {
const content = await asyncFs.readFile(file, "utf-8");
Expand Down Expand Up @@ -512,7 +515,12 @@ class PurgeCSS {
const processedOptions: Array<string | RawCSS> = [];
for (const option of cssOptions) {
if (typeof option === "string") {
processedOptions.push(...glob.sync(option, { nodir: true }));
processedOptions.push(
...glob.sync(option, {
nodir: true,
ignore: this.options.skippedContentGlobs,
})
);
} else {
processedOptions.push(option);
}
Expand Down
1 change: 1 addition & 0 deletions packages/purgecss/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export const defaultOptions: Options = {
keyframes: [],
},
blocklist: [],
skippedContentGlobs: [],
};
2 changes: 2 additions & 0 deletions packages/purgecss/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface UserDefinedOptions {
variables?: boolean;
safelist?: UserDefinedSafelist;
blocklist?: StringRegExpArray;
skippedContentGlobs?: Array<string>;
}

export interface Options {
Expand All @@ -82,6 +83,7 @@ export interface Options {
variables: boolean;
safelist: Required<ComplexSafelist>;
blocklist: StringRegExpArray;
skippedContentGlobs: Array<string>;
}

export interface ResultPurge {
Expand Down