@@ -9,8 +9,9 @@ import {AbsoluteFsPath, FileSystem, join, resolve} from '../../../src/ngtsc/file
9
9
import { DependencyResolver , SortedEntryPointsInfo } from '../dependencies/dependency_resolver' ;
10
10
import { Logger } from '../logging/logger' ;
11
11
import { NgccConfiguration } from '../packages/configuration' ;
12
- import { EntryPoint , getEntryPointInfo } from '../packages/entry_point' ;
12
+ import { EntryPoint , INVALID_ENTRY_POINT , NO_ENTRY_POINT , getEntryPointInfo } from '../packages/entry_point' ;
13
13
import { PathMappings } from '../utils' ;
14
+ import { NGCC_DIRECTORY } from '../writing/new_entry_point_file_writer' ;
14
15
import { EntryPointFinder } from './interface' ;
15
16
import { getBasePaths } from './utils' ;
16
17
@@ -40,10 +41,24 @@ export class DirectoryWalkerEntryPointFinder implements EntryPointFinder {
40
41
* The function will recurse into directories that start with `@...`, e.g. `@angular/...`.
41
42
* @param sourceDirectory An absolute path to the root directory where searching begins.
42
43
*/
43
- private walkDirectoryForEntryPoints ( sourceDirectory : AbsoluteFsPath ) : EntryPoint [ ] {
44
+ walkDirectoryForEntryPoints ( sourceDirectory : AbsoluteFsPath ) : EntryPoint [ ] {
44
45
const entryPoints = this . getEntryPointsForPackage ( sourceDirectory ) ;
46
+ if ( entryPoints === null ) {
47
+ return [ ] ;
48
+ }
49
+
45
50
if ( entryPoints . length > 0 ) {
46
- // The `sourceDirectory` is an entry-point itself so no need to search its sub-directories.
51
+ // The `sourceDirectory` is an entry point itself so no need to search its sub-directories.
52
+ // Also check for any nested node_modules in this package but only if it was compiled by
53
+ // Angular.
54
+ // It is unlikely that a non Angular entry point has a dependency on an Angular library.
55
+ if ( entryPoints . some ( e => e . compiledByAngular ) ) {
56
+ const nestedNodeModulesPath = this . fs . join ( sourceDirectory , 'node_modules' ) ;
57
+ if ( this . fs . exists ( nestedNodeModulesPath ) ) {
58
+ entryPoints . push ( ...this . walkDirectoryForEntryPoints ( nestedNodeModulesPath ) ) ;
59
+ }
60
+ }
61
+
47
62
return entryPoints ;
48
63
}
49
64
@@ -52,54 +67,57 @@ export class DirectoryWalkerEntryPointFinder implements EntryPointFinder {
52
67
// Not interested in hidden files
53
68
. filter ( p => ! p . startsWith ( '.' ) )
54
69
// Ignore node_modules
55
- . filter ( p => p !== 'node_modules' )
70
+ . filter ( p => p !== 'node_modules' && p !== NGCC_DIRECTORY )
56
71
// Only interested in directories (and only those that are not symlinks)
57
72
. filter ( p => {
58
73
const stat = this . fs . lstat ( resolve ( sourceDirectory , p ) ) ;
59
74
return stat . isDirectory ( ) && ! stat . isSymbolicLink ( ) ;
60
75
} )
61
76
. forEach ( p => {
62
- // Either the directory is a potential package or a namespace containing packages (e.g
63
- // `@angular`).
77
+ // Package is a potential namespace containing packages (e.g `@angular`).
64
78
const packagePath = join ( sourceDirectory , p ) ;
65
79
entryPoints . push ( ...this . walkDirectoryForEntryPoints ( packagePath ) ) ;
66
-
67
- // Also check for any nested node_modules in this package
68
- const nestedNodeModulesPath = join ( packagePath , 'node_modules' ) ;
69
- if ( this . fs . exists ( nestedNodeModulesPath ) ) {
70
- entryPoints . push ( ...this . walkDirectoryForEntryPoints ( nestedNodeModulesPath ) ) ;
71
- }
72
80
} ) ;
73
81
return entryPoints ;
74
82
}
75
83
76
84
/**
77
85
* Recurse the folder structure looking for all the entry points
78
86
* @param packagePath The absolute path to an npm package that may contain entry points
79
- * @returns An array of entry points that were discovered.
87
+ * @returns An array of entry points that were discovered or null when it's not a valid entrypoint
80
88
*/
81
- private getEntryPointsForPackage ( packagePath : AbsoluteFsPath ) : EntryPoint [ ] {
89
+ private getEntryPointsForPackage ( packagePath : AbsoluteFsPath ) : EntryPoint [ ] | null {
82
90
const entryPoints : EntryPoint [ ] = [ ] ;
83
91
84
92
// Try to get an entry point from the top level package directory
85
93
const topLevelEntryPoint =
86
94
getEntryPointInfo ( this . fs , this . config , this . logger , packagePath , packagePath ) ;
87
95
88
96
// If there is no primary entry-point then exit
89
- if ( topLevelEntryPoint === null ) {
97
+ if ( topLevelEntryPoint === NO_ENTRY_POINT ) {
90
98
return [ ] ;
91
99
}
92
100
101
+ if ( topLevelEntryPoint === INVALID_ENTRY_POINT ) {
102
+ return null ;
103
+ }
104
+
93
105
// Otherwise store it and search for secondary entry-points
94
106
entryPoints . push ( topLevelEntryPoint ) ;
95
107
this . walkDirectory ( packagePath , packagePath , ( path , isDirectory ) => {
108
+ if ( ! path . endsWith ( '.js' ) && ! isDirectory ) {
109
+ return false ;
110
+ }
111
+
96
112
// If the path is a JS file then strip its extension and see if we can match an entry-point.
97
113
const possibleEntryPointPath = isDirectory ? path : stripJsExtension ( path ) ;
98
114
const subEntryPoint =
99
115
getEntryPointInfo ( this . fs , this . config , this . logger , packagePath , possibleEntryPointPath ) ;
100
- if ( subEntryPoint !== null ) {
101
- entryPoints . push ( subEntryPoint ) ;
116
+ if ( subEntryPoint === NO_ENTRY_POINT || subEntryPoint === INVALID_ENTRY_POINT ) {
117
+ return false ;
102
118
}
119
+ entryPoints . push ( subEntryPoint ) ;
120
+ return true ;
103
121
} ) ;
104
122
105
123
return entryPoints ;
@@ -113,26 +131,25 @@ export class DirectoryWalkerEntryPointFinder implements EntryPointFinder {
113
131
*/
114
132
private walkDirectory (
115
133
packagePath : AbsoluteFsPath , dir : AbsoluteFsPath ,
116
- fn : ( path : AbsoluteFsPath , isDirectory : boolean ) => void ) {
134
+ fn : ( path : AbsoluteFsPath , isDirectory : boolean ) => boolean ) {
117
135
return this . fs
118
136
. readdir ( dir )
119
137
// Not interested in hidden files
120
138
. filter ( path => ! path . startsWith ( '.' ) )
121
139
// Ignore node_modules
122
- . filter ( path => path !== 'node_modules' )
123
- . map ( path => resolve ( dir , path ) )
140
+ . filter ( path => path !== 'node_modules' && path !== NGCC_DIRECTORY )
124
141
. forEach ( path => {
125
- const stat = this . fs . lstat ( path ) ;
142
+ const absolutePath = resolve ( dir , path ) ;
143
+ const stat = this . fs . lstat ( absolutePath ) ;
126
144
127
145
if ( stat . isSymbolicLink ( ) ) {
128
146
// We are not interested in symbolic links
129
147
return ;
130
148
}
131
149
132
- fn ( path , stat . isDirectory ( ) ) ;
133
-
134
- if ( stat . isDirectory ( ) ) {
135
- this . walkDirectory ( packagePath , path , fn ) ;
150
+ const containsEntryPoint = fn ( absolutePath , stat . isDirectory ( ) ) ;
151
+ if ( containsEntryPoint ) {
152
+ this . walkDirectory ( packagePath , absolutePath , fn ) ;
136
153
}
137
154
} ) ;
138
155
}
0 commit comments