1
- const path = require ( 'path' ) ;
2
- const fs = require ( 'fs' ) ;
3
- const os = require ( 'os' ) ;
4
-
5
1
const webpack = require ( 'webpack' ) ;
6
2
const merge = require ( 'webpack-merge' ) ;
7
3
8
- class KarmaSyncPlugin {
9
- constructor ( options ) {
10
- this . karmaEmitter = options . karmaEmitter ;
11
- this . controller = options . controller ;
12
- }
4
+ const KW_WebpackPlugin = require ( '../webpack/plugin' ) ;
5
+ const DefaultWebpackOptionsFactory = require ( '../webpack/defaults' ) ;
13
6
14
- apply ( compiler ) {
15
- this . compiler = compiler ;
16
-
17
- // webpack bundles are finished
18
- compiler . hooks . done . tap ( 'KarmaSyncPlugin' , async ( stats ) => {
19
- // read generated file content and store for karma preprocessor
20
- this . controller . bundlesContent = { } ;
21
- stats . toJson ( ) . assets . forEach ( ( webpackFileObj ) => {
22
- const filePath = `${ compiler . options . output . path } /${ webpackFileObj . name } ` ;
23
- this . controller . bundlesContent [ webpackFileObj . name ] = fs . readFileSync (
24
- filePath ,
25
- 'utf-8'
26
- ) ;
27
- } ) ;
28
-
29
- // karma refresh
30
- this . karmaEmitter . refreshFiles ( ) ;
31
- } ) ;
7
+ class KW_Controller {
8
+ constructor ( ) {
9
+ this . isActive = false ;
10
+ this . bundlesContent = { } ;
11
+ this . hasBeenBuiltAtLeastOnce = false ;
12
+ this . webpackOptions = DefaultWebpackOptionsFactory . create ( ) ;
32
13
}
33
- }
34
14
35
- const defaultWebpackOptions = {
36
- mode : 'development' ,
37
- output : {
38
- filename : '[name].js' ,
39
- // eslint-disable-next-line prettier/prettier
40
- path : path . join ( os . tmpdir ( ) , '_karma_webpack_' ) + Math . floor ( Math . random ( ) * 1000000 ) ,
41
- } ,
42
- stats : {
43
- modules : false ,
44
- colors : true ,
45
- } ,
46
- watch : false ,
47
- optimization : {
48
- runtimeChunk : 'single' ,
49
- splitChunks : {
50
- chunks : 'all' ,
51
- minSize : 0 ,
52
- cacheGroups : {
53
- commons : {
54
- name : 'commons' ,
55
- chunks : 'all' ,
56
- minChunks : 1 ,
57
- } ,
58
- } ,
59
- } ,
60
- } ,
61
- plugins : [ ] ,
62
- // Something like this will be auto added by this.configure()
63
- // entry: {
64
- // 'foo-one.test.js': 'path/to/test/foo-one.test.js',
65
- // 'foo-two.test.js': 'path/to/test/foo-two.test.js',
66
- // },
67
- // plugins: [
68
- // new KarmaSyncPlugin()
69
- // ],
70
- } ;
71
-
72
- class KarmaWebpackController {
73
15
set webpackOptions ( options ) {
74
16
this . __webpackOptions = options ;
75
17
}
@@ -78,11 +20,15 @@ class KarmaWebpackController {
78
20
return this . __webpackOptions ;
79
21
}
80
22
23
+ updateWebpackOptions ( newOptions ) {
24
+ this . webpackOptions = merge ( this . webpackOptions , newOptions ) ;
25
+ }
26
+
81
27
set karmaEmitter ( emitter ) {
82
28
this . __karmaEmitter = emitter ;
83
29
84
30
this . __webpackOptions . plugins . push (
85
- new KarmaSyncPlugin ( {
31
+ new KW_WebpackPlugin ( {
86
32
karmaEmitter : emitter ,
87
33
controller : this ,
88
34
} )
@@ -97,13 +43,6 @@ class KarmaWebpackController {
97
43
return this . webpackOptions . output . path ;
98
44
}
99
45
100
- constructor ( ) {
101
- this . isActive = false ;
102
- this . bundlesContent = { } ;
103
- this . hasBeenBuiltAtLeastOnce = false ;
104
- this . webpackOptions = defaultWebpackOptions ;
105
- }
106
-
107
46
setupExitHandler ( compiler ) {
108
47
this . karmaEmitter . once ( 'exit' , ( done ) => {
109
48
compiler . close ( ( ) => {
@@ -113,10 +52,6 @@ class KarmaWebpackController {
113
52
} ) ;
114
53
}
115
54
116
- updateWebpackOptions ( newOptions ) {
117
- this . webpackOptions = merge ( this . webpackOptions , newOptions ) ;
118
- }
119
-
120
55
async bundle ( ) {
121
56
if ( this . isActive === false && this . hasBeenBuiltAtLeastOnce === false ) {
122
57
console . log ( 'Webpack bundling...' ) ;
@@ -169,8 +104,4 @@ class KarmaWebpackController {
169
104
}
170
105
}
171
106
172
- module . exports = {
173
- KarmaSyncPlugin,
174
- KarmaWebpackController,
175
- defaultWebpackOptions,
176
- } ;
107
+ module . exports = KW_Controller ;
0 commit comments