fix(ivy): ngcc - resolve path-mapped modules correctly (#31450)

Non-wild-card path-mappings were not being matched correctly.

Further path-mapped secondary entry-points that
were imported from the associated primary entry-point were not
being martched correctly.

Fixes #31274

PR Close #31450
This commit is contained in:
Pete Bacon Darwin
2019-07-08 12:43:32 +01:00
committed by Jason Aden
parent 9a2d1fab84
commit 50c4ec6687
2 changed files with 57 additions and 11 deletions

View File

@ -94,14 +94,13 @@ export class ModuleResolver {
const packagePath = this.findPackagePath(fromPath);
if (packagePath !== null) {
for (const mappedPath of mappedPaths) {
const isRelative =
mappedPath.startsWith(packagePath) && !mappedPath.includes('node_modules');
if (isRelative) {
return this.resolveAsRelativePath(mappedPath, fromPath);
} else if (this.isEntryPoint(mappedPath)) {
if (this.isEntryPoint(mappedPath)) {
return new ResolvedExternalModule(mappedPath);
} else if (this.resolveAsRelativePath(mappedPath, fromPath)) {
return new ResolvedDeepImport(mappedPath);
}
const nonEntryPointImport = this.resolveAsRelativePath(mappedPath, fromPath);
if (nonEntryPointImport !== null) {
return isRelativeImport(packagePath, mappedPath) ? nonEntryPointImport :
new ResolvedDeepImport(mappedPath);
}
}
}
@ -190,7 +189,9 @@ export class ModuleResolver {
}
}
return (bestMapping && bestMatch) ? this.computeMappedTemplates(bestMapping, bestMatch) : [];
return (bestMapping !== undefined && bestMatch !== undefined) ?
this.computeMappedTemplates(bestMapping, bestMatch) :
[];
}
/**
@ -203,10 +204,13 @@ export class ModuleResolver {
*/
private matchMapping(path: string, mapping: ProcessedPathMapping): string|null {
const {prefix, postfix, hasWildcard} = mapping.matcher;
if (path.startsWith(prefix) && path.endsWith(postfix)) {
return hasWildcard ? path.substring(prefix.length, path.length - postfix.length) : '';
if (hasWildcard) {
return (path.startsWith(prefix) && path.endsWith(postfix)) ?
path.substring(prefix.length, path.length - postfix.length) :
null;
} else {
return (path === prefix) ? '' : null;
}
return null;
}
/**
@ -277,3 +281,7 @@ interface PathMappingPattern {
postfix: string;
hasWildcard: boolean;
}
function isRelativeImport(from: AbsoluteFsPath, to: AbsoluteFsPath) {
return to.startsWith(from) && !to.includes('node_modules');
}