Skip to content

Commit 9243884

Browse files
committed
feat: add TsconfigPathsPlugin
1 parent f1bc1c2 commit 9243884

File tree

36 files changed

+947
-195
lines changed

36 files changed

+947
-195
lines changed

.cspell.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
"zipp",
3535
"zippi",
3636
"zizizi",
37-
"codecov"
37+
"codecov",
38+
"xiaoxiaojx",
39+
"Natsu",
40+
"tsconfigs"
3841
],
3942
"ignorePaths": ["package.json", "yarn.lock", "coverage", "*.log"]
4043
}

lib/AliasPlugin.js

Lines changed: 8 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
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

13+
const { aliasResolveHandler } = require("./AliasUtils");
14+
1715
module.exports = class AliasPlugin {
1816
/**
1917
* @param {string | ResolveStepHook} source source
@@ -32,143 +30,16 @@ module.exports = class AliasPlugin {
3230
*/
3331
apply(resolver) {
3432
const target = resolver.ensureHook(this.target);
35-
/**
36-
* @param {string} maybeAbsolutePath path
37-
* @returns {null|string} absolute path with slash ending
38-
*/
39-
const getAbsolutePathWithSlashEnding = (maybeAbsolutePath) => {
40-
const type = getType(maybeAbsolutePath);
41-
if (type === PathType.AbsolutePosix || type === PathType.AbsoluteWin) {
42-
return resolver.join(maybeAbsolutePath, "_").slice(0, -1);
43-
}
44-
return null;
45-
};
46-
/**
47-
* @param {string} path path
48-
* @param {string} maybeSubPath sub path
49-
* @returns {boolean} true, if path is sub path
50-
*/
51-
const isSubPath = (path, maybeSubPath) => {
52-
const absolutePath = getAbsolutePathWithSlashEnding(maybeSubPath);
53-
if (!absolutePath) return false;
54-
return path.startsWith(absolutePath);
55-
};
33+
5634
resolver
5735
.getHook(this.source)
5836
.tapAsync("AliasPlugin", (request, resolveContext, callback) => {
59-
const innerRequest = request.request || request.path;
60-
if (!innerRequest) return callback();
61-
62-
forEachBail(
37+
aliasResolveHandler(
38+
resolver,
6339
this.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(
162-
item.alias,
163-
resolveWithAlias,
164-
stoppingCallback,
165-
);
166-
}
167-
return resolveWithAlias(item.alias, stoppingCallback);
168-
}
169-
170-
return callback();
171-
},
40+
target,
41+
request,
42+
resolveContext,
17243
callback,
17344
);
17445
});

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)