Skip to content

Commit

Permalink
feat: add wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
sapenlei committed Dec 20, 2024
1 parent 4fbcfa1 commit 35b67ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
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

0 comments on commit 35b67ce

Please sign in to comment.