Skip to content

Commit bf11f2b

Browse files
clydinalan-agius4
authored andcommitted
refactor(@ngtools/webpack): create ngcc resolver from Webpack Compiler
By using the Webpack compiler to request a resolver, configuration supplied options will automatically be used (such as symlinks) and also ensures that the same version of the resolver code will be used. Additionally, this removes one of the reasons to directly depend on the `enhanced-resolve` package.
1 parent cf3b22d commit bf11f2b

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

packages/ngtools/webpack/src/ivy/plugin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,18 @@ function initializeNgccProcessor(
7272

7373
const errors: string[] = [];
7474
const warnings: string[] = [];
75+
const resolver = compiler.resolverFactory.get('normal', {
76+
extensions: ['.json'],
77+
useSyncFileSystemCalls: true,
78+
});
7579
const processor = new NgccProcessor(
7680
mainFields,
7781
warnings,
7882
errors,
7983
compiler.context,
8084
tsconfig,
8185
inputFileSystem,
82-
webpackOptions.resolve?.symlinks,
86+
resolver,
8387
);
8488

8589
return { processor, errors, warnings };

packages/ngtools/webpack/src/ngcc_processor.ts

+4-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { LogLevel, Logger, process as mainNgcc } from '@angular/compiler-cli/ngcc';
1010
import { spawnSync } from 'child_process';
1111
import { createHash } from 'crypto';
12-
import { Resolver, ResolverFactory } from 'enhanced-resolve';
12+
import { Resolver } from 'enhanced-resolve';
1313
import { accessSync, constants, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
1414
import * as path from 'path';
1515
import * as ts from 'typescript';
@@ -30,7 +30,6 @@ export class NgccProcessor {
3030
private _processedModules = new Set<string>();
3131
private _logger: NgccLogger;
3232
private _nodeModulesDirectory: string;
33-
private _resolver: Resolver;
3433

3534
constructor(
3635
private readonly propertiesToConsider: string[],
@@ -39,19 +38,10 @@ export class NgccProcessor {
3938
private readonly basePath: string,
4039
private readonly tsConfigPath: string,
4140
private readonly inputFileSystem: InputFileSystem,
42-
private readonly symlinks: boolean | undefined,
41+
private readonly resolver: Resolver,
4342
) {
4443
this._logger = new NgccLogger(this.compilationWarnings, this.compilationErrors);
4544
this._nodeModulesDirectory = this.findNodeModulesDirectory(this.basePath);
46-
47-
this._resolver = ResolverFactory.createResolver({
48-
// NOTE: @types/webpack InputFileSystem is missing some methods
49-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50-
fileSystem: this.inputFileSystem as any,
51-
extensions: ['.json'],
52-
useSyncFileSystemCalls: true,
53-
symlinks,
54-
});
5545
}
5646

5747
/** Process the entire node modules tree. */
@@ -207,10 +197,7 @@ export class NgccProcessor {
207197

208198
// Purge this file from cache, since NGCC add new mainFields. Ex: module_ivy_ngcc
209199
// which are unknown in the cached file.
210-
if (this.inputFileSystem.purge) {
211-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
212-
(this.inputFileSystem.purge as any)(packageJsonPath);
213-
}
200+
this.inputFileSystem.purge?.(packageJsonPath);
214201

215202
this._processedModules.add(resolvedFileName);
216203
}
@@ -224,7 +211,7 @@ export class NgccProcessor {
224211
*/
225212
private tryResolvePackage(moduleName: string, resolvedFileName: string): string | undefined {
226213
try {
227-
const resolvedPath = this._resolver.resolveSync(
214+
const resolvedPath = this.resolver.resolveSync(
228215
{},
229216
resolvedFileName,
230217
`${moduleName}/package.json`,

0 commit comments

Comments
 (0)