@@ -10,36 +10,74 @@ var webpack = require('webpack');
10
10
var rimraf = require ( 'rimraf' ) ;
11
11
var WebpackRecompilationSimulator = require ( 'webpack-recompilation-simulator' ) ;
12
12
var HtmlWebpackPlugin = require ( '../index.js' ) ;
13
- var webpackMajorVersion = require ( 'webpack/package.json' ) . version . split ( '.' ) [ 0 ] ;
14
13
15
14
var OUTPUT_DIR = path . join ( __dirname , '../dist/caching-spec' ) ;
16
15
17
16
jest . setTimeout ( 30000 ) ;
18
17
process . on ( 'unhandledRejection' , r => console . log ( r ) ) ;
18
+ process . traceDeprecation = true ;
19
19
20
20
function setUpCompiler ( htmlWebpackPlugin ) {
21
21
jest . spyOn ( htmlWebpackPlugin , 'evaluateCompilationResult' ) ;
22
22
var webpackConfig = {
23
+ stats : { all : true } ,
24
+ // Caching works only in development
25
+ mode : 'development' ,
23
26
entry : path . join ( __dirname , 'fixtures/index.js' ) ,
27
+ module : {
28
+ rules : [
29
+ {
30
+ test : / \. h t m l $ / ,
31
+ loader : require . resolve ( '../lib/loader.js' ) ,
32
+ options : {
33
+ force : true
34
+ }
35
+ }
36
+ ]
37
+ } ,
24
38
output : {
25
39
path : OUTPUT_DIR ,
26
40
filename : 'index_bundle.js'
27
41
} ,
28
42
plugins : [ htmlWebpackPlugin ]
29
43
} ;
30
- if ( Number ( webpackMajorVersion ) >= 4 ) {
31
- webpackConfig . mode = 'development' ;
32
- }
33
44
var compiler = new WebpackRecompilationSimulator ( webpack ( webpackConfig ) ) ;
34
45
return compiler ;
35
46
}
36
47
37
- function getCompiledModuleCount ( statsJson ) {
38
- return statsJson . modules . filter ( function ( webpackModule ) {
48
+ function getCompiledModules ( statsJson ) {
49
+ const builtModules = statsJson . modules . filter ( function ( webpackModule ) {
39
50
return webpackModule . built ;
40
- } ) . length + statsJson . children . reduce ( function ( sum , childCompilationStats ) {
41
- return sum + getCompiledModuleCount ( childCompilationStats ) ;
42
- } , 0 ) ;
51
+ } ) . map ( ( webpackModule ) => {
52
+ return module . userRequest ;
53
+ } ) ;
54
+ statsJson . children . forEach ( ( childCompilationStats ) => {
55
+ const builtChildModules = getCompiledModules ( childCompilationStats ) ;
56
+ Array . prototype . push . apply ( builtModules , builtChildModules ) ;
57
+ } ) ;
58
+ return builtModules ;
59
+ }
60
+
61
+ function getCompiledModuleCount ( statsJson ) {
62
+ return getCompiledModules ( statsJson ) . length ;
63
+ }
64
+
65
+ function expectNoErrors ( stats ) {
66
+ const errors = {
67
+ main : stats . compilation . errors ,
68
+ childCompilation : [ ]
69
+ } ;
70
+ stats . compilation . children . forEach ( ( child ) => {
71
+ Array . prototype . push . apply ( errors . childCompilation , child . errors ) ;
72
+ } ) ;
73
+ if ( errors . main . length ) {
74
+ errors . main . forEach ( ( error ) => {
75
+ console . log ( 'Error => ' , error ) ;
76
+ } ) ;
77
+ console . dir ( stats . toJson ( { errorDetails : true , moduleTrace : true } ) , { depth : 5 } ) ;
78
+ }
79
+ expect ( errors . main ) . toEqual ( [ ] ) ;
80
+ expect ( errors . childCompilation ) . toEqual ( [ ] ) ;
43
81
}
44
82
45
83
describe ( 'HtmlWebpackPluginCaching' , function ( ) {
@@ -54,16 +92,19 @@ describe('HtmlWebpackPluginCaching', function () {
54
92
} ) ;
55
93
var childCompilerHash ;
56
94
var compiler = setUpCompiler ( htmlWebpackPlugin ) ;
95
+ compiler . addTestFile ( path . join ( __dirname , 'fixtures/index.js' ) ) ;
57
96
compiler . run ( )
58
97
// Change the template file and compile again
59
98
. then ( function ( ) {
60
99
childCompilerHash = htmlWebpackPlugin . childCompilerHash ;
61
100
return compiler . run ( ) ;
62
101
} )
63
102
. then ( function ( stats ) {
103
+ // Expect no errors:
104
+ expectNoErrors ( stats ) ;
64
105
// Verify that no file was built
65
- expect ( getCompiledModuleCount ( stats . toJson ( ) ) )
66
- . toBe ( 0 ) ;
106
+ expect ( getCompiledModules ( stats . toJson ( ) ) )
107
+ . toEqual ( [ ] ) ;
67
108
// Verify that the html was processed only during the inital build
68
109
expect ( htmlWebpackPlugin . evaluateCompilationResult . mock . calls . length )
69
110
. toBe ( 1 ) ;
@@ -78,6 +119,7 @@ describe('HtmlWebpackPluginCaching', function () {
78
119
var htmlWebpackPlugin = new HtmlWebpackPlugin ( ) ;
79
120
var compiler = setUpCompiler ( htmlWebpackPlugin ) ;
80
121
var childCompilerHash ;
122
+ compiler . addTestFile ( path . join ( __dirname , 'fixtures/index.js' ) ) ;
81
123
compiler . run ( )
82
124
// Change a js file and compile again
83
125
. then ( function ( ) {
@@ -86,6 +128,8 @@ describe('HtmlWebpackPluginCaching', function () {
86
128
return compiler . run ( ) ;
87
129
} )
88
130
. then ( function ( stats ) {
131
+ // Expect no errors:
132
+ expectNoErrors ( stats ) ;
89
133
// Verify that only one file was built
90
134
expect ( getCompiledModuleCount ( stats . toJson ( ) ) )
91
135
. toBe ( 1 ) ;
@@ -105,6 +149,7 @@ describe('HtmlWebpackPluginCaching', function () {
105
149
} ) ;
106
150
var childCompilerHash ;
107
151
var compiler = setUpCompiler ( htmlWebpackPlugin ) ;
152
+ compiler . addTestFile ( path . join ( __dirname , 'fixtures/index.js' ) ) ;
108
153
compiler . run ( )
109
154
// Change a js file and compile again
110
155
. then ( function ( ) {
@@ -113,6 +158,8 @@ describe('HtmlWebpackPluginCaching', function () {
113
158
return compiler . run ( ) ;
114
159
} )
115
160
. then ( function ( stats ) {
161
+ // Expect no errors:
162
+ expectNoErrors ( stats ) ;
116
163
// Verify that only one file was built
117
164
expect ( getCompiledModuleCount ( stats . toJson ( ) ) )
118
165
. toBe ( 1 ) ;
@@ -133,6 +180,7 @@ describe('HtmlWebpackPluginCaching', function () {
133
180
} ) ;
134
181
var childCompilerHash ;
135
182
var compiler = setUpCompiler ( htmlWebpackPlugin ) ;
183
+ compiler . addTestFile ( template ) ;
136
184
compiler . run ( )
137
185
// Change the template file and compile again
138
186
. then ( function ( ) {
@@ -141,6 +189,8 @@ describe('HtmlWebpackPluginCaching', function () {
141
189
return compiler . run ( ) ;
142
190
} )
143
191
. then ( function ( stats ) {
192
+ // Expect no errors:
193
+ expectNoErrors ( stats ) ;
144
194
// Verify that only one file was built
145
195
expect ( getCompiledModuleCount ( stats . toJson ( ) ) )
146
196
. toBe ( 1 ) ;
0 commit comments