Skip to content

Commit 54e6ab5

Browse files
alan-agius4kyliau
authored andcommitted
fix(@ngtools/webpack): report a warning when lazy route discovery is disabled
At the moment this will cause a runtime error instead. With this change a warning will be shown during the build. Closes #12238 and Closes #12025
1 parent fc460d2 commit 54e6ab5

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

packages/angular_devkit/build_angular/test/browser/no-entry-module_spec_large.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { runTargetSpec } from '@angular-devkit/architect/testing';
9+
import { TestLogger, runTargetSpec } from '@angular-devkit/architect/testing';
1010
import { tap } from 'rxjs/operators';
1111
import { browserTargetSpec, host } from '../utils';
1212

@@ -26,4 +26,15 @@ describe('Browser Builder no entry module', () => {
2626
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
2727
).toPromise().then(done, done.fail);
2828
});
29+
30+
it('reports warning when no bootstrap code', (done) => {
31+
host.replaceInFile('src/main.ts', /./g, '');
32+
host.appendToFile('src/main.ts', `import './app/app.module';`);
33+
34+
const logger = new TestLogger('no-bootstrap');
35+
runTargetSpec(host, browserTargetSpec, undefined, undefined, logger).pipe(
36+
tap(() => expect(logger.includes('Lazy routes discovery is not enabled')).toBe(true)),
37+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
38+
).toPromise().then(done, done.fail);
39+
});
2940
});

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,17 @@ export class AngularCompilerPlugin {
418418
}
419419

420420
// If there's still no entryModule try to resolve from mainPath.
421-
if (!this._entryModule && this._mainPath) {
421+
if (!this._entryModule && this._mainPath && !this._compilerOptions.enableIvy) {
422422
time('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
423423
this._entryModule = resolveEntryModuleFromMain(
424424
this._mainPath, this._compilerHost, this._getTsProgram() as ts.Program);
425+
426+
if (!this.entryModule) {
427+
this._warnings.push('Lazy routes discovery is not enabled. '
428+
+ 'Because there is neither an entryModule nor a '
429+
+ 'statically analyzable bootstrap code in the main file.',
430+
);
431+
}
425432
timeEnd('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
426433
}
427434
}

packages/ngtools/webpack/src/entry_resolver.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,13 @@ export function resolveEntryModuleFromMain(mainPath: string,
153153
.map(node => node.arguments[0] as ts.Identifier)
154154
.filter(node => node.kind == ts.SyntaxKind.Identifier);
155155

156-
if (bootstrap.length != 1) {
157-
return null;
158-
}
159-
const bootstrapSymbolName = bootstrap[0].text;
160-
const module = _symbolImportLookup(source, bootstrapSymbolName, host, program);
161-
if (module) {
162-
return `${module.replace(/\.ts$/, '')}#${bootstrapSymbolName}`;
156+
if (bootstrap.length === 1) {
157+
const bootstrapSymbolName = bootstrap[0].text;
158+
const module = _symbolImportLookup(source, bootstrapSymbolName, host, program);
159+
if (module) {
160+
return `${module.replace(/\.ts$/, '')}#${bootstrapSymbolName}`;
161+
}
163162
}
164163

165-
// shrug... something bad happened and we couldn't find the import statement.
166-
throw new Error('Tried to find bootstrap code, but could not. Specify either '
167-
+ 'statically analyzable bootstrap code or pass in an entryModule '
168-
+ 'to the plugins options.');
164+
return null;
169165
}

0 commit comments

Comments
 (0)