From 7aff3641a10fc9b0abfa49689dd2fa4462083e5f Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Tue, 28 Aug 2018 16:40:30 -0700 Subject: [PATCH] fix(bazel): only lookup amd module-name tags in .d.ts files (#25710) PR Close #25710 --- packages/bazel/src/ngc-wrapped/index.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/bazel/src/ngc-wrapped/index.ts b/packages/bazel/src/ngc-wrapped/index.ts index 77ef56ebce..ec3b82a7eb 100644 --- a/packages/bazel/src/ngc-wrapped/index.ts +++ b/packages/bazel/src/ngc-wrapped/index.ts @@ -222,14 +222,21 @@ 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; + // Lookup the module name specified in the TypeScript source file, if present + // it will look like /// + // For performance, we only do this for .d.ts files, to avoid parsing lots of + // additional files that were not in the program. + // (We don't have the ts.Program available in this codepath) + if (importedFilePath.endsWith('.d.ts')) { + 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. } - } 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) {