99import { LogLevel , Logger , process as mainNgcc } from '@angular/compiler-cli/ngcc' ;
1010import { spawnSync } from 'child_process' ;
1111import { createHash } from 'crypto' ;
12+ import { Resolver , ResolverFactory } from 'enhanced-resolve' ;
1213import { accessSync , constants , existsSync , mkdirSync , readFileSync , writeFileSync } from 'fs' ;
1314import * as path from 'path' ;
1415import * as ts from 'typescript' ;
16+ import { InputFileSystem } from 'webpack' ;
1517import { time , timeEnd } from './benchmark' ;
1618
1719// We cannot create a plugin for this, because NGTSC requires addition type
@@ -28,17 +30,28 @@ export class NgccProcessor {
2830 private _processedModules = new Set < string > ( ) ;
2931 private _logger : NgccLogger ;
3032 private _nodeModulesDirectory : string ;
33+ private _resolver : Resolver ;
3134
3235 constructor (
3336 private readonly propertiesToConsider : string [ ] ,
34- private readonly fileWatchPurger : ( path : string ) => void ,
3537 private readonly compilationWarnings : ( Error | string ) [ ] ,
3638 private readonly compilationErrors : ( Error | string ) [ ] ,
3739 private readonly basePath : string ,
3840 private readonly tsConfigPath : string ,
41+ private readonly inputFileSystem : InputFileSystem ,
42+ private readonly symlinks : boolean | undefined ,
3943 ) {
4044 this . _logger = new NgccLogger ( this . compilationWarnings , this . compilationErrors ) ;
4145 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+ } ) ;
4255 }
4356
4457 /** Process the entire node modules tree. */
@@ -191,7 +204,10 @@ export class NgccProcessor {
191204
192205 // Purge this file from cache, since NGCC add new mainFields. Ex: module_ivy_ngcc
193206 // 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+ }
195211
196212 this . _processedModules . add ( resolvedFileName ) ;
197213 }
@@ -205,14 +221,10 @@ export class NgccProcessor {
205221 */
206222 private tryResolvePackage ( moduleName : string , resolvedFileName : string ) : string | undefined {
207223 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 ;
214227 } catch {
215- // if it fails this might be a deep import which doesn't have a package.json
216228 // Ex: @angular /compiler/src/i18n/i18n_ast/package.json
217229 // or local libraries which don't reside in node_modules
218230 const packageJsonPath = path . resolve ( resolvedFileName , '../package.json' ) ;
0 commit comments