Skip to content

fix(@ngtools/webpack): fixes ngcc error when project name is the same… #14325

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

Merged
merged 1 commit into from
May 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ export class AngularCompilerPlugin {
compilerWithFileSystems.inputFileSystem,
this._warnings,
this._errors,
this._basePath,
);
}
}
Expand Down
26 changes: 21 additions & 5 deletions packages/ngtools/webpack/src/ngcc_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
*/

import { Logger } from '@angular/compiler-cli/ngcc';
import { existsSync } from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import { InputFileSystem } from 'webpack';
import { time, timeEnd } from './benchmark';
import { workaroundResolve } from './utils';

// We cannot create a plugin for this, because NGTSC requires addition type
// information which ngcc creates when processing a package which was compiled with NGC.
Expand All @@ -24,17 +25,19 @@ import { workaroundResolve } from './utils';

export class NgccProcessor {
private _processedModules = new Set<string>();

private _logger: NgccLogger;
private _nodeModulesDirectory: string;

constructor(
private readonly ngcc: typeof import('@angular/compiler-cli/ngcc'),
private readonly propertiesToConsider: string[],
private readonly inputFileSystem: InputFileSystem,
private readonly compilationWarnings: (Error | string)[],
private readonly compilationErrors: (Error | string)[],
private readonly basePath: string,
) {
this._logger = new NgccLogger(this.compilationWarnings, this.compilationErrors);
this._nodeModulesDirectory = this.findNodeModulesDirectory(this.basePath);
}

processModule(
Expand All @@ -57,13 +60,12 @@ export class NgccProcessor {

return;
}
const normalizedJsonPath = workaroundResolve(packageJsonPath);

const timeLabel = `NgccProcessor.processModule.ngcc.process+${moduleName}`;
time(timeLabel);
this.ngcc.process({
basePath: normalizedJsonPath.substring(0, normalizedJsonPath.indexOf(moduleName)),
targetEntryPointPath: moduleName,
basePath: this._nodeModulesDirectory,
targetEntryPointPath: path.dirname(packageJsonPath),
propertiesToConsider: this.propertiesToConsider,
compileAllFormats: false,
createNewEntryPointFormats: true,
Expand Down Expand Up @@ -101,6 +103,20 @@ export class NgccProcessor {
return undefined;
}
}

private findNodeModulesDirectory(startPoint: string): string {
let current = startPoint;
while (path.dirname(current) !== current) {
const nodePath = path.join(current, 'node_modules');
if (existsSync(nodePath)) {
return nodePath;
}

current = path.dirname(current);
}

throw new Error(`Cannot locate the 'node_modules' directory.`);
}
}

class NgccLogger implements Logger {
Expand Down