Skip to content

Commit

Permalink
support wildcards pattern with common suffix in imports/exports field
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Jul 26, 2022
1 parent 651e578 commit ae04929
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/util/entrypoints.js
Expand Up @@ -430,7 +430,24 @@ function targetMapping(
return mappingTarget + remainingRequest;
}
assert(mappingTarget, false);
return mappingTarget.replace(/\*/g, remainingRequest.replace(/\$/g, "$$"));
const wildcardIndex = mappingTarget.indexOf("*");

if (wildcardIndex !== -1) {
const maybeCommonSuffix = mappingTarget.slice(wildcardIndex + 1);
if (remainingRequest.endsWith(maybeCommonSuffix)) {
return mappingTarget.replace(
new RegExp(`\\*${maybeCommonSuffix}`, "g"),
remainingRequest.replace(/\$/g, "$$")
);
} else {
return mappingTarget.replace(
/\*/g,
remainingRequest.replace(/\$/g, "$$")
);
}
} else {
return mappingTarget;
}
}

/**
Expand Down Expand Up @@ -549,9 +566,10 @@ function walkPath(root, path, target) {
node.folder = target;
} else {
const file = lastNonSlashIndex > 0 ? path.slice(lastNonSlashIndex) : path;
if (file.endsWith("*")) {
const wildcardsIndex = file.indexOf("*");
if (wildcardsIndex !== -1) {
if (node.wildcards === null) node.wildcards = new Map();
node.wildcards.set(file.slice(0, -1), target);
node.wildcards.set(file.slice(0, wildcardsIndex), target);
} else {
node.files.set(file, target);
}
Expand Down
54 changes: 54 additions & 0 deletions test/exportsField.js
Expand Up @@ -1953,6 +1953,28 @@ describe("Process exports field", function exportsField() {
"./a/b/d/c.js",
[]
]
},
{
name: "wildcard pattern with suffix #1",
expect: ["./A/b.js"],
suite: [
{
"./a/*.js": "./A/*.js"
},
"./a/b.js",
[]
]
},
{
name: "wildcard pattern with suffix #2",
expect: ["./A/b/c.js"],
suite: [
{
"./a/*.js": "./A/*.js"
},
"./a/b/c.js",
[]
]
}
];

Expand Down Expand Up @@ -2452,4 +2474,36 @@ describe("ExportsFieldPlugin", () => {
}
);
});

it("should resolve with wildcard pattern #1", done => {
const fixture = path.resolve(
__dirname,
"./fixtures/imports-exports-wildcard/"
);

resolver.resolve({}, fixture, "m/features/f.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(
path.resolve(fixture, "./node_modules/m/src/features/f.js")
);
done();
});
});

it("should resolve with wildcard pattern #2", done => {
const fixture = path.resolve(
__dirname,
"./fixtures/imports-exports-wildcard/"
);

resolver.resolve({}, fixture, "m/features/y/y.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(
path.resolve(fixture, "./node_modules/m/src/features/y/y.js")
);
done();
});
});
});

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

Empty file.
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions test/importsField.js
Expand Up @@ -1357,4 +1357,17 @@ describe("ImportsFieldPlugin", () => {
}
);
});

it("should resolve with wildcard pattern", done => {
const fixture = path.resolve(
__dirname,
"./fixtures/imports-exports-wildcard/node_modules/m/"
);
resolver.resolve({}, fixture, "#internal/i.js", {}, (err, result) => {
if (err) return done(err);
if (!result) throw new Error("No result");
result.should.equal(path.resolve(fixture, "./src/internal/i.js"));
done();
});
});
});

0 comments on commit ae04929

Please sign in to comment.