Skip to content

Commit 550433a

Browse files
committed
feat(compiler-cli): lower loadChildren fields to allow dynamic module paths (#23088)
Computing the value of loadChildren does not work externally, as the CLI needs to be able to detect the paths referenced to properly set up codesplitting. However, internally, different approaches to codesplitting require hashed module IDs, and the computation of those hashes involves something like: {path: '...', loadChildren: hashFn('module')} ngc should lower loadChildren into an exported constant in that case. This will never break externally, because loadChildren is always a string externally, and a string won't get lowered. PR Close #23088
1 parent 4506230 commit 550433a

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

packages/compiler-cli/src/transformers/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const MAX_FILE_COUNT_FOR_SINGLE_FILE_EMIT = 20;
6969
/**
7070
* Fields to lower within metadata in render2 mode.
7171
*/
72-
const LOWER_FIELDS = ['useValue', 'useFactory', 'data', 'id'];
72+
const LOWER_FIELDS = ['useValue', 'useFactory', 'data', 'id', 'loadChildren'];
7373

7474
/**
7575
* Fields to lower within metadata in render3 mode.

packages/compiler-cli/test/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ jasmine_node_test(
6969
"//packages/common:npm_package",
7070
"//packages/core:npm_package",
7171
"//packages/platform-browser:npm_package",
72+
"//packages/router:npm_package",
7273
],
7374
deps = [
7475
":ngc_lib",

packages/compiler-cli/test/ngc_spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,40 @@ describe('ngc transformer command-line', () => {
869869
expect(mymoduleSource).toMatch(/ɵ0 = .*'test'/);
870870
});
871871

872+
it('should lower loadChildren', () => {
873+
write('mymodule.ts', `
874+
import {Component, NgModule} from '@angular/core';
875+
import {RouterModule} from '@angular/router';
876+
877+
export function foo(): string {
878+
console.log('side-effect');
879+
return 'test';
880+
}
881+
882+
@Component({
883+
selector: 'route',
884+
template: 'route',
885+
})
886+
export class Route {}
887+
888+
@NgModule({
889+
declarations: [Route],
890+
imports: [
891+
RouterModule.forRoot([
892+
{path: '', pathMatch: 'full', component: Route, loadChildren: foo()}
893+
]),
894+
]
895+
})
896+
export class MyModule {}
897+
`);
898+
expect(compile()).toEqual(0);
899+
900+
const mymodulejs = path.resolve(outDir, 'mymodule.js');
901+
const mymoduleSource = fs.readFileSync(mymodulejs, 'utf8');
902+
expect(mymoduleSource).toContain('loadChildren: ɵ0');
903+
expect(mymoduleSource).toMatch(/ɵ0 = .*foo\(\)/);
904+
});
905+
872906
it('should be able to lower supported expressions', () => {
873907
writeConfig(`{
874908
"extends": "./tsconfig-base.json",

packages/compiler/src/aot/static_reflector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const IGNORE = {
2828

2929
const USE_VALUE = 'useValue';
3030
const PROVIDE = 'provide';
31-
const REFERENCE_SET = new Set([USE_VALUE, 'useFactory', 'data', 'id']);
31+
const REFERENCE_SET = new Set([USE_VALUE, 'useFactory', 'data', 'id', 'loadChildren']);
3232
const TYPEGUARD_POSTFIX = 'TypeGuard';
3333
const USE_IF = 'UseIf';
3434

0 commit comments

Comments
 (0)