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:
parent
4506230fbb
commit
550433a128
|
@ -69,7 +69,7 @@ const MAX_FILE_COUNT_FOR_SINGLE_FILE_EMIT = 20;
|
||||||
/**
|
/**
|
||||||
* Fields to lower within metadata in render2 mode.
|
* 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.
|
* Fields to lower within metadata in render3 mode.
|
||||||
|
|
|
@ -69,6 +69,7 @@ jasmine_node_test(
|
||||||
"//packages/common:npm_package",
|
"//packages/common:npm_package",
|
||||||
"//packages/core:npm_package",
|
"//packages/core:npm_package",
|
||||||
"//packages/platform-browser:npm_package",
|
"//packages/platform-browser:npm_package",
|
||||||
|
"//packages/router:npm_package",
|
||||||
],
|
],
|
||||||
deps = [
|
deps = [
|
||||||
":ngc_lib",
|
":ngc_lib",
|
||||||
|
|
|
@ -869,6 +869,40 @@ describe('ngc transformer command-line', () => {
|
||||||
expect(mymoduleSource).toMatch(/ɵ0 = .*'test'/);
|
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', () => {
|
it('should be able to lower supported expressions', () => {
|
||||||
writeConfig(`{
|
writeConfig(`{
|
||||||
"extends": "./tsconfig-base.json",
|
"extends": "./tsconfig-base.json",
|
||||||
|
|
|
@ -28,7 +28,7 @@ const IGNORE = {
|
||||||
|
|
||||||
const USE_VALUE = 'useValue';
|
const USE_VALUE = 'useValue';
|
||||||
const PROVIDE = 'provide';
|
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 TYPEGUARD_POSTFIX = 'TypeGuard';
|
||||||
const USE_IF = 'UseIf';
|
const USE_IF = 'UseIf';
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue