@@ -57,6 +57,7 @@ function assert<T>(condition: T | null | undefined) {
57
57
export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts . CompilerHost , AotCompilerHost ,
58
58
TypeCheckHost {
59
59
private metadataReaderCache = createMetadataReaderCache ( ) ;
60
+ private fileNameToModuleNameCache = new Map < string , string > ( ) ;
60
61
private flatModuleIndexCache = new Map < string , boolean > ( ) ;
61
62
private flatModuleIndexNames = new Set < string > ( ) ;
62
63
private flatModuleIndexRedirectNames = new Set < string > ( ) ;
@@ -187,6 +188,12 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
187
188
* import project sources.
188
189
*/
189
190
fileNameToModuleName ( importedFile : string , containingFile : string ) : string {
191
+ const cacheKey = `${ importedFile } :${ containingFile } ` ;
192
+ let moduleName = this . fileNameToModuleNameCache . get ( cacheKey ) ;
193
+ if ( moduleName != null ) {
194
+ return moduleName ;
195
+ }
196
+
190
197
const originalImportedFile = importedFile ;
191
198
if ( this . options . traceResolution ) {
192
199
console . error (
@@ -196,11 +203,10 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
196
203
197
204
// drop extension
198
205
importedFile = importedFile . replace ( EXT , '' ) ;
199
- const importedFilePackagName = getPackageName ( importedFile ) ;
206
+ const importedFilePackageName = getPackageName ( importedFile ) ;
200
207
const containingFilePackageName = getPackageName ( containingFile ) ;
201
208
202
- let moduleName : string ;
203
- if ( importedFilePackagName === containingFilePackageName ||
209
+ if ( importedFilePackageName === containingFilePackageName ||
204
210
GENERATED_FILES . test ( originalImportedFile ) ) {
205
211
const rootedContainingFile = relativeToRootDirs ( containingFile , this . rootDirs ) ;
206
212
const rootedImportedFile = relativeToRootDirs ( importedFile , this . rootDirs ) ;
@@ -211,12 +217,31 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
211
217
importedFile = rootedImportedFile ;
212
218
}
213
219
moduleName = dotRelative ( path . dirname ( containingFile ) , importedFile ) ;
214
- } else if ( importedFilePackagName ) {
220
+ } else if ( importedFilePackageName ) {
215
221
moduleName = stripNodeModulesPrefix ( importedFile ) ;
222
+ if ( originalImportedFile . endsWith ( '.d.ts' ) ) {
223
+ // the moduleName for these typings could be shortented to the npm package name
224
+ // if the npm package typings matches the importedFile
225
+ try {
226
+ const modulePath = importedFile . substring ( 0 , importedFile . length - moduleName . length ) +
227
+ importedFilePackageName ;
228
+ const packageJson = require ( modulePath + '/package.json' ) ;
229
+ const packageTypings = path . posix . join ( modulePath , packageJson . typings ) ;
230
+ if ( packageTypings === originalImportedFile ) {
231
+ moduleName = importedFilePackageName ;
232
+ }
233
+ } catch ( e ) {
234
+ // the above require() will throw if there is no package.json file
235
+ // and this is safe to ignore and correct to keep the longer
236
+ // moduleName in this case
237
+ }
238
+ }
216
239
} else {
217
240
throw new Error (
218
241
`Trying to import a source file from a node_modules package: import ${ originalImportedFile } from ${ containingFile } ` ) ;
219
242
}
243
+
244
+ this . fileNameToModuleNameCache . set ( cacheKey , moduleName ) ;
220
245
return moduleName ;
221
246
}
222
247
0 commit comments