Skip to content

Commit 6199ea5

Browse files
gregmagolanIgorMinar
authored andcommitted
fix(compiler-cli): shorten resolved module name in fileNameToModuleName to npm package name for typings (#23231)
PR Close #23231
1 parent 2e270bb commit 6199ea5

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

integration/bazel/BUILD.bazel

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ filegroup(
44
name = "node_modules",
55
srcs = glob(
66
["node_modules/**/*"],
7-
# Exclude directories that commonly contain filenames which are
8-
# illegal bazel labels
97
exclude = [
8+
# Exclude rxjs because we build it from sources using the label @rxjs//:rxjs
9+
"node_modules/rxjs/**",
10+
11+
# Exclude directories that commonly contain filenames which are
12+
# illegal bazel labels
1013
# e.g. node_modules/adm-zip/test/assets/attributes_test/New folder/hidden.txt
1114
"node_modules/**/test/**",
1215
# e.g. node_modules/xpath/docs/function resolvers.md

packages/compiler-cli/src/transformers/compiler_host.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function assert<T>(condition: T | null | undefined) {
5757
export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHost, AotCompilerHost,
5858
TypeCheckHost {
5959
private metadataReaderCache = createMetadataReaderCache();
60+
private fileNameToModuleNameCache = new Map<string, string>();
6061
private flatModuleIndexCache = new Map<string, boolean>();
6162
private flatModuleIndexNames = new Set<string>();
6263
private flatModuleIndexRedirectNames = new Set<string>();
@@ -187,6 +188,12 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
187188
* import project sources.
188189
*/
189190
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+
190197
const originalImportedFile = importedFile;
191198
if (this.options.traceResolution) {
192199
console.error(
@@ -196,11 +203,10 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
196203

197204
// drop extension
198205
importedFile = importedFile.replace(EXT, '');
199-
const importedFilePackagName = getPackageName(importedFile);
206+
const importedFilePackageName = getPackageName(importedFile);
200207
const containingFilePackageName = getPackageName(containingFile);
201208

202-
let moduleName: string;
203-
if (importedFilePackagName === containingFilePackageName ||
209+
if (importedFilePackageName === containingFilePackageName ||
204210
GENERATED_FILES.test(originalImportedFile)) {
205211
const rootedContainingFile = relativeToRootDirs(containingFile, this.rootDirs);
206212
const rootedImportedFile = relativeToRootDirs(importedFile, this.rootDirs);
@@ -211,12 +217,31 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
211217
importedFile = rootedImportedFile;
212218
}
213219
moduleName = dotRelative(path.dirname(containingFile), importedFile);
214-
} else if (importedFilePackagName) {
220+
} else if (importedFilePackageName) {
215221
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+
}
216239
} else {
217240
throw new Error(
218241
`Trying to import a source file from a node_modules package: import ${originalImportedFile} from ${containingFile}`);
219242
}
243+
244+
this.fileNameToModuleNameCache.set(cacheKey, moduleName);
220245
return moduleName;
221246
}
222247

0 commit comments

Comments
 (0)