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

feat: add wildcards #439

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions lib/AliasPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,24 @@ module.exports = class AliasPlugin {
.tapAsync("AliasPlugin", (request, resolveContext, callback) => {
const innerRequest = request.request || request.path;
if (!innerRequest) return callback();

forEachBail(
this.options,
(item, callback) => {
/** @type {boolean} */
let shouldStop = false;
if (

const matchRequest =
innerRequest === item.name ||
(!item.onlyModule &&
(request.request
? innerRequest.startsWith(`${item.name}/`)
: isSubPath(innerRequest, item.name)))
) {
/** @type {string} */
const remainingRequest = innerRequest.slice(item.name.length);
: isSubPath(innerRequest, item.name)));

const splitName = item.name.split("*");
const matchWildcard = !item.onlyModule && splitName.length === 2;

if (matchRequest || matchWildcard) {
/**
* @param {Alias} alias alias
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
Expand All @@ -90,12 +94,34 @@ module.exports = class AliasPlugin {
}
return callback(null, ignoreObj);
}

let newRequestStr;

const [prefix, suffix] = splitName;
if (
matchWildcard &&
innerRequest.startsWith(prefix) &&
innerRequest.endsWith(suffix)
) {
const match = innerRequest.slice(
prefix.length,
innerRequest.length - suffix.length
);
newRequestStr = item.alias.toString().replace("*", match);
}

if (
matchRequest &&
innerRequest !== alias &&
!innerRequest.startsWith(alias + "/")
) {
/** @type {string} */
const remainingRequest = innerRequest.slice(item.name.length);
newRequestStr = alias + remainingRequest;
}

if (newRequestStr !== undefined) {
shouldStop = true;
const newRequestStr = alias + remainingRequest;
/** @type {ResolveRequest} */
const obj = {
...request,
Expand All @@ -122,6 +148,7 @@ module.exports = class AliasPlugin {
}
return callback();
};

/**
* @param {null|Error} [err] error
* @param {null|ResolveRequest} [result] result
Expand All @@ -135,6 +162,7 @@ module.exports = class AliasPlugin {
if (shouldStop) return callback(null, null);
return callback();
};

if (Array.isArray(item.alias)) {
return forEachBail(
item.alias,
Expand All @@ -145,6 +173,7 @@ module.exports = class AliasPlugin {
return resolveWithAlias(item.alias, stoppingCallback);
}
}

return callback();
},
callback
Expand Down
13 changes: 13 additions & 0 deletions test/alias.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ describe("alias", () => {
// alias configuration should work
"#": "/c/dir",
"@": "/c/dir",
"@*": "/*",
"@e*": "/e/*",
"@e*file": "/e*file",
ignored: false
},
modules: "/",
Expand Down Expand Up @@ -73,6 +76,16 @@ describe("alias", () => {
expect(resolver.resolveSync({}, "/", "@")).toEqual("/c/dir/index");
expect(resolver.resolveSync({}, "/", "@/index")).toEqual("/c/dir/index");
});

it("should resolve wildcard alias", () => {
expect(resolver.resolveSync({}, "/", "@a")).toEqual("/a/index");
expect(resolver.resolveSync({}, "/", "@a/dir")).toEqual("/a/dir/index");
expect(resolver.resolveSync({}, "/", "@e/dir/file")).toEqual("/e/dir/file");
expect(resolver.resolveSync({}, "/", "@e/anotherDir")).toEqual(
"/e/anotherDir/index"
);
expect(resolver.resolveSync({}, "/", "@e/dir/file")).toEqual("/e/dir/file");
});
it("should resolve an ignore module", () => {
expect(resolver.resolveSync({}, "/", "ignored")).toEqual(false);
});
Expand Down
Loading