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
This commit is contained in:
Alex Rickabaugh 2018-03-30 08:02:30 -07:00
parent 4506230fbb
commit 550433a128
4 changed files with 37 additions and 2 deletions

View File

@ -69,7 +69,7 @@ const MAX_FILE_COUNT_FOR_SINGLE_FILE_EMIT = 20;
/**
* Fields to lower within metadata in render2 mode.
*/
const LOWER_FIELDS = ['useValue', 'useFactory', 'data', 'id'];
const LOWER_FIELDS = ['useValue', 'useFactory', 'data', 'id', 'loadChildren'];
/**
* Fields to lower within metadata in render3 mode.

View File

@ -69,6 +69,7 @@ jasmine_node_test(
"//packages/common:npm_package",
"//packages/core:npm_package",
"//packages/platform-browser:npm_package",
"//packages/router:npm_package",
],
deps = [
":ngc_lib",

View File

@ -869,6 +869,40 @@ describe('ngc transformer command-line', () => {
expect(mymoduleSource).toMatch(/ɵ0 = .*'test'/);
});
it('should lower loadChildren', () => {
write('mymodule.ts', `
import {Component, NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
export function foo(): string {
console.log('side-effect');
return 'test';
}
@Component({
selector: 'route',
template: 'route',
})
export class Route {}
@NgModule({
declarations: [Route],
imports: [
RouterModule.forRoot([
{path: '', pathMatch: 'full', component: Route, loadChildren: foo()}
]),
]
})
export class MyModule {}
`);
expect(compile()).toEqual(0);
const mymodulejs = path.resolve(outDir, 'mymodule.js');
const mymoduleSource = fs.readFileSync(mymodulejs, 'utf8');
expect(mymoduleSource).toContain('loadChildren: ɵ0');
expect(mymoduleSource).toMatch(/ɵ0 = .*foo\(\)/);
});
it('should be able to lower supported expressions', () => {
writeConfig(`{
"extends": "./tsconfig-base.json",

View File

@ -28,7 +28,7 @@ const IGNORE = {
const USE_VALUE = 'useValue';
const PROVIDE = 'provide';
const REFERENCE_SET = new Set([USE_VALUE, 'useFactory', 'data', 'id']);
const REFERENCE_SET = new Set([USE_VALUE, 'useFactory', 'data', 'id', 'loadChildren']);
const TYPEGUARD_POSTFIX = 'TypeGuard';
const USE_IF = 'UseIf';