Skip to content

Commit 3cfdf3b

Browse files
committed
fix: test
1 parent a7d9693 commit 3cfdf3b

File tree

34 files changed

+397
-240
lines changed

34 files changed

+397
-240
lines changed

lib/AliasPlugin.js

Lines changed: 12 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@
55

66
"use strict";
77

8-
const forEachBail = require("./forEachBail");
9-
const { PathType, getType } = require("./util/path");
10-
118
/** @typedef {import("./Resolver")} Resolver */
12-
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
139
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
1410
/** @typedef {string | Array<string> | false} Alias */
1511
/** @typedef {{alias: Alias, name: string, onlyModule?: boolean}} AliasOption */
1612

17-
/** @typedef { AliasOption | Array<AliasOption> } BaseAliasOptions */
13+
const { aliasResolveHandler } = require("./AliasUtils");
1814

1915
module.exports = class AliasPlugin {
2016
/**
2117
* @param {string | ResolveStepHook} source source
22-
* @param {BaseAliasOptions | Promise<BaseAliasOptions>} options options
18+
* @param {AliasOption | Array<AliasOption>} options options
2319
* @param {string | ResolveStepHook} target target
2420
*/
2521
constructor(source, options, target) {
2622
this.source = source;
27-
this.options = options;
23+
this.options = Array.isArray(options) ? options : [options];
2824
this.target = target;
2925
}
3026

@@ -34,165 +30,18 @@ module.exports = class AliasPlugin {
3430
*/
3531
apply(resolver) {
3632
const target = resolver.ensureHook(this.target);
37-
/**
38-
* @param {string} maybeAbsolutePath path
39-
* @returns {null|string} absolute path with slash ending
40-
*/
41-
const getAbsolutePathWithSlashEnding = (maybeAbsolutePath) => {
42-
const type = getType(maybeAbsolutePath);
43-
if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
44-
return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
45-
}
46-
return null;
47-
};
48-
/**
49-
* @param {string} path path
50-
* @param {string} maybeSubPath sub path
51-
* @returns {boolean} true, if path is sub path
52-
*/
53-
const isSubPath = (path, maybeSubPath) => {
54-
const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
55-
if (!absolutePath) return false;
56-
return path.startsWith(absolutePath);
57-
};
33+
5834
resolver
5935
.getHook(this.source)
6036
.tapAsync("AliasPlugin", (request, resolveContext, callback) => {
61-
const innerRequest = request.request || request.path;
62-
if (!innerRequest) return callback();
63-
64-
/**
65-
* @param {AliasOption | Array<AliasOption>} options options
66-
* @returns {void}
67-
*/
68-
const applyOptions = (options) => {
69-
const normalizedOptions = Array.isArray(options)
70-
? options
71-
: [options];
72-
73-
forEachBail(
74-
normalizedOptions,
75-
(item, callback) => {
76-
/** @type {boolean} */
77-
let shouldStop = false;
78-
79-
const matchRequest =
80-
innerRequest === item.name ||
81-
(!item.onlyModule &&
82-
(request.request
83-
? innerRequest.startsWith(`${item.name}/`)
84-
: isSubPath(innerRequest, item.name)));
85-
86-
const splitName = item.name.split("*");
87-
const matchWildcard = !item.onlyModule && splitName.length === 2;
88-
89-
if (matchRequest || matchWildcard) {
90-
/**
91-
* @param {Alias} alias alias
92-
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
93-
* @returns {void}
94-
*/
95-
const resolveWithAlias = (alias, callback) => {
96-
if (alias === false) {
97-
/** @type {ResolveRequest} */
98-
const ignoreObj = {
99-
...request,
100-
path: false,
101-
};
102-
if (typeof resolveContext.yield === "function") {
103-
resolveContext.yield(ignoreObj);
104-
return callback(null, null);
105-
}
106-
return callback(null, ignoreObj);
107-
}
108-
109-
let newRequestStr;
110-
111-
const [prefix, suffix] = splitName;
112-
if (
113-
matchWildcard &&
114-
innerRequest.startsWith(prefix) &&
115-
innerRequest.endsWith(suffix)
116-
) {
117-
const match = innerRequest.slice(
118-
prefix.length,
119-
innerRequest.length - suffix.length,
120-
);
121-
newRequestStr = item.alias.toString().replace("*", match);
122-
}
123-
124-
if (
125-
matchRequest &&
126-
innerRequest !== alias &&
127-
!innerRequest.startsWith(`${alias}/`)
128-
) {
129-
/** @type {string} */
130-
const remainingRequest = innerRequest.slice(
131-
item.name.length,
132-
);
133-
newRequestStr = alias + remainingRequest;
134-
}
135-
136-
if (newRequestStr !== undefined) {
137-
shouldStop = true;
138-
/** @type {ResolveRequest} */
139-
const obj = {
140-
...request,
141-
request: newRequestStr,
142-
fullySpecified: false,
143-
};
144-
return resolver.doResolve(
145-
target,
146-
obj,
147-
`aliased with mapping '${item.name}': '${alias}' to '${newRequestStr}'`,
148-
resolveContext,
149-
(err, result) => {
150-
if (err) return callback(err);
151-
if (result) return callback(null, result);
152-
return callback();
153-
},
154-
);
155-
}
156-
return callback();
157-
};
158-
159-
/**
160-
* @param {(null | Error)=} err error
161-
* @param {(null | ResolveRequest)=} result result
162-
* @returns {void}
163-
*/
164-
const stoppingCallback = (err, result) => {
165-
if (err) return callback(err);
166-
167-
if (result) return callback(null, result);
168-
// Don't allow other aliasing or raw request
169-
if (shouldStop) return callback(null, null);
170-
return callback();
171-
};
172-
173-
if (Array.isArray(item.alias)) {
174-
return forEachBail(
175-
item.alias,
176-
resolveWithAlias,
177-
stoppingCallback,
178-
);
179-
}
180-
return resolveWithAlias(item.alias, stoppingCallback);
181-
}
182-
183-
return callback();
184-
},
185-
callback,
186-
);
187-
};
188-
189-
if (this.options instanceof Promise) {
190-
this.options.then((options) => {
191-
applyOptions(options);
192-
});
193-
} else {
194-
applyOptions(this.options);
195-
}
37+
aliasResolveHandler(
38+
resolver,
39+
this.options,
40+
target,
41+
request,
42+
resolveContext,
43+
callback,
44+
);
19645
});
19746
}
19847
};

