diff --git a/packages/bazel/src/ngc-wrapped/index.ts b/packages/bazel/src/ngc-wrapped/index.ts index aeed9b07a6..77ef56ebce 100644 --- a/packages/bazel/src/ngc-wrapped/index.ts +++ b/packages/bazel/src/ngc-wrapped/index.ts @@ -222,6 +222,15 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true, const ngHost = ng.createCompilerHost({options: compilerOpts, tsHost: bazelHost}); ngHost.fileNameToModuleName = (importedFilePath: string, containingFilePath: string) => { + try { + const sourceFile = ngHost.getSourceFile(importedFilePath, ts.ScriptTarget.Latest); + if (sourceFile && sourceFile.moduleName) { + return sourceFile.moduleName; + } + } catch (err) { + // File does not exist or parse error. Ignore this case and continue onto the + // other methods of resolving the module below. + } if ((compilerOpts.module === ts.ModuleKind.UMD || compilerOpts.module === ts.ModuleKind.AMD) && ngHost.amdModuleName) { return ngHost.amdModuleName({ fileName: importedFilePath } as ts.SourceFile);