Skip to content

Commit f61ea6b

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
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 9d69367 commit f61ea6b

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

+12-1
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,17 @@ export class AngularCompilerPlugin {
410410
}
411411

412412
// If there's still no entryModule try to resolve from mainPath.
413-
if (!this._entryModule && this._mainPath) {
413+
if (!this._entryModule && this._mainPath && !this._compilerOptions.enableIvy) {
414414
time('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
415415
this._entryModule = resolveEntryModuleFromMain(
416416
this._mainPath, this._compilerHost, this._getTsProgram() as ts.Program);
417+
418+
if (!this.entryModule) {
419+
this._warnings.push('Lazy routes discovery is not enabled. '
420+
+ 'Because there is neither an entryModule nor a '
421+
+ 'statically analyzable bootstrap code in the main file.',
422+
);
423+
}
417424
timeEnd('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
418425
}
419426
}

packages/ngtools/webpack/src/entry_resolver.ts

+7-11
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)