Skip to content

fix(@ngtools/webpack): NGCC should process secondary entry-points #14934

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
Jun 28, 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
30 changes: 10 additions & 20 deletions packages/ngtools/webpack/src/ngcc_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export class NgccProcessor {
resolvedModule: ts.ResolvedModule | ts.ResolvedTypeReferenceDirective,
): void {
const resolvedFileName = resolvedModule.resolvedFileName;
if (
!resolvedFileName
|| moduleName.startsWith('.')
|| this._processedModules.has(moduleName)) {
if (!resolvedFileName || moduleName.startsWith('.') || this._processedModules.has(moduleName)) {
// Skip when module is unknown, relative or NGCC compiler is not found or already processed.
return;
}
Expand Down Expand Up @@ -97,25 +94,19 @@ export class NgccProcessor {
*/
private tryResolvePackage(moduleName: string, resolvedFileName: string): string | undefined {
try {
let packageJsonPath = path.resolve(resolvedFileName, '../package.json');
if (existsSync(packageJsonPath)) {
return packageJsonPath;
}

// This is based on the logic in the NGCC compiler
// tslint:disable-next-line:max-line-length
// See: https://github.com/angular/angular/blob/b93c1dffa17e4e6900b3ab1b9e554b6da92be0de/packages/compiler-cli/src/ngcc/src/packages/dependency_host.ts#L85-L121
packageJsonPath = require.resolve(`${moduleName}/package.json`,
{
paths: [resolvedFileName],
},
);

return packageJsonPath;
return require.resolve(`${moduleName}/package.json`, {
paths: [resolvedFileName],
});
} catch {
// if it fails this might be a deep import which doesn't have a package.json
// Ex: @angular/compiler/src/i18n/i18n_ast/package.json
return undefined;
// or local libraries which don't reside in node_modules
const packageJsonPath = path.resolve(resolvedFileName, '../package.json');

return existsSync(packageJsonPath) ? packageJsonPath : undefined;
}
}

Expand All @@ -138,10 +129,9 @@ class NgccLogger implements Logger {
constructor(
private readonly compilationWarnings: (Error | string)[],
private readonly compilationErrors: (Error | string)[],
) {
}
) {}

debug(..._args: string[]) { }
debug(..._args: string[]) {}

info(...args: string[]) {
// Log to stderr because it's a progress-like info message.
Expand Down
16 changes: 14 additions & 2 deletions tests/legacy-cli/e2e/tests/ivy/ivy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { expectFileToExist, replaceInFile } from '../../utils/fs';
import { request } from '../../utils/http';
import { killAllProcesses } from '../../utils/process';
import { killAllProcesses, ng } from '../../utils/process';
import { createProject, ngServe } from '../../utils/project';

export default async function() {
try {
await createProject('ivy-project', '--enable-ivy');

// Add in a reference to a secondary entry-point to check that ngcc processes it correctly
await replaceInFile(
'src/app/app.module.ts',
`import { AppComponent } from './app.component';`,
`import { AppComponent } from './app.component';\nimport { HttpClientModule } from '@angular/common/http';`,
);
await replaceInFile('src/app/app.module.ts', `imports: [`, `imports: [\n HttpClientModule,`);

await ngServe('--prod');

// Check that @angular/common/http was processed by ngcc
await expectFileToExist('node_modules/@angular/common/http/http.d.ts.__ivy_ngcc_bak');

// Verify the index.html
const body = await request('http://localhost:4200/');
if (!body.match(/<app-root><\/app-root>/)) {
Expand All @@ -33,6 +45,6 @@ export default async function() {
throw new Error('NgDevMode was not tree shaken away.');
}
} finally {
await killAllProcesses();
killAllProcesses();
}
}