fix(compiler-cli): shorten resolved module name in fileNameToModuleName to npm package name for typings (#23231)

PR Close #23231
This commit is contained in:
Greg Magolan 2018-04-06 11:08:19 -07:00 committed by Igor Minar
parent 2e270bb96a
commit 6199ea5d4a
2 changed files with 34 additions and 6 deletions

View File

@ -4,9 +4,12 @@ filegroup(
name = "node_modules", name = "node_modules",
srcs = glob( srcs = glob(
["node_modules/**/*"], ["node_modules/**/*"],
# Exclude directories that commonly contain filenames which are
# illegal bazel labels
exclude = [ exclude = [
# Exclude rxjs because we build it from sources using the label @rxjs//:rxjs
"node_modules/rxjs/**",
# Exclude directories that commonly contain filenames which are
# illegal bazel labels
# e.g. node_modules/adm-zip/test/assets/attributes_test/New folder/hidden.txt # e.g. node_modules/adm-zip/test/assets/attributes_test/New folder/hidden.txt
"node_modules/**/test/**", "node_modules/**/test/**",
# e.g. node_modules/xpath/docs/function resolvers.md # e.g. node_modules/xpath/docs/function resolvers.md

View File

@ -57,6 +57,7 @@ function assert<T>(condition: T | null | undefined) {
export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHost, AotCompilerHost, export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHost, AotCompilerHost,
TypeCheckHost { TypeCheckHost {
private metadataReaderCache = createMetadataReaderCache(); private metadataReaderCache = createMetadataReaderCache();
private fileNameToModuleNameCache = new Map<string, string>();
private flatModuleIndexCache = new Map<string, boolean>(); private flatModuleIndexCache = new Map<string, boolean>();
private flatModuleIndexNames = new Set<string>(); private flatModuleIndexNames = new Set<string>();
private flatModuleIndexRedirectNames = new Set<string>(); private flatModuleIndexRedirectNames = new Set<string>();
@ -187,6 +188,12 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
* import project sources. * import project sources.
*/ */
fileNameToModuleName(importedFile: string, containingFile: string): string { fileNameToModuleName(importedFile: string, containingFile: string): string {
const cacheKey = `${importedFile}:${containingFile}`;
let moduleName = this.fileNameToModuleNameCache.get(cacheKey);
if (moduleName != null) {
return moduleName;
}
const originalImportedFile = importedFile; const originalImportedFile = importedFile;
if (this.options.traceResolution) { if (this.options.traceResolution) {
console.error( console.error(
@ -196,11 +203,10 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
// drop extension // drop extension
importedFile = importedFile.replace(EXT, ''); importedFile = importedFile.replace(EXT, '');
const importedFilePackagName = getPackageName(importedFile); const importedFilePackageName = getPackageName(importedFile);
const containingFilePackageName = getPackageName(containingFile); const containingFilePackageName = getPackageName(containingFile);
let moduleName: string; if (importedFilePackageName === containingFilePackageName ||
if (importedFilePackagName === containingFilePackageName ||
GENERATED_FILES.test(originalImportedFile)) { GENERATED_FILES.test(originalImportedFile)) {
const rootedContainingFile = relativeToRootDirs(containingFile, this.rootDirs); const rootedContainingFile = relativeToRootDirs(containingFile, this.rootDirs);
const rootedImportedFile = relativeToRootDirs(importedFile, this.rootDirs); const rootedImportedFile = relativeToRootDirs(importedFile, this.rootDirs);
@ -211,12 +217,31 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
importedFile = rootedImportedFile; importedFile = rootedImportedFile;
} }
moduleName = dotRelative(path.dirname(containingFile), importedFile); moduleName = dotRelative(path.dirname(containingFile), importedFile);
} else if (importedFilePackagName) { } else if (importedFilePackageName) {
moduleName = stripNodeModulesPrefix(importedFile); moduleName = stripNodeModulesPrefix(importedFile);
if (originalImportedFile.endsWith('.d.ts')) {
// the moduleName for these typings could be shortented to the npm package name
// if the npm package typings matches the importedFile
try {
const modulePath = importedFile.substring(0, importedFile.length - moduleName.length) +
importedFilePackageName;
const packageJson = require(modulePath + '/package.json');
const packageTypings = path.posix.join(modulePath, packageJson.typings);
if (packageTypings === originalImportedFile) {
moduleName = importedFilePackageName;
}
} catch (e) {
// the above require() will throw if there is no package.json file
// and this is safe to ignore and correct to keep the longer
// moduleName in this case
}
}
} else { } else {
throw new Error( throw new Error(
`Trying to import a source file from a node_modules package: import ${originalImportedFile} from ${containingFile}`); `Trying to import a source file from a node_modules package: import ${originalImportedFile} from ${containingFile}`);
} }
this.fileNameToModuleNameCache.set(cacheKey, moduleName);
return moduleName; return moduleName;
} }