Skip to content

Commit 271e145

Browse files
committed
feat: add TsconfigPathsPlugin
1 parent 79e2961 commit 271e145

File tree

54 files changed

+1992
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1992
-218
lines changed

.cspell.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
"zipp",
3535
"zippi",
3636
"zizizi",
37-
"codecov"
37+
"codecov",
38+
"xiaoxiaojx",
39+
"Natsu",
40+
"tsconfigs",
41+
"preact",
42+
"compat"
3843
],
3944
"ignorePaths": ["package.json", "yarn.lock", "coverage", "*.log"]
4045
}

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,36 @@ myResolver.resolve(
8585

8686
#### Resolver Options
8787

88-
| Field | Default | Description |
89-
| ---------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
90-
| alias | [] | A list of module alias configurations or an object which maps key to value |
91-
| aliasFields | [] | A list of alias fields in description files |
92-
| extensionAlias | {} | An object which maps extension to extension aliases |
93-
| cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
94-
| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
95-
| conditionNames | [] | A list of exports field condition names |
96-
| descriptionFiles | ["package.json"] | A list of description files to read from |
97-
| enforceExtension | false | Enforce that a extension from extensions must be used |
98-
| exportsFields | ["exports"] | A list of exports fields in description files |
99-
| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
100-
| fallback | [] | Same as `alias`, but only used if default resolving fails |
101-
| fileSystem | | The file system which should be used |
102-
| fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
103-
| mainFields | ["main"] | A list of main fields in description files |
104-
| mainFiles | ["index"] | A list of main files in directories |
105-
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
106-
| plugins | [] | A list of additional resolve plugins which should be applied |
107-
| resolver | undefined | A prepared Resolver to which the plugins are attached |
108-
| resolveToContext | false | Resolve to a context instead of a file |
109-
| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
110-
| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
111-
| restrictions | [] | A list of resolve restrictions |
112-
| roots | [] | A list of root paths |
113-
| symlinks | true | Whether to resolve symlinks to their symlinked location |
114-
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
88+
| Field | Default | Description |
89+
| ------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
90+
| alias | [] | A list of module alias configurations or an object which maps key to value |
91+
| aliasFields | [] | A list of alias fields in description files |
92+
| extensionAlias | {} | An object which maps extension to extension aliases |
93+
| cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
94+
| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
95+
| conditionNames | [] | A list of exports field condition names |
96+
| descriptionFiles | ["package.json"] | A list of description files to read from |
97+
| enforceExtension | false | Enforce that a extension from extensions must be used |
98+
| exportsFields | ["exports"] | A list of exports fields in description files |
99+
| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
100+
| fallback | [] | Same as `alias`, but only used if default resolving fails |
101+
| fileSystem | | The file system which should be used |
102+
| fullySpecified | false | Request passed to resolve is already fully specified and extensions or main files are not resolved for it (they are still resolved for internal requests) |
103+
| mainFields | ["main"] | A list of main fields in description files |
104+
| mainFiles | ["index"] | A list of main files in directories |
105+
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
106+
| plugins | [] | A list of additional resolve plugins which should be applied |
107+
| resolver | undefined | A prepared Resolver to which the plugins are attached |
108+
| resolveToContext | false | Resolve to a context instead of a file |
109+
| preferRelative | false | Prefer to resolve module requests as relative request and fallback to resolving as module |
110+
| preferAbsolute | false | Prefer to resolve server-relative urls as absolute paths before falling back to resolve in roots |
111+
| restrictions | [] | A list of resolve restrictions |
112+
| roots | [] | A list of root paths |
113+
| symlinks | true | Whether to resolve symlinks to their symlinked location |
114+
| tsconfig | false | TypeScript config for paths mapping. Can be `false` (disabled), a string path to `tsconfig.json`, or an object with `configFile` and `references` options. |
115+
| tsconfig.configFile | tsconfig.json | Path to the tsconfig.json file |
116+
| tsconfig.references | [] | Project references. `'auto'` to load from tsconfig, or an array of paths to referenced projects |
117+
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
115118

116119
## Plugins
117120

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 = 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
});

0 commit comments

Comments
 (0)