@@ -35,6 +35,7 @@ import {
35
35
getBaseFileName ,
36
36
GetCanonicalFileName ,
37
37
getConditions ,
38
+ getDefaultResolutionModeForFile ,
38
39
getDirectoryPath ,
39
40
getEmitModuleResolutionKind ,
40
41
getModeForResolutionAtIndex ,
@@ -134,7 +135,7 @@ export interface ModuleSpecifierPreferences {
134
135
/**
135
136
* @param syntaxImpliedNodeFormat Used when the import syntax implies ESM or CJS irrespective of the mode of the file.
136
137
*/
137
- getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: SourceFile [ "impliedNodeFormat" ] ) : ModuleSpecifierEnding [ ] ;
138
+ getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: ResolutionMode ) : ModuleSpecifierEnding [ ] ;
138
139
}
139
140
140
141
/** @internal */
@@ -154,8 +155,10 @@ export function getModuleSpecifierPreferences(
154
155
importModuleSpecifierPreference === "project-relative" ? RelativePreference . ExternalNonRelative :
155
156
RelativePreference . Shortest ,
156
157
getAllowedEndingsInPreferredOrder : syntaxImpliedNodeFormat => {
157
- const preferredEnding = syntaxImpliedNodeFormat !== importingSourceFile . impliedNodeFormat ? getPreferredEnding ( syntaxImpliedNodeFormat ) : filePreferredEnding ;
158
- if ( ( syntaxImpliedNodeFormat ?? importingSourceFile . impliedNodeFormat ) === ModuleKind . ESNext ) {
158
+ const impliedNodeFormat = getDefaultResolutionModeForFile ( importingSourceFile , compilerOptions ) ;
159
+ const preferredEnding = syntaxImpliedNodeFormat !== impliedNodeFormat ? getPreferredEnding ( syntaxImpliedNodeFormat ) : filePreferredEnding ;
160
+ const moduleResolution = getEmitModuleResolutionKind ( compilerOptions ) ;
161
+ if ( ( syntaxImpliedNodeFormat ?? impliedNodeFormat ) === ModuleKind . ESNext && ModuleResolutionKind . Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind . NodeNext ) {
159
162
if ( shouldAllowImportingTsExtension ( compilerOptions , importingSourceFile . fileName ) ) {
160
163
return [ ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . JsExtension ] ;
161
164
}
@@ -195,7 +198,7 @@ export function getModuleSpecifierPreferences(
195
198
}
196
199
return getModuleSpecifierEndingPreference (
197
200
importModuleSpecifierEnding ,
198
- resolutionMode ?? importingSourceFile . impliedNodeFormat ,
201
+ resolutionMode ?? getDefaultResolutionModeForFile ( importingSourceFile , compilerOptions ) ,
199
202
compilerOptions ,
200
203
importingSourceFile ,
201
204
) ;
@@ -266,7 +269,7 @@ function getModuleSpecifierWorker(
266
269
const info = getInfo ( importingSourceFileName , host ) ;
267
270
const modulePaths = getAllModulePaths ( info , toFileName , host , userPreferences , options ) ;
268
271
return firstDefined ( modulePaths , modulePath => tryGetModuleNameAsNodeModule ( modulePath , info , importingSourceFile , host , compilerOptions , userPreferences , /*packageNameOnly*/ undefined , options . overrideImportMode ) ) ||
269
- getLocalModuleSpecifier ( toFileName , info , compilerOptions , host , options . overrideImportMode || importingSourceFile . impliedNodeFormat , preferences ) ;
272
+ getLocalModuleSpecifier ( toFileName , info , compilerOptions , host , options . overrideImportMode || getDefaultResolutionModeForFile ( importingSourceFile , compilerOptions ) , preferences ) ;
270
273
}
271
274
272
275
/** @internal */
@@ -388,7 +391,11 @@ function computeModuleSpecifiers(
388
391
if ( reason . kind !== FileIncludeKind . Import || reason . file !== importingSourceFile . path ) return undefined ;
389
392
// If the candidate import mode doesn't match the mode we're generating for, don't consider it
390
393
// TODO: maybe useful to keep around as an alternative option for certain contexts where the mode is overridable
391
- if ( importingSourceFile . impliedNodeFormat && importingSourceFile . impliedNodeFormat !== getModeForResolutionAtIndex ( importingSourceFile , reason . index , compilerOptions ) ) return undefined ;
394
+ const existingMode = getModeForResolutionAtIndex ( importingSourceFile , reason . index , compilerOptions ) ;
395
+ const targetMode = options . overrideImportMode ?? getDefaultResolutionModeForFile ( importingSourceFile , compilerOptions ) ;
396
+ if ( existingMode !== targetMode && existingMode !== undefined && targetMode !== undefined ) {
397
+ return undefined ;
398
+ }
392
399
const specifier = getModuleNameStringLiteralAt ( importingSourceFile , reason . index ) . text ;
393
400
// If the preference is for non relative and the module specifier is relative, ignore it
394
401
return preferences . relativePreference !== RelativePreference . NonRelative || ! pathIsRelative ( specifier ) ?
@@ -1078,7 +1085,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
1078
1085
const cachedPackageJson = host . getPackageJsonInfoCache ?.( ) ?. getPackageJsonInfo ( packageJsonPath ) ;
1079
1086
if ( isPackageJsonInfo ( cachedPackageJson ) || cachedPackageJson === undefined && host . fileExists ( packageJsonPath ) ) {
1080
1087
const packageJsonContent : Record < string , any > | undefined = cachedPackageJson ?. contents . packageJsonContent || tryParseJson ( host . readFile ! ( packageJsonPath ) ! ) ;
1081
- const importMode = overrideMode || importingSourceFile . impliedNodeFormat ;
1088
+ const importMode = overrideMode || getDefaultResolutionModeForFile ( importingSourceFile , options ) ;
1082
1089
if ( getResolvePackageJsonExports ( options ) ) {
1083
1090
// The package name that we found in node_modules could be different from the package
1084
1091
// name in the package.json content via url/filepath dependency specifiers. We need to
0 commit comments