fix(ivy): ngcc - handle pathMappings to files rather then directories (#30525)

Paths can be mapped directly to files, which was not being taken
into account when computing `basePaths` for the `EntryPointFinder`s.

Now if a `pathMapping` pattern does not exist or is a file, then we try
the containing folder instead.

Fixes #31424

PR Close #30525
This commit is contained in:
Pete Bacon Darwin 2019-07-06 22:24:36 +01:00 committed by Jason Aden
parent a581773887
commit 207f9b6017
3 changed files with 62 additions and 4 deletions

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AbsoluteFsPath, join, resolve} from '../../../src/ngtsc/file_system';
import {AbsoluteFsPath, getFileSystem, join, resolve} from '../../../src/ngtsc/file_system';
import {PathMappings} from '../utils';
/**
@ -29,11 +29,17 @@ import {PathMappings} from '../utils';
*/
export function getBasePaths(
sourceDirectory: AbsoluteFsPath, pathMappings: PathMappings | undefined): AbsoluteFsPath[] {
const basePaths = [sourceDirectory];
const fs = getFileSystem();
let basePaths = [sourceDirectory];
if (pathMappings) {
const baseUrl = resolve(pathMappings.baseUrl);
Object.values(pathMappings.paths).forEach(paths => paths.forEach(path => {
basePaths.push(join(baseUrl, extractPathPrefix(path)));
// We only want base paths that exist and are not files
let basePath = join(baseUrl, extractPathPrefix(path));
while (basePath !== baseUrl && (!fs.exists(basePath) || fs.stat(basePath).isFile())) {
basePath = fs.dirname(basePath);
}
basePaths.push(basePath);
}));
}
basePaths.sort(); // Get the paths in order with the shorter ones first.

View File

@ -166,6 +166,30 @@ runInEachFileSystem(() => {
]);
});
it('should handle pathMappings that map to files or non-existent directories', () => {
const basePath = _Abs('/path_mapped/node_modules');
const pathMappings: PathMappings = {
baseUrl: '/path_mapped/dist',
paths: {
'@test': ['pkg2/fesm2015/pkg2.js'],
'@missing': ['pkg3'],
}
};
loadTestFiles([
...createPackage(_Abs('/path_mapped/node_modules'), 'test', []),
...createPackage(_Abs('/path_mapped/dist'), 'pkg2'),
]);
resolver = new DependencyResolver(
fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))});
const finder = new DirectoryWalkerEntryPointFinder(
fs, config, logger, resolver, basePath, pathMappings);
const {entryPoints} = finder.findEntryPoints();
expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([
['../dist/pkg2', '../dist/pkg2'],
['test', 'test'],
]);
});
function createPackage(
basePath: AbsoluteFsPath, packageName: string, deps: string[] = []): TestFile[] {
return [

View File

@ -146,15 +146,18 @@ runInEachFileSystem(() => {
paths: {
'@x/*': ['*'],
'@y/*/test': ['lib/*/test'],
'@z/*': ['../dist/moo/../*'],
}
};
loadTestFiles([
...createPackage(
_Abs('/path_mapped/node_modules'), 'test', ['pkg1', '@x/pkg2', '@y/pkg3/test']),
_Abs('/path_mapped/node_modules'), 'test',
['pkg1', '@x/pkg2', '@y/pkg3/test', '@z/pkg5']),
...createPackage(_Abs('/path_mapped/node_modules'), 'pkg1'),
...createPackage(_Abs('/path_mapped/dist'), 'pkg2', ['pkg4']),
...createPackage(_Abs('/path_mapped/dist/pkg2/node_modules'), 'pkg4'),
...createPackage(_Abs('/path_mapped/dist/lib/pkg3'), 'test'),
...createPackage(_Abs('/path_mapped/dist'), 'pkg5'),
]);
resolver = new DependencyResolver(
fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))});
@ -166,6 +169,31 @@ runInEachFileSystem(() => {
['../dist/pkg2/node_modules/pkg4', '../dist/pkg2/node_modules/pkg4'],
['../dist/pkg2', '../dist/pkg2'],
['../dist/lib/pkg3/test', '../dist/lib/pkg3/test'],
['../dist/pkg5', '../dist/pkg5'],
['test', 'test'],
]);
});
it('should handle pathMappings that map to files or non-existent directories', () => {
const basePath = _Abs('/path_mapped/node_modules');
const targetPath = _Abs('/path_mapped/node_modules/test');
const pathMappings: PathMappings = {
baseUrl: '/path_mapped/dist',
paths: {
'@test': ['pkg2/fesm2015/pkg2.js'],
'@missing': ['pkg3'],
}
};
loadTestFiles([
...createPackage(_Abs('/path_mapped/node_modules'), 'test', []),
...createPackage(_Abs('/path_mapped/dist'), 'pkg2'),
]);
resolver = new DependencyResolver(
fs, logger, {esm2015: new EsmDependencyHost(fs, new ModuleResolver(fs, pathMappings))});
const finder = new TargetedEntryPointFinder(
fs, config, logger, resolver, basePath, targetPath, pathMappings);
const {entryPoints} = finder.findEntryPoints();
expect(dumpEntryPointPaths(basePath, entryPoints)).toEqual([
['test', 'test'],
]);
});