Skip to content

Commit 4f5d243

Browse files
committed
Put paths specifiers to redirects in lower priority bucket
1 parent 1d25792 commit 4f5d243

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ function computeModuleSpecifiers(
275275
// 4. Relative paths
276276
let nodeModulesSpecifiers: string[] | undefined;
277277
let pathsSpecifiers: string[] | undefined;
278+
let redirectPathsSpecifiers: string[] | undefined;
278279
let relativeSpecifiers: string[] | undefined;
279280
for (const modulePath of modulePaths) {
280281
const specifier = tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode);
@@ -286,11 +287,25 @@ function computeModuleSpecifiers(
286287
}
287288

288289
if (!specifier) {
289-
const local = getLocalModuleSpecifier(modulePath.path, info, compilerOptions, host, options.overrideImportMode || importingSourceFile.impliedNodeFormat, preferences);
290-
if (pathIsBareSpecifier(local)) {
290+
const local = getLocalModuleSpecifier(
291+
modulePath.path,
292+
info,
293+
compilerOptions,
294+
host,
295+
options.overrideImportMode || importingSourceFile.impliedNodeFormat,
296+
preferences,
297+
/*pathsOnly*/ modulePath.isRedirect,
298+
);
299+
if (!local) {
300+
continue;
301+
}
302+
if (modulePath.isRedirect) {
303+
redirectPathsSpecifiers = append(redirectPathsSpecifiers, local);
304+
}
305+
else if (pathIsBareSpecifier(local)) {
291306
pathsSpecifiers = append(pathsSpecifiers, local);
292307
}
293-
else if (!modulePath.isRedirect && (!importedFileIsInNodeModules || modulePath.isInNodeModules)) {
308+
else if (!importedFileIsInNodeModules || modulePath.isInNodeModules) {
294309
// Why this extra conditional, not just an `else`? If some path to the file contained
295310
// 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
296311
// that means we had to go through a *sibling's* node_modules, not one we can access directly.
@@ -306,8 +321,9 @@ function computeModuleSpecifiers(
306321
}
307322

308323
return pathsSpecifiers?.length ? pathsSpecifiers :
324+
redirectPathsSpecifiers?.length ? redirectPathsSpecifiers :
309325
nodeModulesSpecifiers?.length ? nodeModulesSpecifiers :
310-
Debug.checkDefined(relativeSpecifiers);
326+
Debug.checkDefined(relativeSpecifiers);
311327
}
312328

313329
interface Info {
@@ -322,22 +338,32 @@ function getInfo(importingSourceFileName: Path, host: ModuleSpecifierResolutionH
322338
return { getCanonicalFileName, importingSourceFileName, sourceDirectory };
323339
}
324340

325-
function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOptions: CompilerOptions, host: ModuleSpecifierResolutionHost, importMode: ResolutionMode, { ending, relativePreference }: Preferences): string {
341+
function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOptions: CompilerOptions, host: ModuleSpecifierResolutionHost, importMode: ResolutionMode, { ending, relativePreference }: Preferences): string;
342+
function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOptions: CompilerOptions, host: ModuleSpecifierResolutionHost, importMode: ResolutionMode, { ending, relativePreference }: Preferences, pathsOnly?: boolean): string | undefined;
343+
function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOptions: CompilerOptions, host: ModuleSpecifierResolutionHost, importMode: ResolutionMode, { ending, relativePreference }: Preferences, pathsOnly?: boolean): string | undefined {
326344
const { baseUrl, paths, rootDirs } = compilerOptions;
345+
if (pathsOnly && !paths) {
346+
return undefined;
347+
}
348+
327349
const { sourceDirectory, getCanonicalFileName } = info;
328350
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
329351
removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
330352
if (!baseUrl && !paths || relativePreference === RelativePreference.Relative) {
331-
return relativePath;
353+
return pathsOnly ? undefined : relativePath;
332354
}
333355

334356
const baseDirectory = getNormalizedAbsolutePath(getPathsBasePath(compilerOptions, host) || baseUrl!, host.getCurrentDirectory());
335357
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
336358
if (!relativeToBaseUrl) {
337-
return relativePath;
359+
return pathsOnly ? undefined : relativePath;
338360
}
339361

340362
const fromPaths = paths && tryGetModuleNameFromPaths(relativeToBaseUrl, paths, getAllowedEndings(ending, compilerOptions, importMode), host, compilerOptions);
363+
if (pathsOnly) {
364+
return fromPaths;
365+
}
366+
341367
const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? removeExtensionAndIndexPostFix(relativeToBaseUrl, ending, compilerOptions) : fromPaths;
342368
if (!maybeNonRelative) {
343369
return relativePath;

0 commit comments

Comments
 (0)