@@ -7,6 +7,13 @@ proxyquire.noCallThru();
7
7
8
8
class EmptyClass { } ;
9
9
10
+ let angularCompilerOptions : any ;
11
+ class AngularCompilerStub {
12
+ constructor ( options ) {
13
+ angularCompilerOptions = options ;
14
+ }
15
+ } ;
16
+
10
17
const nativeScriptDevWebpack = {
11
18
GenerateBundleStarterPlugin : EmptyClass ,
12
19
WatchStateLoggerPlugin : EmptyClass ,
@@ -18,15 +25,18 @@ const nativeScriptDevWebpack = {
18
25
} ;
19
26
20
27
const emptyObject = { } ;
21
-
28
+ const FakeAotTransformerFlag = "aot" ;
29
+ const FakeHmrTransformerFlag = "hmr" ;
30
+ const FakeLazyTransformerFlag = "lazy" ;
22
31
const webpackConfigAngular = proxyquire ( './webpack.angular' , {
23
32
'nativescript-dev-webpack' : nativeScriptDevWebpack ,
24
33
'nativescript-dev-webpack/nativescript-target' : emptyObject ,
25
- 'nativescript-dev-webpack/transformers/ns-replace-bootstrap' : emptyObject ,
26
- 'nativescript-dev-webpack/transformers/ns-replace-lazy-loader' : emptyObject ,
27
- 'nativescript-dev-webpack/utils/ast-utils' : emptyObject ,
34
+ 'nativescript-dev-webpack/transformers/ns-replace-bootstrap' : { nsReplaceBootstrap : ( ) => { return FakeAotTransformerFlag } } ,
35
+ 'nativescript-dev-webpack/transformers/ns-replace-lazy-loader' : { nsReplaceLazyLoader : ( ) => { return FakeLazyTransformerFlag } } ,
36
+ 'nativescript-dev-webpack/transformers/ns-support-hmr-ng' : { nsSupportHmrNg : ( ) => { return FakeHmrTransformerFlag } } ,
37
+ 'nativescript-dev-webpack/utils/ast-utils' : { getMainModulePath : ( ) => { return "fakePath" ; } } ,
28
38
'@ngtools/webpack' : {
29
- AngularCompilerPlugin : EmptyClass
39
+ AngularCompilerPlugin : AngularCompilerStub
30
40
}
31
41
} ) ;
32
42
@@ -48,6 +58,12 @@ const webpackConfigVue = proxyquire('./webpack.vue', {
48
58
} ) ;
49
59
50
60
describe ( 'webpack.config.js' , ( ) => {
61
+ const getInput = ( options : { platform : string , aot ?: boolean , hmr ?: boolean , externals ?: string [ ] } ) => {
62
+ const input : any = { aot : options . aot , hmr : options . hmr , externals : options . externals } ;
63
+ input [ options . platform ] = true ;
64
+ return input ;
65
+ } ;
66
+
51
67
[
52
68
{ type : 'javascript' , webpackConfig : webpackConfigJavaScript } ,
53
69
{ type : 'typescript' , webpackConfig : webpackConfigTypeScript } ,
@@ -57,12 +73,6 @@ describe('webpack.config.js', () => {
57
73
const { type, webpackConfig } = element ;
58
74
59
75
describe ( `verify externals for webpack.${ type } .js` , ( ) => {
60
- const getInput = ( platform : string , externals : string [ ] ) => {
61
- const input : any = { externals } ;
62
- input [ platform ] = true ;
63
- return input ;
64
- } ;
65
-
66
76
[
67
77
'android' ,
68
78
'ios'
@@ -73,7 +83,7 @@ describe('webpack.config.js', () => {
73
83
} ) ;
74
84
75
85
it ( 'returns empty array when externals are not passed' , ( ) => {
76
- const config = webpackConfig ( getInput ( platform , null ) ) ;
86
+ const config = webpackConfig ( getInput ( { platform } ) ) ;
77
87
expect ( config . externals ) . toEqual ( [ ] ) ;
78
88
} ) ;
79
89
@@ -84,7 +94,7 @@ describe('webpack.config.js', () => {
84
94
return [ ] ;
85
95
} ;
86
96
87
- const input = getInput ( platform , [ 'nativescript-vue' ] ) ;
97
+ const input = getInput ( { platform, externals : [ 'nativescript-vue' ] } ) ;
88
98
webpackConfig ( input ) ;
89
99
expect ( isCalled ) . toBe ( true , 'Webpack.config.js must use the getConvertedExternals method' ) ;
90
100
} ) ;
@@ -99,7 +109,7 @@ describe('webpack.config.js', () => {
99
109
expectedOutput : [ / ^ n a t i v e s c r i p t - v u e ( ( \/ .* ) | $ ) / , / ^ n a t i v e s c r i p t - a n g u l a r ( ( \/ .* ) | $ ) / ]
100
110
} ,
101
111
] . forEach ( testCase => {
102
- const input = getInput ( platform , testCase . input ) ;
112
+ const input = getInput ( { platform, externals : testCase . input } ) ;
103
113
104
114
it ( `are correct regular expressions, for input ${ testCase . input } ` , ( ) => {
105
115
const config = webpackConfig ( input ) ;
@@ -110,4 +120,133 @@ describe('webpack.config.js', () => {
110
120
} ) ;
111
121
} ) ;
112
122
} ) ;
123
+
124
+ [
125
+ 'android' ,
126
+ 'ios'
127
+ ] . forEach ( platform => {
128
+ describe ( `angular transformers (${ platform } )` , ( ) => {
129
+
130
+ beforeEach ( ( ) => {
131
+ angularCompilerOptions = null ;
132
+ } ) ;
133
+
134
+ it ( "should be empty by default" , ( ) => {
135
+ const input = getInput ( { platform } ) ;
136
+
137
+ webpackConfigAngular ( input ) ;
138
+
139
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
140
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
141
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 0 ) ;
142
+ } ) ;
143
+
144
+ it ( "should contain the AOT transformer when the AOT flag is passed" , ( ) => {
145
+ const input = getInput ( { platform, aot : true } ) ;
146
+
147
+ webpackConfigAngular ( input ) ;
148
+
149
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
150
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
151
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 1 ) ;
152
+ expect ( angularCompilerOptions . platformTransformers [ 0 ] ) . toEqual ( FakeAotTransformerFlag ) ;
153
+ } ) ;
154
+
155
+ it ( "should contain the HMR transformer when the HMR flag is passed" , ( ) => {
156
+ const input = getInput ( { platform, hmr : true } ) ;
157
+
158
+ webpackConfigAngular ( input ) ;
159
+
160
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
161
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
162
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 1 ) ;
163
+ expect ( angularCompilerOptions . platformTransformers [ 0 ] ) . toEqual ( FakeHmrTransformerFlag ) ;
164
+ } ) ;
165
+
166
+ it ( "should contain the Lazy transformer when the @angular/core is an external module" , ( ) => {
167
+ const input = getInput ( { platform, externals : [ "@angular/core" ] } ) ;
168
+
169
+ webpackConfigAngular ( input ) ;
170
+
171
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
172
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
173
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 1 ) ;
174
+ expect ( angularCompilerOptions . platformTransformers [ 0 ] ) . toEqual ( FakeLazyTransformerFlag ) ;
175
+ } ) ;
176
+
177
+ it ( "should contain the AOT + HMR transformers when the AOT and HMR flags are passed" , ( ) => {
178
+ const input = getInput ( { platform, aot : true , hmr : true } ) ;
179
+
180
+ webpackConfigAngular ( input ) ;
181
+
182
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
183
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
184
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 2 ) ;
185
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeAotTransformerFlag ) ;
186
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeHmrTransformerFlag ) ;
187
+ } ) ;
188
+
189
+ it ( "should set the AOT transformer before the HMR one when the AOT and HMR flags are passed" , ( ) => {
190
+ const input = getInput ( { platform, aot : true , hmr : true } ) ;
191
+
192
+ webpackConfigAngular ( input ) ;
193
+
194
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
195
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
196
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 2 ) ;
197
+ expect ( angularCompilerOptions . platformTransformers [ 0 ] ) . toEqual ( FakeAotTransformerFlag ) ;
198
+ expect ( angularCompilerOptions . platformTransformers [ 1 ] ) . toEqual ( FakeHmrTransformerFlag ) ;
199
+ } ) ;
200
+
201
+ it ( "should contain the AOT + Lazy transformers when the AOT flag is passed and @angular/core is an external module" , ( ) => {
202
+ const input = getInput ( { platform, aot : true , externals : [ "@angular/core" ] } ) ;
203
+
204
+ webpackConfigAngular ( input ) ;
205
+
206
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
207
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
208
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 2 ) ;
209
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeAotTransformerFlag ) ;
210
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeLazyTransformerFlag ) ;
211
+ } ) ;
212
+
213
+ it ( "should contain the HMR + Lazy transformers when the HMR flag is passed and @angular/core is an external module" , ( ) => {
214
+ const input = getInput ( { platform, hmr : true , externals : [ "@angular/core" ] } ) ;
215
+
216
+ webpackConfigAngular ( input ) ;
217
+
218
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
219
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
220
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 2 ) ;
221
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeHmrTransformerFlag ) ;
222
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeLazyTransformerFlag ) ;
223
+ } ) ;
224
+
225
+ it ( "should contain the AOT + HMR + Lazy transformers when the AOT and HMR flags are passed and @angular/core is an external module" , ( ) => {
226
+ const input = getInput ( { platform, aot : true , hmr : true , externals : [ "@angular/core" ] } ) ;
227
+
228
+ webpackConfigAngular ( input ) ;
229
+
230
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
231
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
232
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 3 ) ;
233
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeAotTransformerFlag ) ;
234
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeHmrTransformerFlag ) ;
235
+ expect ( angularCompilerOptions . platformTransformers ) . toContain ( FakeLazyTransformerFlag ) ;
236
+ } ) ;
237
+
238
+ it ( "should contain the AOT + HMR + Lazy transformers in the proper order when the AOT and HMR flags are passed and @angular/core is an external module" , ( ) => {
239
+ const input = getInput ( { platform, aot : true , hmr : true , externals : [ "@angular/core" ] } ) ;
240
+
241
+ webpackConfigAngular ( input ) ;
242
+
243
+ expect ( angularCompilerOptions ) . toBeDefined ( ) ;
244
+ expect ( angularCompilerOptions . platformTransformers ) . toBeDefined ( ) ;
245
+ expect ( angularCompilerOptions . platformTransformers . length ) . toEqual ( 3 ) ;
246
+ expect ( angularCompilerOptions . platformTransformers [ 0 ] ) . toEqual ( FakeAotTransformerFlag ) ;
247
+ expect ( angularCompilerOptions . platformTransformers [ 1 ] ) . toEqual ( FakeHmrTransformerFlag ) ;
248
+ expect ( angularCompilerOptions . platformTransformers [ 2 ] ) . toEqual ( FakeLazyTransformerFlag ) ;
249
+ } ) ;
250
+ } ) ;
251
+ } ) ;
113
252
} ) ;
0 commit comments