1
+ import { PresetOptions } from '@wpackio/babel-preset-base/lib/preset' ;
1
2
import miniCssExtractPlugin from 'mini-css-extract-plugin' ;
3
+ import webpack from 'webpack' ;
2
4
import {
3
5
ProjectConfig ,
4
6
projectConfigDefault ,
@@ -12,6 +14,39 @@ import {
12
14
WebpackConfigHelperConfig ,
13
15
} from '../../src/config/WebpackConfigHelper' ;
14
16
17
+ type babelLoaderModuleRules = {
18
+ use : {
19
+ loader : string ;
20
+ // tslint:disable-next-line:no-any
21
+ options ?: { [ x : string ] : any } ;
22
+ } [ ] ;
23
+ } [ ] ;
24
+
25
+ const findWpackIoBabelOnJs = (
26
+ modules : webpack . Module
27
+ ) : babelLoaderModuleRules => {
28
+ return modules . rules . filter ( rule => {
29
+ const { test } = rule ;
30
+
31
+ return test !== undefined && test . toString ( ) === '/\\.m?jsx?$/' ;
32
+ } ) as babelLoaderModuleRules ;
33
+ } ;
34
+ const findWpackIoBabelOnTs = (
35
+ modules : webpack . Module
36
+ ) : babelLoaderModuleRules => {
37
+ return modules . rules . filter ( rule => {
38
+ const { test } = rule ;
39
+
40
+ return test !== undefined && test . toString ( ) === '/\\.tsx?$/' ;
41
+ } ) as babelLoaderModuleRules ;
42
+ } ;
43
+
44
+ const findWpackIoBabelOnTJs = (
45
+ modules : webpack . Module
46
+ ) : babelLoaderModuleRules => {
47
+ return [ ...findWpackIoBabelOnJs ( modules ) , ...findWpackIoBabelOnTs ( modules ) ] ;
48
+ } ;
49
+
15
50
function getConfigFromProjectAndServer (
16
51
pCfg : ProjectConfig ,
17
52
sCfg : ServerConfig
@@ -41,7 +76,7 @@ function getConfigFromProjectAndServer(
41
76
// Create separate configuration for easy use within every test
42
77
let projectConfig : ProjectConfig ;
43
78
let serverConfig : ServerConfig ;
44
- beforeEach ( ( ) => {
79
+ const initConfig = ( ) : void => {
45
80
projectConfig = {
46
81
...projectConfigDefault ,
47
82
files : [
@@ -52,7 +87,8 @@ beforeEach(() => {
52
87
] ,
53
88
} ;
54
89
serverConfig = { ...serverConfigDefault } ;
55
- } ) ;
90
+ } ;
91
+ beforeEach ( initConfig ) ;
56
92
57
93
describe ( 'CreateWebPackConfig' , ( ) => {
58
94
// Now do the testing
@@ -174,35 +210,143 @@ describe('CreateWebPackConfig', () => {
174
210
175
211
// getModule()
176
212
describe ( 'getModule' , ( ) => {
177
- test ( 'has babel-loader for both typescript and javascript' , ( ) => {
178
- const cwc = new WebpackConfigHelper (
179
- projectConfig . files [ 0 ] ,
180
- getConfigFromProjectAndServer ( projectConfig , serverConfig ) ,
181
- '/foo/bar' ,
182
- true
183
- ) ;
184
- const modules = cwc . getModule ( ) ;
185
- if ( Array . isArray ( modules . rules ) ) {
186
- const jsTsRules = modules . rules . filter ( rule => {
187
- const { test } = rule ;
188
-
189
- return (
190
- test !== undefined &&
191
- ( test . toString ( ) === '/\\.m?jsx?$/' ||
192
- test . toString ( ) === '/\\.tsx?$/' )
193
- ) ;
194
- } ) as { use : { loader : string } [ ] } [ ] ;
195
- expect ( jsTsRules ) . toHaveLength ( 2 ) ;
196
- jsTsRules . forEach ( rule => {
197
- if ( rule && rule . use ) {
198
- expect ( rule . use [ 0 ] . loader ) . toBe ( 'babel-loader' ) ;
213
+ describe ( 'babel-loader for typescript and javascript' , ( ) => {
214
+ test ( 'has babel-loader' , ( ) => {
215
+ const cwc = new WebpackConfigHelper (
216
+ projectConfig . files [ 0 ] ,
217
+ getConfigFromProjectAndServer ( projectConfig , serverConfig ) ,
218
+ '/foo/bar' ,
219
+ true
220
+ ) ;
221
+ const modules = cwc . getModule ( ) ;
222
+ if ( Array . isArray ( modules . rules ) ) {
223
+ const jsTsRules = findWpackIoBabelOnTJs ( modules ) ;
224
+ expect ( jsTsRules ) . toHaveLength ( 2 ) ;
225
+ jsTsRules . forEach ( rule => {
226
+ if ( rule && rule . use ) {
227
+ expect ( rule . use [ 0 ] . loader ) . toBe ( 'babel-loader' ) ;
228
+ } else {
229
+ throw new Error ( 'JavaScript rule is undefined' ) ;
230
+ }
231
+ } ) ;
232
+ } else {
233
+ throw new Error ( 'Module is not an array' ) ;
234
+ }
235
+ } ) ;
236
+ test ( 'obeys hasFlow & hasRect' , ( ) => {
237
+ const cwc = new WebpackConfigHelper (
238
+ projectConfig . files [ 0 ] ,
239
+ {
240
+ ...getConfigFromProjectAndServer (
241
+ projectConfig ,
242
+ serverConfig
243
+ ) ,
244
+ hasFlow : true ,
245
+ hasReact : true ,
246
+ } ,
247
+ '/foo/bar' ,
248
+ true
249
+ ) ;
250
+ const modules = cwc . getModule ( ) ;
251
+ if ( Array . isArray ( modules . rules ) ) {
252
+ const jsRule = findWpackIoBabelOnJs ( modules ) ;
253
+ const tsRule = findWpackIoBabelOnTs ( modules ) ;
254
+ expect ( jsRule ) . toHaveLength ( 1 ) ;
255
+ expect ( tsRule ) . toHaveLength ( 1 ) ;
256
+ [ ...jsRule , ...tsRule ] . forEach ( rule => {
257
+ if ( rule && rule . use && rule . use [ 0 ] . options ) {
258
+ expect (
259
+ rule . use [ 0 ] . options . presets [ 0 ] [ 1 ]
260
+ ) . toMatchObject ( { hasReact : true } ) ;
261
+ } else {
262
+ throw new Error ( 'babel rule is undefined' ) ;
263
+ }
264
+ } ) ;
265
+ if ( jsRule [ 0 ] . use [ 0 ] . options ) {
266
+ expect ( jsRule [ 0 ] . use [ 0 ] . options . presets [ 1 ] ) . toEqual ( [
267
+ '@babel/preset-flow' ,
268
+ ] ) ;
199
269
} else {
200
- throw new Error ( 'JavaScript rule is undefined' ) ;
270
+ throw new Error (
271
+ 'JavaScript babel-loader options not present'
272
+ ) ;
201
273
}
202
- } ) ;
203
- } else {
204
- throw new Error ( 'Module is not an array' ) ;
205
- }
274
+ } else {
275
+ throw new Error ( 'Module is not an array' ) ;
276
+ }
277
+ } ) ;
278
+
279
+ test ( 'overrides @wpackio/babel-preset-base from config' , ( ) => {
280
+ const override : PresetOptions = {
281
+ noImportMeta : true ,
282
+ presetEnv : { modules : 'umd' } ,
283
+ presetReact : { pragma : 'wp.createElement' } ,
284
+ } ;
285
+ const cwc = new WebpackConfigHelper (
286
+ projectConfig . files [ 0 ] ,
287
+ {
288
+ ...getConfigFromProjectAndServer (
289
+ projectConfig ,
290
+ serverConfig
291
+ ) ,
292
+ jsBabelPresetOptions : override ,
293
+ tsBabelPresetOptions : override ,
294
+ } ,
295
+ '/foo/bar' ,
296
+ true
297
+ ) ;
298
+
299
+ const modules = cwc . getModule ( ) ;
300
+ if ( Array . isArray ( modules . rules ) ) {
301
+ const jsTsRules = findWpackIoBabelOnTJs ( modules ) ;
302
+ expect ( jsTsRules ) . toHaveLength ( 2 ) ;
303
+ jsTsRules . forEach ( rule => {
304
+ if ( rule && rule . use && rule . use [ 0 ] . options ) {
305
+ expect (
306
+ rule . use [ 0 ] . options . presets [ 0 ] [ 1 ]
307
+ ) . toMatchObject ( override ) ;
308
+ } else {
309
+ throw new Error ( 'JavaScript rule is undefined' ) ;
310
+ }
311
+ } ) ;
312
+ } else {
313
+ throw new Error ( 'Module is not an array' ) ;
314
+ }
315
+ } ) ;
316
+
317
+ test ( 'overrides all babel-loader options from config' , ( ) => {
318
+ const override : webpack . RuleSetLoader [ 'options' ] = {
319
+ presets : 'foo' ,
320
+ plugins : [ 'bar' , 'baz' ] ,
321
+ } ;
322
+ const cwc = new WebpackConfigHelper (
323
+ projectConfig . files [ 0 ] ,
324
+ {
325
+ ...getConfigFromProjectAndServer (
326
+ projectConfig ,
327
+ serverConfig
328
+ ) ,
329
+ jsBabelOverride : override ,
330
+ tsBabelOverride : override ,
331
+ } ,
332
+ '/foo/bar' ,
333
+ true
334
+ ) ;
335
+ const modules = cwc . getModule ( ) ;
336
+ if ( Array . isArray ( modules . rules ) ) {
337
+ const jsTsRules = findWpackIoBabelOnTJs ( modules ) ;
338
+ expect ( jsTsRules ) . toHaveLength ( 2 ) ;
339
+ jsTsRules . forEach ( rule => {
340
+ if ( rule && rule . use && rule . use [ 0 ] . options ) {
341
+ expect ( rule . use [ 0 ] . options ) . toMatchObject ( override ) ;
342
+ } else {
343
+ throw new Error ( 'JavaScript rule is undefined' ) ;
344
+ }
345
+ } ) ;
346
+ } else {
347
+ throw new Error ( 'Module is not an array' ) ;
348
+ }
349
+ } ) ;
206
350
} ) ;
207
351
208
352
test ( 'uses style loader when in dev mode' , ( ) => {
0 commit comments