lib/AliasUtils.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
6+
"use strict";
7+
8+
const forEachBail = require("./forEachBail");
9+
const { PathType, getType } = require("./util/path");
10+
11+
/** @typedef {import("./Resolver")} Resolver */
12+
/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
13+
/** @typedef {import("./Resolver").ResolveContext} ResolveContext */
14+
/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
15+
/** @typedef {import("./Resolver").ResolveCallback} ResolveCallback */
16+
/** @typedef {string | Array<string> | false} Alias */
17+
/** @typedef {{alias: Alias, name: string, onlyModule?: boolean}} AliasOption */
18+
19+
/** @typedef {(err?: null | Error, result?: null | ResolveRequest) => void} InnerCallback */
20+
/**
21+
* @param {Resolver} resolver resolver
22+
* @param {Array<AliasOption>} options options
23+
* @param {ResolveStepHook} target target
24+
* @param {ResolveRequest} request request
25+
* @param {ResolveContext} resolveContext resolve context
26+
* @param {InnerCallback} callback callback
27+
* @returns {void}
28+
*/
29+
function aliasResolveHandler(
30+
resolver,
31+
options,
32+
target,
33+
request,
34+
resolveContext,
35+
callback,
36+
) {
37+
const innerRequest = request.request || request.path;
38+
if (!innerRequest) return callback();
39+
40+
/**
41+
* @param {string} maybeAbsolutePath path
42+
* @returns {null|string} absolute path with slash ending
43+
*/
44+
const getAbsolutePathWithSlashEnding = (maybeAbsolutePath) => {
45+
const type = getType(maybeAbsolutePath);
46+
if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
47+
return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
48+
}
49+
return null;
50+
};
51+
/**
52+
* @param {string} path path
53+
* @param {string} maybeSubPath sub path
54+
* @returns {boolean} true, if path is sub path
55+
*/
56+
const isSubPath = (path, maybeSubPath) => {
57+
const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
58+
if (!absolutePath) return false;
59+
return path.startsWith(absolutePath);
60+
};
61+
62+
forEachBail(
63+
options,
64+
(item, callback) => {
65+
/** @type {boolean} */
66+
let shouldStop = false;
67+
68+
const matchRequest =
69+
innerRequest === item.name ||
70+
(!item.onlyModule &&
71+
(request.request
72+
? innerRequest.startsWith(`${item.name}/`)
73+
: isSubPath(innerRequest, item.name)));
74+
75+
const splitName = item.name.split("*");
76+
const matchWildcard = !item.onlyModule && splitName.length === 2;
77+
78+
if (matchRequest || matchWildcard) {
79+
/**
80+
* @param {Alias} alias alias
81+
* @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
82+
* @returns {void}
83+
*/
84+
const resolveWithAlias = (alias, callback) => {
85+
if (alias === false) {
86+
/** @type {ResolveRequest} */
87+
const ignoreObj = {
88+
...request,
89+
path: false,
90+
};
91+
if (typeof resolveContext.yield === "function") {
92+
resolveContext.yield(ignoreObj);
93+
return callback(null, null);
94+
}
95+
return callback(null, ignoreObj);
96+
}
97+
98+
let newRequestStr;
99+
100+
const [prefix, suffix] = splitName;
101+
if (
102+
matchWildcard &&
103+
innerRequest.startsWith(prefix) &&
104+
innerRequest.endsWith(suffix)
105+
) {
106+
const match = innerRequest.slice(
107+
prefix.length,
108+
innerRequest.length - suffix.length,
109+
);
110+
newRequestStr = item.alias.toString().replace("*", match);
111+
}
112+
113+
if (
114+
matchRequest &&
115+
innerRequest !== alias &&
116+
!innerRequest.startsWith(`${alias}/`)
117+
) {
118+
/** @type {string} */
119+
const remainingRequest = innerRequest.slice(item.name.length);
120+
newRequestStr = alias + remainingRequest;
121+
}
122+
123+
if (newRequestStr !== undefined) {
124+
shouldStop = true;
125+
/** @type {ResolveRequest} */
126+
const obj = {
127+
...request,
128+
request: newRequestStr,
129+
fullySpecified: false,
130+
};
131+
return resolver.doResolve(
132+
target,
133+
obj,
134+
`aliased with mapping '${item.name}': '${alias}' to '${newRequestStr}'`,
135+
resolveContext,
136+
(err, result) => {
137+
if (err) return callback(err);
138+
if (result) return callback(null, result);
139+
return callback();
140+
},
141+
);
142+
}
143+
return callback();
144+
};
145+
146+
/**
147+
* @param {(null | Error)=} err error
148+
* @param {(null | ResolveRequest)=} result result
149+
* @returns {void}
150+
*/
151+
const stoppingCallback = (err, result) => {
152+
if (err) return callback(err);
153+
154+
if (result) return callback(null, result);
155+
// Don't allow other aliasing or raw request
156+
if (shouldStop) return callback(null, null);
157+
return callback();
158+
};
159+
160+
if (Array.isArray(item.alias)) {
161+
return forEachBail(item.alias, resolveWithAlias, stoppingCallback);
162+
}
163+
return resolveWithAlias(item.alias, stoppingCallback);
164+
}
165+
166+
return callback();
167+
},
168+
callback,
169+
);
170+
}
171+
172+
module.exports.aliasResolveHandler = aliasResolveHandler;

0 commit comments

Comments
 (0)