Skip to content

Commit

Permalink
refactor(@ngtools/webpack): create ngcc resolver from Webpack Compiler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clydin authored and alan-agius4 committed Jun 9, 2021
1 parent cf3b22d commit bf11f2b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
6 changes: 5 additions & 1 deletion packages/ngtools/webpack/src/ivy/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ function initializeNgccProcessor(

const errors: string[] = [];
const warnings: string[] = [];
const resolver = compiler.resolverFactory.get('normal', {
extensions: ['.json'],
useSyncFileSystemCalls: true,
});
const processor = new NgccProcessor(
mainFields,
warnings,
errors,
compiler.context,
tsconfig,
inputFileSystem,
webpackOptions.resolve?.symlinks,
resolver,
);

return { processor, errors, warnings };
Expand Down
21 changes: 4 additions & 17 deletions packages/ngtools/webpack/src/ngcc_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { LogLevel, Logger, process as mainNgcc } from '@angular/compiler-cli/ngcc';
import { spawnSync } from 'child_process';
import { createHash } from 'crypto';
import { Resolver, ResolverFactory } from 'enhanced-resolve';
import { Resolver } from 'enhanced-resolve';
import { accessSync, constants, existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
Expand All @@ -30,7 +30,6 @@ export class NgccProcessor {
private _processedModules = new Set<string>();
private _logger: NgccLogger;
private _nodeModulesDirectory: string;
private _resolver: Resolver;

constructor(
private readonly propertiesToConsider: string[],
Expand All @@ -39,19 +38,10 @@ export class NgccProcessor {
private readonly basePath: string,
private readonly tsConfigPath: string,
private readonly inputFileSystem: InputFileSystem,
private readonly symlinks: boolean | undefined,
private readonly resolver: Resolver,
) {
this._logger = new NgccLogger(this.compilationWarnings, this.compilationErrors);
this._nodeModulesDirectory = this.findNodeModulesDirectory(this.basePath);

this._resolver = ResolverFactory.createResolver({
// NOTE: @types/webpack InputFileSystem is missing some methods
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fileSystem: this.inputFileSystem as any,
extensions: ['.json'],
useSyncFileSystemCalls: true,
symlinks,
});
}

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

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

this._processedModules.add(resolvedFileName);
}
Expand All @@ -224,7 +211,7 @@ export class NgccProcessor {
*/
private tryResolvePackage(moduleName: string, resolvedFileName: string): string | undefined {
try {
const resolvedPath = this._resolver.resolveSync(
const resolvedPath = this.resolver.resolveSync(
{},
resolvedFileName,
`${moduleName}/package.json`,
Expand Down

0 comments on commit bf11f2b

Please sign in to comment.