From 35b67ce83434be840c6af0012449d94130ee4dd7 Mon Sep 17 00:00:00 2001 From: sapenlei <2503404258gl@gmail.com> Date: Fri, 20 Dec 2024 10:39:47 +0800 Subject: [PATCH] feat: add wildcards --- lib/AliasPlugin.js | 41 +++++++++++++++++++++++++++++++++++------ test/alias.test.js | 13 +++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lib/AliasPlugin.js b/lib/AliasPlugin.js index bbe9724a..ce6ea246 100644 --- a/lib/AliasPlugin.js +++ b/lib/AliasPlugin.js @@ -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 @@ -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, @@ -122,6 +148,7 @@ module.exports = class AliasPlugin { } return callback(); }; + /** * @param {null|Error} [err] error * @param {null|ResolveRequest} [result] result @@ -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, @@ -145,6 +173,7 @@ module.exports = class AliasPlugin { return resolveWithAlias(item.alias, stoppingCallback); } } + return callback(); }, callback diff --git a/test/alias.test.js b/test/alias.test.js index 81cf5bcb..8a81cab6 100644 --- a/test/alias.test.js +++ b/test/alias.test.js @@ -40,6 +40,9 @@ describe("alias", () => { // alias configuration should work "#": "/c/dir", "@": "/c/dir", + "@*": "/*", + "@e*": "/e/*", + "@e*file": "/e*file", ignored: false }, modules: "/", @@ -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); });