/
caching.spec.js
288 lines (270 loc) · 10.1 KB
/
caching.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Integration tests for caching
*/
/* eslint-env jest */
'use strict';
const path = require('path');
const webpack = require('webpack');
const rimraf = require('rimraf');
const WebpackRecompilationSimulator = require('webpack-recompilation-simulator');
const HtmlWebpackPlugin = require('../index.js');
const OUTPUT_DIR = path.join(__dirname, '../dist/caching-spec');
jest.setTimeout(30000);
process.on('unhandledRejection', r => console.log(r));
process.traceDeprecation = true;
function setUpCompiler (htmlWebpackPlugin) {
jest.spyOn(htmlWebpackPlugin, 'evaluateCompilationResult');
const webpackConfig = {
stats: { all: true },
// Caching works only in development
mode: 'development',
entry: path.join(__dirname, 'fixtures/index.js'),
module: {
rules: [
{
test: /\.html$/,
loader: require.resolve('../lib/loader.js'),
options: {
force: true
}
}
]
},
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: Array.from(arguments)
};
const compiler = new WebpackRecompilationSimulator(webpack(webpackConfig));
return compiler;
}
function getCompiledModules (statsJson) {
const builtModules = statsJson.modules.filter(webpackModule => webpackModule.built).map((webpackModule) => {
return module.userRequest;
});
statsJson.children.forEach((childCompilationStats) => {
const builtChildModules = getCompiledModules(childCompilationStats);
Array.prototype.push.apply(builtModules, builtChildModules);
});
return builtModules;
}
function getCompiledModuleCount (statsJson) {
return getCompiledModules(statsJson).length;
}
function expectNoErrors (stats) {
const errors = {
main: stats.compilation.errors,
childCompilation: []
};
stats.compilation.children.forEach((child) => {
Array.prototype.push.apply(errors.childCompilation, child.errors);
});
if (errors.main.length) {
errors.main.forEach((error) => {
console.log('Error => ', error);
});
console.dir(stats.toJson({ errorDetails: true, moduleTrace: true }), { depth: 5 });
}
expect(errors.main).toEqual([]);
expect(errors.childCompilation).toEqual([]);
}
describe('HtmlWebpackPluginCaching', () => {
beforeEach(done => {
rimraf(OUTPUT_DIR, done);
});
it('should compile nothing if no file was changed', done => {
const template = path.join(__dirname, 'fixtures/plain.html');
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: template
});
let childCompilerHash;
const compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change the template file and compile again
.then(() => {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
return compiler.run();
})
.then(stats => {
// Expect no errors:
expectNoErrors(stats);
// Verify that no file was built
expect(getCompiledModules(stats.toJson()))
.toEqual([]);
// Verify that the html was processed only during the inital build
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(1);
// Verify that the child compilation was executed twice
expect(htmlWebpackPlugin.childCompilerHash)
.toBe(childCompilerHash);
})
.then(done);
});
it('should not compile the webpack html file if only a javascript file was changed', done => {
const htmlWebpackPlugin = new HtmlWebpackPlugin();
const compiler = setUpCompiler(htmlWebpackPlugin);
let childCompilerHash;
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change a js file and compile again
.then(() => {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), { footer: '//1' });
return compiler.run();
})
.then(stats => {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
// Verify that the html was processed only during the inital build
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(1);
// Verify that the child compilation was executed only once
expect(htmlWebpackPlugin.childCompilerHash)
.toBe(childCompilerHash);
})
.then(done);
});
it('should compile the webpack html file even if only a javascript file was changed if caching is disabled', done => {
const htmlWebpackPlugin = new HtmlWebpackPlugin({
cache: false
});
let childCompilerHash;
const compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change a js file and compile again
.then(() => {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), { footer: '//1' });
return compiler.run();
})
.then(stats => {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
// Verify that the html was processed on every run
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(2);
// Verify that the child compilation was executed only once
expect(htmlWebpackPlugin.childCompilerHash)
.toBe(childCompilerHash);
})
.then(done);
});
it('should compile the webpack html if the template file was changed', done => {
const template = path.join(__dirname, 'fixtures/plain.html');
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: template
});
let childCompilerHash;
const compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(template);
compiler.run()
// Change the template file and compile again
.then(() => {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
compiler.simulateFileChange(template, { footer: '<!-- 1 -->' });
return compiler.run();
})
.then(stats => {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
// Verify that the html was processed twice
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(2);
// Verify that the child compilation was executed twice
expect(htmlWebpackPlugin.childCompilerHash)
.not.toBe(childCompilerHash);
})
.then(done);
});
it('should not slow down linear (10 plugins should not take twice as much time as a 1 plugin)', done => {
const template = path.join(__dirname, 'fixtures/plain.html');
const createHtmlWebpackPlugin = () => new HtmlWebpackPlugin({
template: template
});
let singlePluginCompileStart;
let singleCompileRunDuration;
let multiPluginComileStart;
let multiCompileRunDuration;
let singleCompiler = setUpCompiler(createHtmlWebpackPlugin());
let multiCompiler = setUpCompiler.apply(null, Array(10).fill(0).map(() => createHtmlWebpackPlugin()));
Promise.resolve()
.then(function singleCompileRun () {
singlePluginCompileStart = process.hrtime();
return singleCompiler.run()
// Change the template file and compile again
.then(() => {
singleCompileRunDuration = process.hrtime(singlePluginCompileStart);
});
})
.then(function multiCompileRun () {
multiPluginComileStart = process.hrtime();
return multiCompiler.run()
// Change the template file and compile again
.then(() => {
multiCompileRunDuration = process.hrtime(multiPluginComileStart);
});
}).then(function meassureTime () {
const singleCompileRunDurationInNs = singleCompileRunDuration[0] * 1e9 + singleCompileRunDuration[1];
const multiCompileRunDurationInNs = multiCompileRunDuration[0] * 1e9 + multiCompileRunDuration[1];
const speedComarision = multiCompileRunDurationInNs / singleCompileRunDurationInNs * 100;
expect(speedComarision).toBeLessThan(200);
done();
});
});
it('should keep watching the webpack html if only a js file was changed', done => {
const template = path.join(__dirname, 'fixtures/plain.html');
const jsFile = path.join(__dirname, 'fixtures/index.js');
const htmlWebpackPlugin = new HtmlWebpackPlugin({
template: template
});
const compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(template);
compiler.addTestFile(jsFile);
// Build the template file for the first time
compiler.startWatching()
// Change the template file (second build)
.then(() => {
compiler.simulateFileChange(template, { footer: '<!-- 1 -->' });
return compiler.waitForWatchRunComplete();
})
// Change js
.then(() => {
compiler.simulateFileChange(jsFile, { footer: '// 1' });
return compiler.waitForWatchRunComplete();
})
// Change js
.then(() => {
compiler.simulateFileChange(jsFile, { footer: '// 2' });
return compiler.waitForWatchRunComplete();
})
// Change js
.then(() => {
compiler.simulateFileChange(jsFile, { footer: '// 3' });
return compiler.waitForWatchRunComplete();
})
// Change the template file (third build)
.then(() => {
compiler.simulateFileChange(template, { footer: '<!-- 2 -->' });
return compiler.waitForWatchRunComplete();
})
.then(() => {
// Verify that the html was processed trice
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(3);
})
.then(() => compiler.stopWatching())
.then(done);
});
});