Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@ngtools/webpack): create ngcc resolver from Webpack Compiler #21090

Merged
merged 1 commit into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/ngtools/webpack/src/ivy/plugin.ts
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
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