|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import { replaceInFile, writeFile } from '../../utils/fs'; |
| 8 | +import { readFile, replaceInFile, writeFile } from '../../utils/fs'; |
9 | 9 | import { ng } from '../../utils/process'; |
10 | 10 | import { createProject, updateJsonFile } from '../../utils/project'; |
11 | 11 | import { expectToFail } from '../../utils/utils'; |
12 | 12 |
|
13 | | -export async function ivyLazyLoadSetup(projectName: string) { |
| 13 | +export default async function () { |
| 14 | + const projectName = 'ivy-lazy-loading'; |
| 15 | + const appRoutingModulePath = 'src/app/app-routing.module.ts'; |
| 16 | + |
14 | 17 | // Make Ivy project. |
15 | 18 | await createProject(projectName, '--enable-ivy', '--routing'); |
16 | 19 |
|
| 20 | + const originalAppRoutingModule = await readFile(appRoutingModulePath); |
| 21 | + // helper to replace loadChildren |
| 22 | + const replaceLoadChildren = async (route: string) => { |
| 23 | + const content = originalAppRoutingModule.replace('const routes: Routes = [];', ` |
| 24 | + const routes: Routes = [{ path: 'lazy', loadChildren: ${route} }]; |
| 25 | + `); |
| 26 | + |
| 27 | + return writeFile(appRoutingModulePath, content); |
| 28 | + }; |
| 29 | + |
17 | 30 | // Add lazy route. |
18 | 31 | await ng('generate', 'module', 'lazy', '--routing'); |
19 | | - await replaceInFile('src/app/app-routing.module.ts', 'const routes: Routes = [];', ` |
20 | | - const routes: Routes = [{ path: 'lazy', loadChildren: LOAD_CHILDREN_MARKER }]; |
21 | | - `); |
22 | 32 | await ng('generate', 'component', 'lazy/lazy-comp'); |
23 | 33 | await replaceInFile('src/app/lazy/lazy-routing.module.ts', 'const routes: Routes = [];', ` |
24 | 34 | import { LazyCompComponent } from './lazy-comp/lazy-comp.component'; |
25 | 35 | const routes: Routes = [{ path: '', component: LazyCompComponent }]; |
26 | 36 | `); |
27 | | - await writeFile('src/app/app.component.html', '<router-outlet></router-outlet>'); |
28 | 37 |
|
29 | 38 | // Add lazy route e2e |
30 | 39 | await writeFile('e2e/src/app.e2e-spec.ts', ` |
@@ -59,30 +68,23 @@ export async function ivyLazyLoadSetup(projectName: string) { |
59 | 68 | buildTarget['options']['aot'] = false; |
60 | 69 | buildTarget['configurations']['production'] = { aot: true }; |
61 | 70 | }); |
62 | | -} |
63 | 71 |
|
64 | | -export default async function () { |
65 | 72 | // Test `import()` style lazy load. |
66 | | - await ivyLazyLoadSetup('ivy-lazy-import'); |
67 | | - await replaceInFile('src/app/app-routing.module.ts', 'LOAD_CHILDREN_MARKER', ` |
68 | | - () => import('./lazy/lazy.module').then(m => m.LazyModule) |
69 | | - `); |
| 73 | + await replaceLoadChildren(`() => import('./lazy/lazy.module').then(m => m.LazyModule)`); |
70 | 74 | await ng('e2e'); |
71 | 75 | await ng('e2e', '--prod'); |
72 | 76 |
|
73 | 77 | // Test string import with factory shims. |
74 | | - await ivyLazyLoadSetup('ivy-lazy-string-shims'); |
75 | | - await replaceInFile('src/app/app-routing.module.ts', 'LOAD_CHILDREN_MARKER', |
76 | | - `'./lazy/lazy.module#LazyModule'`); |
| 78 | + await replaceLoadChildren(`'./lazy/lazy.module#LazyModule'`); |
77 | 79 | await replaceInFile('tsconfig.app.json', `"allowEmptyCodegenFiles": false`, |
78 | 80 | `"allowEmptyCodegenFiles": true`); |
79 | 81 | await expectToFail(() => ng('e2e')); // Currently broken. |
80 | 82 | await ng('e2e', '--prod'); |
81 | 83 |
|
82 | 84 | // Test string import without factory shims. |
83 | | - await ivyLazyLoadSetup('ivy-lazy-string-no-shims'); |
84 | | - await replaceInFile('src/app/app-routing.module.ts', 'LOAD_CHILDREN_MARKER', |
85 | | - `'./lazy/lazy.module#LazyModule'`); |
| 85 | + await replaceLoadChildren(`'./lazy/lazy.module#LazyModule'`); |
| 86 | + await replaceInFile('tsconfig.app.json', `"allowEmptyCodegenFiles": true`, |
| 87 | + `"allowEmptyCodegenFiles": false`); |
86 | 88 | await expectToFail(() => ng('e2e')); // Not supported. |
87 | 89 | await expectToFail(() => ng('e2e', '--prod')); // Not supported. |
88 | 90 | } |
0 commit comments