Skip to content

Commit

Permalink
perf: swtich to zeptomatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 3, 2025
1 parent f1262ed commit 13114c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
26 changes: 11 additions & 15 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,38 @@ import { build, type BuildOptions, transform } from "esbuild";
export default defineBuildConfig({
hooks: {
async "build:before"(ctx) {
ctx.options.alias["minimatch"] = await buildMinimatch();
ctx.options.alias["zeptomatch"] = await buildZeptomatch();
},
},
});

async function buildMinimatch() {
async function buildZeptomatch() {
let bundle = await build(<BuildOptions>{
format: "iife",
globalName: "__lib__",
bundle: true,
write: false,
stdin: {
resolveDir: process.cwd(),
contents: /* js */ `export { minimatch } from "minimatch";`,
contents: /* js */ `export { default as zeptomatch } from "zeptomatch";`,
},
}).then((r) => r.outputFiles![0].text);

bundle = bundle
.replace("options.debug", "false")
.replace(/ this\.debug\(/gm, " // this.debug(");

bundle = (await transform(bundle, { minify: true })).code!;

bundle = /* js */ `
let _lazyMinimatch = () => { ${bundle}; return __lib__; };
let _minimatch;
export const minimatch = (path, pattern, opts) => {
if (!_minimatch) {
_minimatch = _lazyMinimatch();
_lazyMinimatch = null;
let _lazyMatch = () => { ${bundle}; return __lib__.default || __lib__; };
let _match;
export default (path, pattern) => {
if (!_match) {
_match = _lazyMatch();
_lazyMatch = null;
}
return _minimatch.minimatch(path, pattern, opts);
return _match(path, pattern);
};
`;

const outFile = resolve("tmp/node_modules/minimatch/minimatch.min.mjs");
const outFile = resolve("tmp/node_modules/zeptomatch/zeptomatch.min.mjs");
await mkdir(dirname(outFile), { recursive: true });
await writeFile(outFile, bundle);
return outFile;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"eslint": "^9.17.0",
"eslint-config-unjs": "^0.4.2",
"jiti": "^2.4.2",
"minimatch": "^10.0.1",
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"unbuild": "^3.2.0",
"vitest": "^2.1.8"
"vitest": "^2.1.8",
"zeptomatch": "^2.0.0"
},
"packageManager": "[email protected]"
}
26 changes: 15 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Check LICENSE file

import type path from "node:path";

import { minimatch } from "minimatch";
import zeptomatch from "zeptomatch";

import { normalizeWindowsPath } from "./_internal";

Expand Down Expand Up @@ -293,14 +293,14 @@ export const parse: typeof path.parse = function (p) {
};
};

export const matchesGlob: typeof path.matchesGlob = (path, pattern) => {
// https://github.com/nodejs/node/blob/main/lib/internal/fs/glob.js#L660
// https://github.com/isaacs/minimatch#windows
return minimatch(normalize(path), pattern, {
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
optimizationLevel: 2,
nocaseMagicOnly: true,
});
/**
* The `path.matchesGlob()` method determines if `path` matches the `pattern`.
* @param path The path to glob-match against.
* @param pattern The glob to check the path against.
*/
export const matchesGlob = (
path: string,
pattern: string | string[],
): boolean => {
return zeptomatch(pattern, normalize(path));
};
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ describe("constants", () => {
runTest("matchesGlob", matchesGlob, [
["/foo/bar", "/foo/**", true],
[String.raw`\foo\bar`, "/foo/**", true],
["/foo/bar", "/{bar,foo}/**", true],
]);

function _s(item) {
Expand Down

0 comments on commit 13114c6

Please sign in to comment.