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 { readFile , replaceInFile , writeFile } from '../../utils/fs' ;
8
+ import { getGlobalVariable } from '../../utils/env' ;
9
+ import { appendToFile , prependToFile , readFile , replaceInFile , writeFile } from '../../utils/fs' ;
9
10
import { ng } from '../../utils/process' ;
10
- import { createProject , updateJsonFile } from '../../utils/project' ;
11
+ import { updateJsonFile } from '../../utils/project' ;
11
12
import { expectToFail } from '../../utils/utils' ;
12
13
13
14
export default async function ( ) {
14
- const projectName = 'ivy-lazy-loading' ;
15
+ const argv = getGlobalVariable ( 'argv' ) ;
16
+ const ivyProject = argv [ 'ivy' ] ;
17
+ const projectName = 'test-project' ;
15
18
const appRoutingModulePath = 'src/app/app-routing.module.ts' ;
16
19
17
- // Make Ivy project.
18
- await createProject ( projectName , '--enable-ivy' , '--routing' ) ;
20
+ // Add app routing.
21
+ // This is done automatically on a new app with --routing.
22
+ await writeFile ( appRoutingModulePath , `
23
+ import { NgModule } from '@angular/core';
24
+ import { Routes, RouterModule } from '@angular/router';
25
+
26
+ const routes: Routes = [];
27
+
28
+ @NgModule({
29
+ imports: [RouterModule.forRoot(routes)],
30
+ exports: [RouterModule]
31
+ })
32
+ export class AppRoutingModule { }
33
+ ` ) ;
34
+ await prependToFile ( 'src/app/app.module.ts' ,
35
+ `import { AppRoutingModule } from './app-routing.module';` ) ;
36
+ await replaceInFile ( 'src/app/app.module.ts' , `imports: [` , `imports: [ AppRoutingModule,` ) ;
37
+ await appendToFile ( 'src/app/app.component.html' , '<router-outlet></router-outlet>' ) ;
19
38
20
39
const originalAppRoutingModule = await readFile ( appRoutingModulePath ) ;
21
40
// helper to replace loadChildren
@@ -57,8 +76,10 @@ export default async function () {
57
76
58
77
// Set factory shims to false.
59
78
await updateJsonFile ( 'tsconfig.app.json' , json => {
60
- const angularCompilerOptions = json [ 'angularCompilerOptions' ] ;
61
- angularCompilerOptions [ 'allowEmptyCodegenFiles' ] = false ;
79
+ if ( json [ 'angularCompilerOptions' ] === undefined ) {
80
+ json [ 'angularCompilerOptions' ] = { } ;
81
+ }
82
+ json [ 'angularCompilerOptions' ] [ 'allowEmptyCodegenFiles' ] = false ;
62
83
} ) ;
63
84
64
85
// Convert the default config to use JIT and prod to just do AOT.
@@ -69,22 +90,42 @@ export default async function () {
69
90
buildTarget [ 'configurations' ] [ 'production' ] = { aot : true } ;
70
91
} ) ;
71
92
72
- // Test `import()` style lazy load.
73
- await replaceLoadChildren ( `() => import('./lazy/lazy.module').then(m => m.LazyModule)` ) ;
74
- await ng ( 'e2e' ) ;
75
- await ng ( 'e2e' , '--prod' ) ;
76
-
77
93
// Test string import with factory shims.
78
94
await replaceLoadChildren ( `'./lazy/lazy.module#LazyModule'` ) ;
79
95
await replaceInFile ( 'tsconfig.app.json' , `"allowEmptyCodegenFiles": false` ,
80
96
`"allowEmptyCodegenFiles": true` ) ;
81
- await expectToFail ( ( ) => ng ( 'e2e' ) ) ; // Currently broken.
82
- await ng ( 'e2e' , '--prod' ) ;
97
+ if ( ivyProject ) {
98
+ // Ivy should not support the string syntax.
99
+ await expectToFail ( ( ) => ng ( 'e2e' ) ) ;
100
+ await expectToFail ( ( ) => ng ( 'e2e' , '--prod' ) ) ;
101
+ } else {
102
+ // View engine should support the string syntax.
103
+ await ng ( 'e2e' ) ;
104
+ await ng ( 'e2e' , '--prod' ) ;
105
+ }
83
106
84
107
// Test string import without factory shims.
85
108
await replaceLoadChildren ( `'./lazy/lazy.module#LazyModule'` ) ;
86
109
await replaceInFile ( 'tsconfig.app.json' , `"allowEmptyCodegenFiles": true` ,
87
- `"allowEmptyCodegenFiles": false` ) ;
88
- await expectToFail ( ( ) => ng ( 'e2e' ) ) ; // Not supported.
89
- await expectToFail ( ( ) => ng ( 'e2e' , '--prod' ) ) ; // Not supported.
110
+ `"allowEmptyCodegenFiles": false` ) ;
111
+ if ( ivyProject ) {
112
+ // Ivy should not support the string syntax.
113
+ await expectToFail ( ( ) => ng ( 'e2e' ) ) ;
114
+ await expectToFail ( ( ) => ng ( 'e2e' , '--prod' ) ) ;
115
+ } else {
116
+ // View engine should support the string syntax.
117
+ await ng ( 'e2e' ) ;
118
+ await ng ( 'e2e' , '--prod' ) ;
119
+ }
120
+
121
+ // Test `import()` style lazy load.
122
+ await updateJsonFile ( 'angular.json' , json => {
123
+ // Add the experimental flag to import factories in View Engine.
124
+ const buildTarget = json [ 'projects' ] [ projectName ] [ 'architect' ] [ 'build' ] ;
125
+ buildTarget [ 'options' ] [ 'experimentalImportFactories' ] = true ;
126
+ } ) ;
127
+ // Both Ivy and View Engine should support it.
128
+ await replaceLoadChildren ( `() => import('./lazy/lazy.module').then(m => m.LazyModule)` ) ;
129
+ await ng ( 'e2e' ) ;
130
+ await ng ( 'e2e' , '--prod' ) ;
90
131
}
0 commit comments