Skip to content

Commit 584c0f5

Browse files
fix(ivy): ngcc - capture entry-points in top-level path-mapped folders
The `EntryPointFinder` computes the base paths to consider when searching for entry-points. When there are `pathMappings` provided it works out the best top level base-paths that cover all the potential mappings. If this computed basePath happens to coincide with an entry-point path itself then we were missing it. Now we check for an entry-point even at the base-path itself. Related to angular/angular-cli#14755
1 parent 82e0b4a commit 584c0f5

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

packages/compiler-cli/ngcc/src/packages/entry_point_finder.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ export class EntryPointFinder {
7070
* @param sourceDirectory An absolute path to the root directory where searching begins.
7171
*/
7272
private walkDirectoryForEntryPoints(sourceDirectory: AbsoluteFsPath): EntryPoint[] {
73-
const entryPoints: EntryPoint[] = [];
73+
const entryPoints = this.getEntryPointsForPackage(sourceDirectory);
74+
if (entryPoints.length > 0) {
75+
// The `sourceDirectory` is an entry-point itself so no need to search its sub-directories.
76+
return entryPoints;
77+
}
78+
7479
this.fs
7580
.readdir(sourceDirectory)
7681
// Not interested in hidden files
@@ -86,16 +91,12 @@ export class EntryPointFinder {
8691
// Either the directory is a potential package or a namespace containing packages (e.g
8792
// `@angular`).
8893
const packagePath = AbsoluteFsPath.join(sourceDirectory, p);
89-
if (p.startsWith('@')) {
90-
entryPoints.push(...this.walkDirectoryForEntryPoints(packagePath));
91-
} else {
92-
entryPoints.push(...this.getEntryPointsForPackage(packagePath));
94+
entryPoints.push(...this.walkDirectoryForEntryPoints(packagePath));
9395

94-
// Also check for any nested node_modules in this package
95-
const nestedNodeModulesPath = AbsoluteFsPath.join(packagePath, 'node_modules');
96-
if (this.fs.exists(nestedNodeModulesPath)) {
97-
entryPoints.push(...this.walkDirectoryForEntryPoints(nestedNodeModulesPath));
98-
}
96+
// Also check for any nested node_modules in this package
97+
const nestedNodeModulesPath = AbsoluteFsPath.join(packagePath, 'node_modules');
98+
if (this.fs.exists(nestedNodeModulesPath)) {
99+
entryPoints.push(...this.walkDirectoryForEntryPoints(nestedNodeModulesPath));
99100
}
100101
});
101102
return entryPoints;
@@ -111,11 +112,14 @@ export class EntryPointFinder {
111112

112113
// Try to get an entry point from the top level package directory
113114
const topLevelEntryPoint = getEntryPointInfo(this.fs, this.logger, packagePath, packagePath);
114-
if (topLevelEntryPoint !== null) {
115-
entryPoints.push(topLevelEntryPoint);
115+
116+
// If there is no primary entry-point then exit
117+
if (topLevelEntryPoint === null) {
118+
return [];
116119
}
117120

118-
// Now search all the directories of this package for possible entry points
121+
// Otherwise store it and search for secondary entry-points
122+
entryPoints.push(topLevelEntryPoint);
119123
this.walkDirectory(packagePath, subdir => {
120124
const subEntryPoint = getEntryPointInfo(this.fs, this.logger, packagePath, subdir);
121125
if (subEntryPoint !== null) {

packages/compiler-cli/ngcc/test/packages/entry_point_finder_spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ describe('findEntryPoints()', () => {
5252
]);
5353
});
5454

55+
it('should find entry-points via `pathMappings', () => {
56+
const {entryPoints} = finder.findEntryPoints(
57+
_('/pathMappings/node_modules'), undefined,
58+
{baseUrl: _('/pathMappings'), paths: {'my-lib': ['dist/my-lib']}});
59+
const entryPointPaths = entryPoints.map(x => [x.package, x.path]);
60+
expect(entryPointPaths).toEqual([
61+
[_('/pathMappings/dist/my-lib'), _('/pathMappings/dist/my-lib')],
62+
[_('/pathMappings/dist/my-lib'), _('/pathMappings/dist/my-lib/sub-lib')],
63+
[
64+
_('/pathMappings/node_modules/@angular/common'),
65+
_('/pathMappings/node_modules/@angular/common')
66+
],
67+
]);
68+
});
69+
5570
it('should return an empty array if there are no packages', () => {
5671
const {entryPoints} = finder.findEntryPoints(_('/no_packages'));
5772
expect(entryPoints).toEqual([]);
@@ -105,6 +120,26 @@ describe('findEntryPoints()', () => {
105120
},
106121
},
107122
},
123+
'/pathMappings': {
124+
'dist': {
125+
'my-lib': {
126+
'package.json': createPackageJson('my-lib'),
127+
'my-lib.metadata.json': 'metadata info',
128+
'sub-lib': {
129+
'package.json': createPackageJson('sub-lib'),
130+
'sub-lib.metadata.json': 'metadata info',
131+
},
132+
},
133+
},
134+
'node_modules': {
135+
'@angular': {
136+
'common': {
137+
'package.json': createPackageJson('common'),
138+
'common.metadata.json': 'metadata info',
139+
},
140+
},
141+
}
142+
},
108143
'/namespaced': {
109144
'@angular': {
110145
'common': {

0 commit comments

Comments
 (0)