Skip to content

Commit 6af0057

Browse files
committed
update
1 parent f45b2f3 commit 6af0057

File tree

2 files changed

+34
-69
lines changed

2 files changed

+34
-69
lines changed

lib/TsconfigPathsUtils.js

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"use strict";
77

88
const path = require("path");
9-
const { isAbsolute, join, normalize } = require("./util/path");
9+
const { join, normalize } = require("./util/path");
1010

1111
/** @typedef {import("./Resolver")} Resolver */
1212
/** @typedef {import("./Resolver").FileSystem} FileSystem */
@@ -47,34 +47,6 @@ function sortByLongestPrefix(arr) {
4747
return [...arr].sort((a, b) => getPrefixLength(b) - getPrefixLength(a));
4848
}
4949

50-
/**
51-
* Converts an absolute baseUrl and paths to an array of absolute mapping entries.
52-
* The array is sorted by longest prefix.
53-
* Having an array with entries allows us to keep a sorting order rather than
54-
* sort by keys each time we use the mappings.
55-
* @param {{[key: string]: string[]}} paths TypeScript paths mapping
56-
* @param {string} baseUrl Base URL for resolving paths
57-
* @returns {AliasOption[]} Array of alias options
58-
*/
59-
function getAbsoluteMappingEntries(paths, baseUrl) {
60-
/** @type {string[]} */
61-
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
62-
/** @type {AliasOption[]} */
63-
const absolutePaths = [];
64-
65-
for (const pattern of sortedKeys) {
66-
const mappings = paths[pattern];
67-
// const aliasName = pattern.replace(/\/\*$/, "");
68-
// Convert targets like "dir/*" -> "dir"
69-
const aliasTargets = mappings.map((mapping) => join(baseUrl, mapping));
70-
if (aliasTargets.length > 0) {
71-
absolutePaths.push({ name: pattern, alias: aliasTargets });
72-
}
73-
}
74-
75-
return absolutePaths;
76-
}
77-
7850
/**
7951
* Merge two tsconfig objects
8052
* @param {Tsconfig} base base config
@@ -235,11 +207,9 @@ async function readTsconfigCompilerOptions(fileSystem, absTsconfigPath) {
235207
};
236208
let { baseUrl } = compilerOptions;
237209

238-
if (!baseUrl) {
239-
baseUrl = path.dirname(absTsconfigPath);
240-
} else if (!isAbsolute(baseUrl)) {
241-
baseUrl = join(path.dirname(absTsconfigPath), baseUrl);
242-
}
210+
baseUrl = !baseUrl
211+
? path.dirname(absTsconfigPath)
212+
: join(path.dirname(absTsconfigPath), baseUrl);
243213

244214
const paths = compilerOptions.paths || {};
245215
return { options: { baseUrl, paths }, fileDependencies };
@@ -250,30 +220,42 @@ async function readTsconfigCompilerOptions(fileSystem, absTsconfigPath) {
250220

251221
/**
252222
* Convert tsconfig paths to resolver options
253-
* @param {AliasOption[]} aliases the aliases
223+
* @param {{[key: string]: string[]}} paths TypeScript paths mapping
224+
* @param {string} baseUrl Base URL for resolving paths
254225
* @returns {Omit<TsconfigPathsData, "fileDependencies">} the resolver options
255226
*/
256-
function pathsToResolveOptions(aliases) {
227+
function pathsToResolveOptions(paths, baseUrl) {
228+
/** @type {string[]} */
229+
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
257230
/** @type {AliasOption[]} */
258231
const alias = [];
259232
/** @type {string[]} */
260233
const modules = [];
261-
for (const opt of aliases) {
262-
if (opt.name === "*") {
263-
modules.push(
264-
.../** @type {Array<string>} */ (opt.alias)
265-
.map((dir) => {
266-
if (/[/\\]\*$/.test(dir)) {
267-
return dir.replace(/[/\\]\*$/, "");
268-
}
269-
return "";
270-
})
271-
.filter(Boolean),
272-
);
273-
} else {
274-
alias.push(opt);
234+
235+
for (const pattern of sortedKeys) {
236+
const mappings = paths[pattern];
237+
const aliasTargets = mappings.map((mapping) => join(baseUrl, mapping));
238+
239+
if (aliasTargets.length > 0) {
240+
if (pattern === "*") {
241+
// Handle "*" pattern - extract modules from wildcard targets
242+
modules.push(
243+
...aliasTargets
244+
.map((dir) => {
245+
if (/[/\\]\*$/.test(dir)) {
246+
return dir.replace(/[/\\]\*$/, "");
247+
}
248+
return "";
249+
})
250+
.filter(Boolean),
251+
);
252+
} else {
253+
// Handle regular patterns - add as alias
254+
alias.push({ name: pattern, alias: aliasTargets });
255+
}
275256
}
276257
}
258+
277259
return {
278260
alias,
279261
modules,
@@ -293,25 +275,17 @@ async function loadTsconfigPathsData(
293275
context = process.cwd(),
294276
) {
295277
try {
296-
const absTsconfigPath = isAbsolute(configFile)
297-
? configFile
298-
: join(context, configFile);
299-
278+
const absTsconfigPath = join(context, configFile);
300279
const result = await readTsconfigCompilerOptions(
301280
fileSystem,
302281
absTsconfigPath,
303282
);
304283
if (!result) return null;
305284
const { options: compilerOptions, fileDependencies } = result;
306285
if (!compilerOptions) return null;
307-
/** @type {AliasOption[]} */
308-
const absolutePaths = getAbsoluteMappingEntries(
309-
compilerOptions.paths,
310-
compilerOptions.baseUrl,
311-
);
312286

313287
return {
314-
...pathsToResolveOptions(absolutePaths),
288+
...pathsToResolveOptions(compilerOptions.paths, compilerOptions.baseUrl),
315289
fileDependencies,
316290
};
317291
} catch (_e) {

lib/util/path.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ const getType = (maybePath) => {
120120
return PathType.Normal;
121121
};
122122

123-
/**
124-
* @param {string} path a path
125-
* @returns {boolean} true, if path is absolute
126-
*/
127-
const isAbsolute = (path) =>
128-
getType(path) === PathType.AbsolutePosix ||
129-
getType(path) === PathType.AbsoluteWin;
130-
131123
/**
132124
* @param {string} maybePath a path
133125
* @returns {string} the normalized path
@@ -207,6 +199,5 @@ module.exports.cachedJoin = cachedJoin;
207199
module.exports.deprecatedInvalidSegmentRegEx = deprecatedInvalidSegmentRegEx;
208200
module.exports.getType = getType;
209201
module.exports.invalidSegmentRegEx = invalidSegmentRegEx;
210-
module.exports.isAbsolute = isAbsolute;
211202
module.exports.join = join;
212203
module.exports.normalize = normalize;

0 commit comments

Comments
 (0)