9
9
import { LogLevel , Logger , process as mainNgcc } from '@angular/compiler-cli/ngcc' ;
10
10
import { spawnSync } from 'child_process' ;
11
11
import { createHash } from 'crypto' ;
12
+ import { Resolver , ResolverFactory } from 'enhanced-resolve' ;
12
13
import { accessSync , constants , existsSync , mkdirSync , readFileSync , writeFileSync } from 'fs' ;
13
14
import * as path from 'path' ;
14
15
import * as ts from 'typescript' ;
16
+ import { InputFileSystem } from 'webpack' ;
15
17
import { time , timeEnd } from './benchmark' ;
16
18
17
19
// We cannot create a plugin for this, because NGTSC requires addition type
@@ -28,17 +30,28 @@ export class NgccProcessor {
28
30
private _processedModules = new Set < string > ( ) ;
29
31
private _logger : NgccLogger ;
30
32
private _nodeModulesDirectory : string ;
33
+ private _resolver : Resolver ;
31
34
32
35
constructor (
33
36
private readonly propertiesToConsider : string [ ] ,
34
- private readonly fileWatchPurger : ( path : string ) => void ,
35
37
private readonly compilationWarnings : ( Error | string ) [ ] ,
36
38
private readonly compilationErrors : ( Error | string ) [ ] ,
37
39
private readonly basePath : string ,
38
40
private readonly tsConfigPath : string ,
41
+ private readonly inputFileSystem : InputFileSystem ,
42
+ private readonly symlinks : boolean | undefined ,
39
43
) {
40
44
this . _logger = new NgccLogger ( this . compilationWarnings , this . compilationErrors ) ;
41
45
this . _nodeModulesDirectory = this . findNodeModulesDirectory ( this . basePath ) ;
46
+
47
+ this . _resolver = ResolverFactory . createResolver ( {
48
+ // NOTE: @types /webpack InputFileSystem is missing some methods
49
+ // tslint:disable-next-line: no-any
50
+ fileSystem : this . inputFileSystem as any ,
51
+ extensions : [ '.json' ] ,
52
+ useSyncFileSystemCalls : true ,
53
+ symlinks,
54
+ } ) ;
42
55
}
43
56
44
57
/** Process the entire node modules tree. */
@@ -191,7 +204,10 @@ export class NgccProcessor {
191
204
192
205
// Purge this file from cache, since NGCC add new mainFields. Ex: module_ivy_ngcc
193
206
// which are unknown in the cached file.
194
- this . fileWatchPurger ( packageJsonPath ) ;
207
+ if ( this . inputFileSystem . purge ) {
208
+ // tslint:disable-next-line: no-any
209
+ ( this . inputFileSystem . purge as any ) ( packageJsonPath ) ;
210
+ }
195
211
196
212
this . _processedModules . add ( resolvedFileName ) ;
197
213
}
@@ -205,14 +221,10 @@ export class NgccProcessor {
205
221
*/
206
222
private tryResolvePackage ( moduleName : string , resolvedFileName : string ) : string | undefined {
207
223
try {
208
- // This is based on the logic in the NGCC compiler
209
- // tslint:disable-next-line:max-line-length
210
- // See: https://github.com/angular/angular/blob/b93c1dffa17e4e6900b3ab1b9e554b6da92be0de/packages/compiler-cli/src/ngcc/src/packages/dependency_host.ts#L85-L121
211
- return require . resolve ( `${ moduleName } /package.json` , {
212
- paths : [ resolvedFileName ] ,
213
- } ) ;
224
+ const resolvedPath = this . _resolver . resolveSync ( { } , resolvedFileName , `${ moduleName } /package.json` ) ;
225
+
226
+ return resolvedPath || undefined ;
214
227
} catch {
215
- // if it fails this might be a deep import which doesn't have a package.json
216
228
// Ex: @angular /compiler/src/i18n/i18n_ast/package.json
217
229
// or local libraries which don't reside in node_modules
218
230
const packageJsonPath = path . resolve ( resolvedFileName , '../package.json' ) ;
0 commit comments