Skip to content

Commit b27293f

Browse files
authored
fix: fix glob expression illegal operations on directories (#1308)
* fix: fix glob expression illegal operations on directories This fixes the glob regression issues from v5 + parallelizes the async IO operations for faster purge. ## Proposed changes Fixes an issue that the previously working glob expressions no longer work in v6 and v7. This changes the glob dependency to fast-glob to fix the issue. I also parallelized the async-io operations for faster purge. Fixes #1266 ## Types of changes What types of changes does your code introduce? _Put an `x` in the boxes that apply_ - [x] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) ## Checklist _Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code._ - [x] Lint and unit tests pass locally with my changes - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I have added necessary documentation (if appropriate) ## Further comments * test: add tests for glob
1 parent 8eb027d commit b27293f

File tree

9 files changed

+125
-211
lines changed

9 files changed

+125
-211
lines changed

package-lock.json

+91-201
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/gulp-purgecss/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"test": "jest"
3737
},
3838
"dependencies": {
39-
"glob": "^11.0.0",
39+
"fast-glob": "^3.3.2",
4040
"plugin-error": "^2.0.0",
4141
"purgecss": "^7.0.2",
4242
"through2": "^4.0.1",

packages/gulp-purgecss/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as glob from "glob";
1+
import glob from "fast-glob";
22
import PluginError from "plugin-error";
33
import { PurgeCSS } from "purgecss";
44
import * as internal from "stream";

packages/purgecss-webpack-plugin/__tests__/cases/path-and-safelist-functions/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require("path");
2-
const glob = require("glob");
2+
const glob = require("fast-glob");
33
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
44
const {PurgeCSSPlugin} = require("../../../src/");
55

packages/purgecss-webpack-plugin/__tests__/cases/simple-with-exclusion/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require("path");
2-
const glob = require("glob");
2+
const glob = require("fast-glob");
33
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
44
const { PurgeCSSPlugin } = require("../../../src/");
55

packages/purgecss-webpack-plugin/__tests__/cases/simple/purgecss.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require("path");
2-
const glob = require("glob");
2+
const glob = require("fast-glob");
33

44
const customExtractor = (content) => {
55
const res = content.match(/[A-z0-9-:/]+/g) || [];
@@ -19,4 +19,4 @@ module.exports = {
1919
extensions: ["html", "js"],
2020
},
2121
],
22-
}
22+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { PurgeCSS } from "../src/";
2+
import { ROOT_TEST_EXAMPLES } from "./utils";
3+
4+
describe("Glob", () => {
5+
it("glob expressions in content/css work", async () => {
6+
const resultsPurge = await new PurgeCSS().purge({
7+
content: [`${ROOT_TEST_EXAMPLES}comments/**/*.{js,html,json,svg}`],
8+
css: [`${ROOT_TEST_EXAMPLES}comments/**/*.css`],
9+
});
10+
11+
expect(resultsPurge[0].file).toBe(
12+
`${ROOT_TEST_EXAMPLES}comments/ignore_comment.css`,
13+
);
14+
expect(resultsPurge[0].css.includes("/* purgecss ignore */")).toBe(false);
15+
expect(resultsPurge[0].css.includes("/* purgecss ignore current */")).toBe(
16+
false,
17+
);
18+
19+
expect(resultsPurge[1].file).toBe(
20+
`${ROOT_TEST_EXAMPLES}comments/ignore_comment_range.css`,
21+
);
22+
expect(resultsPurge[1].css.includes("h4")).toBe(false);
23+
});
24+
});

packages/purgecss/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"dependencies": {
4343
"commander": "^12.1.0",
44-
"glob": "^11.0.0",
44+
"fast-glob": "^3.3.2",
4545
"postcss": "^8.4.47",
4646
"postcss-selector-parser": "^7.0.0"
4747
},

packages/purgecss/src/index.ts

+3-3
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 "glob";
10+
import * as glob from "fast-glob";
1111
import * as path from "path";
1212
import * as postcss from "postcss";
1313
import selectorParser from "postcss-selector-parser";
@@ -451,7 +451,7 @@ class PurgeCSS {
451451
} catch (err) {
452452
filesNames.push(
453453
...glob.sync(globFile, {
454-
nodir: true,
454+
onlyFiles: true,
455455
ignore: this.options.skippedContentGlobs.map((glob) =>
456456
glob.replace(/^\.\//, ""),
457457
),
@@ -645,7 +645,7 @@ class PurgeCSS {
645645
if (typeof option === "string") {
646646
processedOptions.push(
647647
...glob.sync(option, {
648-
nodir: true,
648+
onlyFiles: true,
649649
ignore: this.options.skippedContentGlobs,
650650
}),
651651
);

0 commit comments

Comments
 (0)