Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support wildcards pattern with common suffix in imports/exports field #353

Merged
merged 13 commits into from Apr 19, 2023
24 changes: 21 additions & 3 deletions lib/util/entrypoints.js
Expand Up @@ -430,7 +430,24 @@
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"),
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
remainingRequest.replace(/\$/g, "$$")
);
} else {
return mappingTarget.replace(
/\*/g,
remainingRequest.replace(/\$/g, "$$")
);
}
} else {
return mappingTarget;

Check warning on line 449 in lib/util/entrypoints.js

View check run for this annotation

Codecov / codecov/patch

lib/util/entrypoints.js#L449

Added line #L449 was not covered by tests
}
}

/**
Expand Down Expand Up @@ -549,9 +566,10 @@
node.folder = target;
} else {
const file = lastNonSlashIndex > 0 ? path.slice(lastNonSlashIndex) : path;
if (file.endsWith("*")) {
const wildcardsIndex = file.indexOf("*");
alexander-akait marked this conversation as resolved.
Show resolved Hide resolved
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.
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();
});
});
});