@@ -311,6 +311,31 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost,
311
311
// File does not exist or parse error. Ignore this case and continue onto the
312
312
// other methods of resolving the module below.
313
313
}
314
+
315
+ // It can happen that the ViewEngine compiler needs to write an import in a factory file,
316
+ // and is using an ngsummary file to get the symbols.
317
+ // The ngsummary comes from an upstream ng_module rule.
318
+ // The upstream rule based its imports on ngsummary file which was generated from a
319
+ // metadata.json file that was published to npm in an Angular library.
320
+ // However, the ngsummary doesn't propagate the 'importAs' from the original metadata.json
321
+ // so we would normally not be able to supply the correct module name for it.
322
+ // For example, if the rootDir-relative filePath is
323
+ // node_modules/@angular /material/toolbar/typings/index
324
+ // we would supply a module name
325
+ // @angular /material/toolbar/typings/index
326
+ // but there is no JavaScript file to load at this path.
327
+ // This is a workaround for https://github.com/angular/angular/issues/29454
328
+ if ( importedFilePath . indexOf ( 'node_modules' ) >= 0 ) {
329
+ const maybeMetadataFile = importedFilePath . replace ( EXT , '' ) + '.metadata.json' ;
330
+ if ( fs . existsSync ( maybeMetadataFile ) ) {
331
+ const moduleName =
332
+ JSON . parse ( fs . readFileSync ( maybeMetadataFile , { encoding : 'utf-8' } ) ) . importAs ;
333
+ if ( moduleName ) {
334
+ return moduleName ;
335
+ }
336
+ }
337
+ }
338
+
314
339
if ( ( compilerOpts . module === ts . ModuleKind . UMD || compilerOpts . module === ts . ModuleKind . AMD ) &&
315
340
ngHost . amdModuleName ) {
316
341
return ngHost . amdModuleName ( { fileName : importedFilePath } as ts . SourceFile ) ;
0 commit comments