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');
}

View File

@ -43,6 +43,11 @@ runInEachFileSystem(() => {
name: _('/dist/package-4/sub-folder/index.js'),
contents: `import {X} from '@shared/package-4/x';`
},
{name: _('/dist/package-4/secondary-entry-point/x.js'), contents: `export class X {}`},
{
name: _('/dist/package-4/secondary-entry-point/package.json'),
contents: 'PACKAGE.JSON for secondary-entry-point'
},
{
name: _('/dist/sub-folder/package-4/package.json'),
contents: 'PACKAGE.JSON for package-4'
@ -199,6 +204,39 @@ runInEachFileSystem(() => {
_('/dist/package-4/sub-folder/index.js')))
.toEqual(new ResolvedExternalModule(_('/dist/sub-folder/package-5/post-fix')));
});
it('should resolve primary entry-points if they match non-wildcards exactly', () => {
const resolver = new ModuleResolver(
getFileSystem(), {baseUrl: '/dist', paths: {'package-4': ['package-4']}});
expect(resolver.resolveModuleImport('package-4', _('/libs/local-package/index.js')))
.toEqual(new ResolvedExternalModule(_('/dist/package-4')));
expect(resolver.resolveModuleImport(
'package-4/secondary-entry-point', _('/libs/local-package/index.js')))
.toEqual(null);
});
it('should resolve secondary entry-points if wildcards match', () => {
const resolver = new ModuleResolver(getFileSystem(), {
baseUrl: '/dist',
paths: {'package-4': ['package-4'], 'package-4/*': ['package-4/*']}
});
expect(resolver.resolveModuleImport('package-4', _('/libs/local-package/index.js')))
.toEqual(new ResolvedExternalModule(_('/dist/package-4')));
expect(resolver.resolveModuleImport(
'package-4/secondary-entry-point', _('/libs/local-package/index.js')))
.toEqual(new ResolvedExternalModule(_('/dist/package-4/secondary-entry-point')));
});
it('should resolve secondary-entry-points referenced from their primary entry-point',
() => {
const resolver = new ModuleResolver(getFileSystem(), {
baseUrl: '/dist',
paths: {'package-4': ['package-4'], 'package-4/*': ['package-4/*']}
});
expect(resolver.resolveModuleImport(
'package-4/secondary-entry-point', _('/dist/package-4/index.js')))
.toEqual(new ResolvedExternalModule(_('/dist/package-4/secondary-entry-point')));
});
});
});
});