6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { tags } from '@angular-devkit/core' ;
9
- import { transformTypescript } from './ast_helpers' ;
9
+ import { createTypescriptContext , transformTypescript } from './ast_helpers' ;
10
10
import { importFactory } from './import_factory' ;
11
11
12
12
describe ( '@ngtools/webpack transformers' , ( ) => {
13
13
describe ( 'import_factory' , ( ) => {
14
14
it ( 'should support arrow functions' , ( ) => {
15
+ const additionalFiles : Record < string , string > = {
16
+ 'lazy/lazy.module.ts' : `
17
+ export const LazyModule = {};
18
+ ` ,
19
+ } ;
15
20
const input = tags . stripIndent `
16
21
const ɵ0 = () => import('./lazy/lazy.module').then(m => m.LazyModule);
17
22
const routes = [{
@@ -28,14 +33,20 @@ describe('@ngtools/webpack transformers', () => {
28
33
` ;
29
34
30
35
let warningCalled = false ;
31
- const transformer = importFactory ( ( ) => warningCalled = true ) ;
32
- const result = transformTypescript ( input , [ transformer ] ) ;
36
+ const { program, compilerHost } = createTypescriptContext ( input , additionalFiles , true ) ;
37
+ const transformer = importFactory ( ( ) => warningCalled = true , ( ) => program . getTypeChecker ( ) ) ;
38
+ const result = transformTypescript ( undefined , [ transformer ] , program , compilerHost ) ;
33
39
34
40
expect ( tags . oneLine `${ result } ` ) . toEqual ( tags . oneLine `${ output } ` ) ;
35
41
expect ( warningCalled ) . toBeFalsy ( ) ;
36
42
} ) ;
37
43
38
44
it ( 'should not transform if the format is different than expected' , ( ) => {
45
+ const additionalFiles : Record < string , string > = {
46
+ 'lazy/lazy.module.ts' : `
47
+ export const LazyModule = {};
48
+ ` ,
49
+ } ;
39
50
const input = tags . stripIndent `
40
51
const ɵ0 = () => import('./lazy/lazy.module').then(function (m) { return m.LazyModule; });
41
52
const routes = [{
@@ -45,35 +56,46 @@ describe('@ngtools/webpack transformers', () => {
45
56
` ;
46
57
47
58
let warningCalled = false ;
48
- const transformer = importFactory ( ( ) => warningCalled = true ) ;
49
- const result = transformTypescript ( input , [ transformer ] ) ;
59
+ const { program, compilerHost } = createTypescriptContext ( input , additionalFiles , true ) ;
60
+ const transformer = importFactory ( ( ) => warningCalled = true , ( ) => program . getTypeChecker ( ) ) ;
61
+ const result = transformTypescript ( undefined , [ transformer ] , program , compilerHost ) ;
50
62
51
63
expect ( tags . oneLine `${ result } ` ) . toEqual ( tags . oneLine `${ input } ` ) ;
52
64
expect ( warningCalled ) . toBeTruthy ( ) ;
53
65
} ) ;
54
66
55
- it ( 'should support different arg name' , ( ) => {
67
+ it ( 'should support resolving reexports' , ( ) => {
68
+ const additionalFiles : Record < string , string > = {
69
+ 'shared/index.ts' : `
70
+ export * from './path/to/lazy/lazy.module';
71
+ ` ,
72
+ 'shared/path/to/lazy/lazy.module.ts' : `
73
+ export const LazyModule = {};
74
+ ` ,
75
+ } ;
56
76
const input = tags . stripIndent `
57
- const ɵ0 = () => import('./lazy/lazy.module ').then(a => a .LazyModule);
77
+ const ɵ0 = () => import('./shared ').then(m => m .LazyModule);
58
78
const routes = [{
59
79
path: 'lazy',
60
80
loadChildren: ɵ0
61
81
}];
62
82
` ;
83
+
84
+ // tslint:disable: max-line-length
63
85
const output = tags . stripIndent `
64
- const ɵ0 = () => import("./lazy/lazy.module.ngfactory").then(a => a .LazyModuleNgFactory);
86
+ const ɵ0 = () => import("./shared/path/to/ lazy/lazy.module.ngfactory").then(m => m .LazyModuleNgFactory);
65
87
const routes = [{
66
88
path: 'lazy',
67
89
loadChildren: ɵ0
68
90
}];
69
91
` ;
92
+ // tslint:enable: max-line-length
70
93
71
- let warningCalled = false ;
72
- const transformer = importFactory ( ( ) => warningCalled = true ) ;
73
- const result = transformTypescript ( input , [ transformer ] ) ;
94
+ const { program , compilerHost } = createTypescriptContext ( input , additionalFiles , true ) ;
95
+ const transformer = importFactory ( ( ) => { } , ( ) => program . getTypeChecker ( ) ) ;
96
+ const result = transformTypescript ( undefined , [ transformer ] , program , compilerHost ) ;
74
97
75
98
expect ( tags . oneLine `${ result } ` ) . toEqual ( tags . oneLine `${ output } ` ) ;
76
- expect ( warningCalled ) . toBeFalsy ( ) ;
77
99
} ) ;
78
100
} ) ;
79
101
} ) ;
0 commit comments