Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e99db4d

Browse files
cometkimmrbbot
andauthored
Change glob matcher to picomatch (#316)
* Change glob matcher to picomatch Fixes #244 * Add more `globsToMatcher` test cases and enable `contains` option Co-authored-by: MrBBot <[email protected]>
1 parent 3e459b7 commit e99db4d

File tree

5 files changed

+77
-43
lines changed

5 files changed

+77
-43
lines changed

package-lock.json

Lines changed: 38 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runner-vm/src/linker.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ export class ModuleLinker {
8989
return module;
9090
}
9191

92-
// Find first matching module rule ("ignore" requires relative paths)
93-
const relativeIdentifier = path.relative("", identifier);
94-
const rule = this.moduleRules.find((rule) =>
95-
rule.include.test(relativeIdentifier)
96-
);
92+
const rule = this.moduleRules.find((rule) => rule.include.test(identifier));
9793
if (rule === undefined) {
9894
const isBuiltin = builtinModules.includes(spec);
9995
const suggestion = isBuiltin ? SUGGEST_NODE : SUGGEST_BUNDLE;
@@ -183,11 +179,7 @@ export class ModuleLinker {
183179
return module.exports;
184180
}
185181

186-
// Find first matching module rule ("ignore" requires relative paths)
187-
const relativeIdentifier = path.relative("", identifier);
188-
const rule = this.moduleRules.find((rule) =>
189-
rule.include.test(relativeIdentifier)
190-
);
182+
const rule = this.moduleRules.find((rule) => rule.include.test(identifier));
191183
if (rule === undefined) {
192184
const isBuiltin = builtinModules.includes(spec);
193185
const suggestion = isBuiltin ? SUGGEST_NODE : SUGGEST_BUNDLE;

packages/shared/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
"extends": "../../package.json"
3636
},
3737
"dependencies": {
38-
"ignore": "^5.1.8",
39-
"kleur": "^4.1.4"
38+
"kleur": "^4.1.4",
39+
"picomatch": "^2.3.1"
4040
},
4141
"devDependencies": {
42-
"@miniflare/shared-test": "2.6.0"
42+
"@miniflare/shared-test": "2.6.0",
43+
"@types/picomatch": "^2.3.0"
4344
}
4445
}

packages/shared/src/data.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import ignore from "ignore";
2+
import picomatch from "picomatch";
33

44
export const numericCompare = new Intl.Collator(undefined, { numeric: true })
55
.compare;
@@ -44,12 +44,25 @@ export interface Matcher {
4444
toString(): string;
4545
}
4646

47-
export function globsToMatcher(globs?: string[]): Matcher {
48-
const ign = ignore();
49-
if (globs) ign.add(globs);
47+
export function globsToMatcher(globs: string[] = []): Matcher {
48+
const matchGlobs: string[] = [];
49+
const ignoreGlobs: string[] = [];
50+
for (const glob of globs) {
51+
if (glob.startsWith("!")) {
52+
ignoreGlobs.push(glob.slice(1));
53+
} else {
54+
matchGlobs.push(glob);
55+
}
56+
}
57+
const isMatch = picomatch(matchGlobs, {
58+
dot: true,
59+
bash: true,
60+
contains: true,
61+
ignore: ignoreGlobs,
62+
});
5063
return {
51-
test: (string) => ign.ignores(string),
52-
toString: () => globs?.join(", ") ?? "",
64+
test: (string) => isMatch(string),
65+
toString: () => globs.join(", "),
5366
};
5467
}
5568

packages/shared/test/data.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ test("base64Decode: decodes base64 string", (t) => {
6060
test("globsToMatcher: converts globs to string matcher", (t) => {
6161
const globs = ["*.txt", "src/**/*.js", "!src/bad.js"];
6262
const matcher = globsToMatcher(globs);
63+
64+
// Check `*.txt`
6365
t.true(matcher.test("test.txt"));
6466
t.true(matcher.test("dist/test.txt"));
67+
68+
// Check `src/**/*.js`
6569
t.true(matcher.test("src/index.js"));
6670
t.true(matcher.test("src/lib/add.js"));
71+
t.false(matcher.test("src/image.jpg"));
72+
73+
// Check `!src/bad.js`
6774
t.false(matcher.test("src/bad.js"));
75+
76+
// Check absolute paths (`ModuleLinker` will `path.resolve` to absolute paths)
77+
// (see https://github.com/cloudflare/miniflare/issues/244)
78+
t.true(matcher.test("/one/two/three.txt"));
79+
t.true(matcher.test(path.join(process.cwd(), "src/index.js")));
80+
81+
// Check debug output
6882
t.is(matcher.toString(), globs.join(", "));
6983
});
7084
test("globsToMatcher: returns matcher that matches nothing on undefined globs", (t) => {

0 commit comments

Comments
 (0)