@@ -235,7 +235,8 @@ namespace ts.moduleSpecifiers {
235
235
const suffix = pattern . substr ( indexOfStar + 1 ) ;
236
236
if ( relativeToBaseUrl . length >= prefix . length + suffix . length &&
237
237
startsWith ( relativeToBaseUrl , prefix ) &&
238
- endsWith ( relativeToBaseUrl , suffix ) ) {
238
+ endsWith ( relativeToBaseUrl , suffix ) ||
239
+ ! suffix && relativeToBaseUrl === removeTrailingDirectorySeparator ( prefix ) ) {
239
240
const matchedStar = relativeToBaseUrl . substr ( prefix . length , relativeToBaseUrl . length - suffix . length ) ;
240
241
return key . replace ( "*" , matchedStar ) ;
241
242
}
@@ -264,6 +265,26 @@ namespace ts.moduleSpecifiers {
264
265
return undefined ;
265
266
}
266
267
268
+ const packageRootPath = moduleFileName . substring ( 0 , parts . packageRootIndex ) ;
269
+ const packageJsonPath = combinePaths ( packageRootPath , "package.json" ) ;
270
+ const packageJsonContent = host . fileExists ! ( packageJsonPath )
271
+ ? JSON . parse ( host . readFile ! ( packageJsonPath ) ! )
272
+ : undefined ;
273
+ const versionPaths = packageJsonContent && packageJsonContent . typesVersions
274
+ ? getPackageJsonTypesVersionsPaths ( packageJsonContent . typesVersions )
275
+ : undefined ;
276
+ if ( versionPaths ) {
277
+ const subModuleName = moduleFileName . slice ( parts . packageRootIndex + 1 ) ;
278
+ const fromPaths = tryGetModuleNameFromPaths (
279
+ removeFileExtension ( subModuleName ) ,
280
+ removeExtensionAndIndexPostFix ( subModuleName , Ending . Minimal , options ) ,
281
+ versionPaths . paths
282
+ ) ;
283
+ if ( fromPaths !== undefined ) {
284
+ moduleFileName = combinePaths ( moduleFileName . slice ( 0 , parts . packageRootIndex ) , fromPaths ) ;
285
+ }
286
+ }
287
+
267
288
// Simplify the full file path to something that can be resolved by Node.
268
289
269
290
// If the module could be imported by a directory name, use that directory's name
@@ -274,23 +295,18 @@ namespace ts.moduleSpecifiers {
274
295
275
296
// If the module was found in @types , get the actual Node package name
276
297
const nodeModulesDirectoryName = moduleSpecifier . substring ( parts . topLevelPackageNameIndex + 1 ) ;
277
- const packageName = getPackageNameFromAtTypesDirectory ( nodeModulesDirectoryName ) ;
298
+ const packageName = getPackageNameFromTypesPackageName ( nodeModulesDirectoryName ) ;
278
299
// For classic resolution, only allow importing from node_modules/@types, not other node_modules
279
300
return getEmitModuleResolutionKind ( options ) !== ModuleResolutionKind . NodeJs && packageName === nodeModulesDirectoryName ? undefined : packageName ;
280
301
281
302
function getDirectoryOrExtensionlessFileName ( path : string ) : string {
282
303
// If the file is the main module, it can be imported by the package name
283
- const packageRootPath = path . substring ( 0 , parts . packageRootIndex ) ;
284
- const packageJsonPath = combinePaths ( packageRootPath , "package.json" ) ;
285
- if ( host . fileExists ! ( packageJsonPath ) ) { // TODO: GH#18217
286
- const packageJsonContent = JSON . parse ( host . readFile ! ( packageJsonPath ) ! ) ;
287
- if ( packageJsonContent ) {
288
- const mainFileRelative = packageJsonContent . typings || packageJsonContent . types || packageJsonContent . main ;
289
- if ( mainFileRelative ) {
290
- const mainExportFile = toPath ( mainFileRelative , packageRootPath , getCanonicalFileName ) ;
291
- if ( removeFileExtension ( mainExportFile ) === removeFileExtension ( getCanonicalFileName ( path ) ) ) {
292
- return packageRootPath ;
293
- }
304
+ if ( packageJsonContent ) {
305
+ const mainFileRelative = packageJsonContent . typings || packageJsonContent . types || packageJsonContent . main ;
306
+ if ( mainFileRelative ) {
307
+ const mainExportFile = toPath ( mainFileRelative , packageRootPath , getCanonicalFileName ) ;
308
+ if ( removeFileExtension ( mainExportFile ) === removeFileExtension ( getCanonicalFileName ( path ) ) ) {
309
+ return packageRootPath ;
294
310
}
295
311
}
296
312
}
0 commit comments