Skip to content

Commit 3a9a876

Browse files
committed
fix: test
1 parent 63b70b2 commit 3a9a876

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

lib/TsconfigPathsPlugin.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ module.exports = class TsconfigPathsPlugin {
3535
apply(resolver) {
3636
if (!this.configFile) return;
3737

38-
const fs = resolver.fileSystem;
3938
const aliasTarget = resolver.ensureHook("internal-resolve");
4039
const moduleTarget = resolver.ensureHook("module");
41-
const aliasOptionsPromise = this.loadAndConvertPaths(fs);
40+
const aliasOptionsPromise = this.loadAndConvertPaths(resolver);
4241

4342
resolver
4443
.getHook("raw-resolve")
@@ -90,25 +89,30 @@ module.exports = class TsconfigPathsPlugin {
9089

9190
/**
9291
* Load tsconfig.json (and referenced tsconfigs) and convert paths to AliasPlugin format
93-
* @param {import("./Resolver").FileSystem} fs the file system
92+
* @param {Resolver} resolver the file system
9493
* @returns {Promise<AliasOption[]>} Array of alias options for AliasPlugin
9594
*/
96-
async loadAndConvertPaths(fs) {
95+
async loadAndConvertPaths(resolver) {
9796
try {
97+
const fs = resolver.fileSystem;
9898
const configPath = path.isAbsolute(this.configFile)
9999
? this.configFile
100-
: path.resolve(process.cwd(), this.configFile);
100+
: resolver.join(process.cwd(), this.configFile);
101101

102102
const mainOptions = await this.readTsconfigCompilerOptions(
103-
fs,
103+
resolver,
104104
configPath,
105105
);
106106
if (!mainOptions) return [];
107107

108108
/** @type {AliasOption[]} */
109109
const aliases = [];
110110
aliases.push(
111-
...this.convertPathsToAliases(mainOptions.paths, mainOptions.baseUrl),
111+
...this.convertPathsToAliases(
112+
resolver,
113+
mainOptions.paths,
114+
mainOptions.baseUrl,
115+
),
112116
);
113117

114118
// Collect references from the main tsconfig.json
@@ -129,7 +133,7 @@ module.exports = class TsconfigPathsPlugin {
129133
if (!refPathLike) continue;
130134
let refPath = path.isAbsolute(refPathLike)
131135
? refPathLike
132-
: path.resolve(path.dirname(configPath), refPathLike);
136+
: resolver.join(path.dirname(configPath), refPathLike);
133137
// If reference points to a directory, append tsconfig.json
134138
try {
135139
const stat = await new Promise((resolve, reject) => {
@@ -139,19 +143,23 @@ module.exports = class TsconfigPathsPlugin {
139143
});
140144
});
141145
if (stat.isDirectory()) {
142-
refPath = path.join(refPath, "tsconfig.json");
146+
refPath = resolver.join(refPath, "tsconfig.json");
143147
}
144148
} catch (_e) {
145149
// if it doesn't exist as directory/file, try adding tsconfig.json
146150
if (!/\.json$/i.test(refPath)) {
147-
refPath = path.join(refPath, "tsconfig.json");
151+
refPath = resolver.join(refPath, "tsconfig.json");
148152
}
149153
}
150154

151155
const refOptions = await this.readTsconfigCompilerOptions(fs, refPath);
152156
if (!refOptions) continue;
153157
aliases.push(
154-
...this.convertPathsToAliases(refOptions.paths, refOptions.baseUrl),
158+
...this.convertPathsToAliases(
159+
resolver,
160+
refOptions.paths,
161+
refOptions.baseUrl,
162+
),
155163
);
156164
}
157165

@@ -163,12 +171,13 @@ module.exports = class TsconfigPathsPlugin {
163171

164172
/**
165173
* Read tsconfig.json and return normalized compiler options
166-
* @param {import("./Resolver").FileSystem} fs the file system
174+
* @param {Resolver} resolver the resolver
167175
* @param {string} absTsconfigPath absolute path to tsconfig.json
168176
* @returns {Promise<{ baseUrl: string, paths: {[key: string]: string[]} } | null>} the normalized compiler options
169177
*/
170-
async readTsconfigCompilerOptions(fs, absTsconfigPath) {
178+
async readTsconfigCompilerOptions(resolver, absTsconfigPath) {
171179
try {
180+
const fs = resolver.fileSystem;
172181
const json = await new Promise((resolve, reject) => {
173182
fs.readFile(absTsconfigPath, "utf8", (err, data) => {
174183
if (err) reject(err);
@@ -181,7 +190,7 @@ module.exports = class TsconfigPathsPlugin {
181190
if (!baseUrl) {
182191
baseUrl = path.dirname(absTsconfigPath);
183192
} else if (!path.isAbsolute(baseUrl)) {
184-
baseUrl = path.resolve(path.dirname(absTsconfigPath), baseUrl);
193+
baseUrl = resolver.join(path.dirname(absTsconfigPath), baseUrl);
185194
}
186195
const paths = compilerOptions.paths || {};
187196
return { baseUrl, paths };
@@ -192,27 +201,28 @@ module.exports = class TsconfigPathsPlugin {
192201

193202
/**
194203
* Convert TypeScript paths configuration to AliasPlugin aliases
204+
* @param {Resolver} resolver the resolver
195205
* @param {{[key: string]: string[]}} paths TypeScript paths mapping
196206
* @param {string} baseUrl Base URL for resolving paths
197207
* @returns {AliasOption[]} Array of alias options
198208
*/
199-
convertPathsToAliases(paths, baseUrl) {
209+
convertPathsToAliases(resolver, paths, baseUrl) {
200210
/** @type {AliasOption[]} */
201211
const aliases = [];
202212

203213
for (const [pattern, mappings] of Object.entries(paths)) {
204214
// Handle exact matches (no wildcards)
205215
if (!pattern.includes("*")) {
206216
if (mappings.length > 0) {
207-
const targetPath = path.resolve(baseUrl, mappings[0]);
217+
const targetPath = resolver.join(baseUrl, mappings[0]);
208218
aliases.push({ name: pattern, alias: targetPath });
209219
}
210220
} else {
211221
// Handle wildcard patterns by mapping the directory
212222
const aliasName = pattern.replace(/\/\*$/, "");
213223
// Convert targets like "dir/*" -> "dir"
214224
const aliasTargets = mappings.map((mapping) =>
215-
path.resolve(baseUrl, mapping.replace(/\/\*$/, "")),
225+
resolver.join(baseUrl, mapping.replace(/\/\*$/, "")),
216226
);
217227
if (aliasTargets.length > 0) {
218228
aliases.push({ name: aliasName, alias: aliasTargets[0] });

types.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,21 +1593,22 @@ declare class TsconfigPathsPlugin {
15931593
* Load tsconfig.json (and referenced tsconfigs) and convert paths to AliasPlugin format
15941594
*/
15951595
loadAndConvertPaths(
1596-
fs: FileSystem,
1596+
resolver: Resolver,
15971597
): Promise<AliasOptionTsconfigPathsPlugin[]>;
15981598

15991599
/**
16001600
* Read tsconfig.json and return normalized compiler options
16011601
*/
16021602
readTsconfigCompilerOptions(
1603-
fs: FileSystem,
1603+
resolver: Resolver,
16041604
absTsconfigPath: string,
16051605
): Promise<null | { baseUrl: string; paths: { [index: string]: string[] } }>;
16061606

16071607
/**
16081608
* Convert TypeScript paths configuration to AliasPlugin aliases
16091609
*/
16101610
convertPathsToAliases(
1611+
resolver: Resolver,
16111612
paths: { [index: string]: string[] },
16121613
baseUrl: string,
16131614
): AliasOptionTsconfigPathsPlugin[];

0 commit comments

Comments
 (0)