Skip to content

Commit

Permalink
Swap from multimatch to micromatch
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 12, 2024
1 parent 6db046f commit 9698667
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@
"micromatch": "^4.0.7",
"minimist": "^1.2.8",
"moo": "^0.5.2",
"multimatch": "^7.0.0",
"node-retrieve-globals": "^6.0.0",
"normalize-path": "^3.0.0",
"nunjucks": "^3.2.4",
Expand Down
8 changes: 7 additions & 1 deletion src/EleventyServe.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import assert from "node:assert";

import debugUtil from "debug";
import { Merge, DeepCopy, TemplatePath } from "@11ty/eleventy-utils";
import EleventyDevServer from "@11ty/eleventy-dev-server";
import debugUtil from "debug";

import EleventyBaseError from "./Errors/EleventyBaseError.js";
import ConsoleLogger from "./Util/ConsoleLogger.js";
import PathPrefixer from "./Util/PathPrefixer.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
import { getModulePackageJson } from "./Util/ImportJsonSync.js";
import { EleventyImport } from "./Util/Require.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";

const debug = debugUtil("Eleventy:EleventyServe");

Expand Down Expand Up @@ -271,6 +273,10 @@ class EleventyServe {
}
}

isEmulatedPassthroughCopyMatch(filepath) {
return isGlobMatch(filepath, this._watchedFiles);
}

hasOptionsChanged() {
try {
assert.deepStrictEqual(this.config.serverOptions, this._savedConfigOptions);
Expand Down
9 changes: 4 additions & 5 deletions src/FileSystemSearch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fastglob from "fast-glob";
import micromatch from "micromatch";
import { TemplatePath } from "@11ty/eleventy-utils";
import debugUtil from "debug";

import { isGlobMatch } from "./Util/GlobMatcher.js";

const debug = debugUtil("Eleventy:FastGlobManager");

class FileSystemSearch {
Expand Down Expand Up @@ -80,11 +81,9 @@ class FileSystemSearch {
for (let key in this.inputs) {
let { input, options } = this.inputs[key];
if (
micromatch([path], input, {
dot: true,
nocase: true, // insensitive
isGlobMatch(path, input, {
ignore: options.ignore,
}).length > 0
})
) {
this.outputs[key][setOperation](normalized);
}
Expand Down
10 changes: 3 additions & 7 deletions src/TemplateCollection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import multimatch from "multimatch";
import { TemplatePath } from "@11ty/eleventy-utils";

import Sortable from "./Util/Objects/Sortable.js";
import TemplateData from "./Data/TemplateData.js";
import Sortable from "./Util/Objects/Sortable.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";

class TemplateCollection extends Sortable {
constructor() {
Expand Down Expand Up @@ -48,11 +48,7 @@ class TemplateCollection extends Sortable {
}

let filtered = this.getAllSorted().filter((item) => {
if (multimatch([item.inputPath], globs).length) {
return true;
}

return false;
return isGlobMatch(item.inputPath, globs);
});
this._dirty = false;
this._filteredByGlobsCache.set(key, [...filtered]);
Expand Down
8 changes: 2 additions & 6 deletions src/TemplatePassthroughManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import multimatch from "multimatch";
import isGlob from "is-glob";
import { TemplatePath } from "@11ty/eleventy-utils";
import debugUtil from "debug";
Expand All @@ -7,6 +6,7 @@ import EleventyExtensionMap from "./EleventyExtensionMap.js";
import EleventyBaseError from "./Errors/EleventyBaseError.js";
import TemplatePassthrough from "./TemplatePassthrough.js";
import checkPassthroughCopyBehavior from "./Util/PassthroughCopyBehaviorCheck.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";

const debug = debugUtil("Eleventy:TemplatePassthroughManager");
const debugDev = debugUtil("Dev:Eleventy:TemplatePassthroughManager");
Expand Down Expand Up @@ -213,11 +213,7 @@ class TemplatePassthroughManager {
if (TemplatePath.startsWithSubPath(changedFile, path.inputPath)) {
return path;
}
if (
changedFile &&
isGlob(path.inputPath) &&
multimatch([changedFile], [path.inputPath]).length
) {
if (changedFile && isGlob(path.inputPath) && isGlobMatch(changedFile, [path.inputPath])) {
return path;
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/Util/GlobMatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import micromatch from "micromatch";
import { TemplatePath } from "@11ty/eleventy-utils";

function isGlobMatch(filepath, globs = [], options = undefined) {
if (!filepath || !Array.isArray(globs) || globs.length === 0) {
return false;
}

let inputPath = TemplatePath.stripLeadingDotSlash(filepath);
let opts = Object.assign(
{
dot: true,
nocase: true, // insensitive
},
options,
);

return micromatch.isMatch(inputPath, globs, opts);
}

export { isGlobMatch };
29 changes: 15 additions & 14 deletions test/TemplateCollectionTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from "ava";
import multimatch from "multimatch";

import { isGlobMatch } from "../src/Util/GlobMatcher.js";
import Collection from "../src/TemplateCollection.js";
import Sortable from "../src/Util/Objects/Sortable.js";

Expand Down Expand Up @@ -257,26 +257,27 @@ test("partial match on tag string, issue 95", async (t) => {
t.is(posts.length, 1);
});

test("multimatch assumptions, issue #127", async (t) => {
t.deepEqual(
multimatch(["src/bookmarks/test.md"], "**/+(bookmarks|posts|screencasts)/**/!(index)*.md"),
["src/bookmarks/test.md"],
// Swapped to micromatch in 3.0.0-alpha.17
test("micromatch assumptions, issue #127", async (t) => {
function isMatch(filepath, globs) {
return isGlobMatch(filepath, globs);
}
t.true(
isMatch("src/bookmarks/test.md", ["**/+(bookmarks|posts|screencasts)/**/!(index)*.md"]),
);
t.deepEqual(
multimatch(["./src/bookmarks/test.md"], "./**/+(bookmarks|posts|screencasts)/**/!(index)*.md"),
["./src/bookmarks/test.md"],

t.true(
isMatch("./src/bookmarks/test.md", ["./**/+(bookmarks|posts|screencasts)/**/!(index)*.md"]),
);

let c = new Collection();
let globs = c.getGlobs("**/+(bookmarks|posts|screencasts)/**/!(index)*.md");
t.deepEqual(globs, ["./**/+(bookmarks|posts|screencasts)/**/!(index)*.md"]);

t.deepEqual(multimatch(["./src/bookmarks/test.md"], globs), ["./src/bookmarks/test.md"]);
t.deepEqual(multimatch(["./src/bookmarks/index.md"], globs), []);
t.deepEqual(multimatch(["./src/bookmarks/index2.md"], globs), []);
t.deepEqual(multimatch(["./src/_content/bookmarks/2018-03-27-git-message.md"], globs), [
"./src/_content/bookmarks/2018-03-27-git-message.md",
]);
t.true(isMatch("./src/bookmarks/test.md", globs));
t.false(isMatch("./src/bookmarks/index.md", globs));
t.false(isMatch("./src/bookmarks/index2.md", globs));
t.true(isMatch("./src/_content/bookmarks/2018-03-27-git-message.md", globs));
});

test("Sort in place (issue #352)", async (t) => {
Expand Down

0 comments on commit 9698667

Please sign in to comment.