html-webpack-plugin 5.5.4 → 5.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  * @file
4
4
  * Helper plugin manages the cached state of the child compilation
5
5
  *
6
- * To optimize performance the child compilation is running asyncronously.
6
+ * To optimize performance the child compilation is running asynchronously.
7
7
  * Therefore it needs to be started in the compiler.make phase and ends after
8
8
  * the compilation.afterCompile phase.
9
9
  *
@@ -21,7 +21,7 @@
21
21
  });
22
22
  * ```
23
23
  */
24
- 'use strict';
24
+ "use strict";
25
25
 
26
26
  // Import types
27
27
  /** @typedef {import("webpack").Compiler} Compiler */
@@ -37,7 +37,7 @@
37
37
  error: Error
38
38
  }} ChildCompilationResult */
39
39
 
40
- const { HtmlWebpackChildCompiler } = require('./child-compiler');
40
+ const { HtmlWebpackChildCompiler } = require("./child-compiler");
41
41
 
42
42
  /**
43
43
  * This plugin is a singleton for performance reasons.
@@ -51,7 +51,7 @@ class CachedChildCompilation {
51
51
  /**
52
52
  * @param {Compiler} compiler
53
53
  */
54
- constructor (compiler) {
54
+ constructor(compiler) {
55
55
  /**
56
56
  * @private
57
57
  * @type {Compiler}
@@ -62,7 +62,8 @@ class CachedChildCompilation {
62
62
  if (compilerMap.has(compiler)) {
63
63
  return;
64
64
  }
65
- const persistentChildCompilerSingletonPlugin = new PersistentChildCompilerSingletonPlugin();
65
+ const persistentChildCompilerSingletonPlugin =
66
+ new PersistentChildCompilerSingletonPlugin();
66
67
  compilerMap.set(compiler, persistentChildCompilerSingletonPlugin);
67
68
  persistentChildCompilerSingletonPlugin.apply(compiler);
68
69
  }
@@ -71,21 +72,25 @@ class CachedChildCompilation {
71
72
  * apply is called by the webpack main compiler during the start phase
72
73
  * @param {string} entry
73
74
  */
74
- addEntry (entry) {
75
- const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
75
+ addEntry(entry) {
76
+ const persistentChildCompilerSingletonPlugin = compilerMap.get(
77
+ this.compiler,
78
+ );
76
79
  if (!persistentChildCompilerSingletonPlugin) {
77
80
  throw new Error(
78
- 'PersistentChildCompilerSingletonPlugin instance not found.'
81
+ "PersistentChildCompilerSingletonPlugin instance not found.",
79
82
  );
80
83
  }
81
84
  persistentChildCompilerSingletonPlugin.addEntry(entry);
82
85
  }
83
86
 
84
- getCompilationResult () {
85
- const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
87
+ getCompilationResult() {
88
+ const persistentChildCompilerSingletonPlugin = compilerMap.get(
89
+ this.compiler,
90
+ );
86
91
  if (!persistentChildCompilerSingletonPlugin) {
87
92
  throw new Error(
88
- 'PersistentChildCompilerSingletonPlugin instance not found.'
93
+ "PersistentChildCompilerSingletonPlugin instance not found.",
89
94
  );
90
95
  }
91
96
  return persistentChildCompilerSingletonPlugin.getLatestResult();
@@ -99,16 +104,18 @@ class CachedChildCompilation {
99
104
  | { mainCompilationHash: string, compiledEntry: ChildCompilationTemplateResult }
100
105
  }
101
106
  */
102
- getCompilationEntryResult (entry) {
107
+ getCompilationEntryResult(entry) {
103
108
  const latestResult = this.getCompilationResult();
104
109
  const compilationResult = latestResult.compilationResult;
105
- return 'error' in compilationResult ? {
106
- mainCompilationHash: latestResult.mainCompilationHash,
107
- error: compilationResult.error
108
- } : {
109
- mainCompilationHash: latestResult.mainCompilationHash,
110
- compiledEntry: compilationResult.compiledEntries[entry]
111
- };
110
+ return "error" in compilationResult
111
+ ? {
112
+ mainCompilationHash: latestResult.mainCompilationHash,
113
+ error: compilationResult.error,
114
+ }
115
+ : {
116
+ mainCompilationHash: latestResult.mainCompilationHash,
117
+ compiledEntry: compilationResult.compiledEntries[entry],
118
+ };
112
119
  }
113
120
  }
114
121
 
@@ -119,7 +126,7 @@ class PersistentChildCompilerSingletonPlugin {
119
126
  * @param {Compilation} mainCompilation
120
127
  * @param {number} startTime
121
128
  */
122
- static createSnapshot (fileDependencies, mainCompilation, startTime) {
129
+ static createSnapshot(fileDependencies, mainCompilation, startTime) {
123
130
  return new Promise((resolve, reject) => {
124
131
  mainCompilation.fileSystemInfo.createSnapshot(
125
132
  startTime,
@@ -133,7 +140,7 @@ class PersistentChildCompilerSingletonPlugin {
133
140
  return reject(err);
134
141
  }
135
142
  resolve(snapshot);
136
- }
143
+ },
137
144
  );
138
145
  });
139
146
  }
@@ -146,7 +153,7 @@ class PersistentChildCompilerSingletonPlugin {
146
153
  * @param {Compilation} mainCompilation
147
154
  * @returns {Promise<boolean | undefined>}
148
155
  */
149
- static isSnapshotValid (snapshot, mainCompilation) {
156
+ static isSnapshotValid(snapshot, mainCompilation) {
150
157
  return new Promise((resolve, reject) => {
151
158
  mainCompilation.fileSystemInfo.checkSnapshotValid(
152
159
  snapshot,
@@ -155,20 +162,20 @@ class PersistentChildCompilerSingletonPlugin {
155
162
  reject(err);
156
163
  }
157
164
  resolve(isValid);
158
- }
165
+ },
159
166
  );
160
167
  });
161
168
  }
162
169
 
163
- static watchFiles (mainCompilation, fileDependencies) {
164
- Object.keys(fileDependencies).forEach((depencyTypes) => {
165
- fileDependencies[depencyTypes].forEach(fileDependency => {
166
- mainCompilation[depencyTypes].add(fileDependency);
170
+ static watchFiles(mainCompilation, fileDependencies) {
171
+ Object.keys(fileDependencies).forEach((dependencyType) => {
172
+ fileDependencies[dependencyType].forEach((fileDependency) => {
173
+ mainCompilation[dependencyType].add(fileDependency);
167
174
  });
168
175
  });
169
176
  }
170
177
 
171
- constructor () {
178
+ constructor() {
172
179
  /**
173
180
  * @private
174
181
  * @type {
@@ -198,15 +205,15 @@ class PersistentChildCompilerSingletonPlugin {
198
205
  isVerifyingCache: false,
199
206
  entries: [],
200
207
  compiledEntries: [],
201
- mainCompilationHash: 'initial',
208
+ mainCompilationHash: "initial",
202
209
  compilationResult: {
203
210
  dependencies: {
204
211
  fileDependencies: [],
205
212
  contextDependencies: [],
206
- missingDependencies: []
213
+ missingDependencies: [],
207
214
  },
208
- compiledEntries: {}
209
- }
215
+ compiledEntries: {},
216
+ },
210
217
  };
211
218
  }
212
219
 
@@ -214,31 +221,34 @@ class PersistentChildCompilerSingletonPlugin {
214
221
  * apply is called by the webpack main compiler during the start phase
215
222
  * @param {Compiler} compiler
216
223
  */
217
- apply (compiler) {
224
+ apply(compiler) {
218
225
  /** @type Promise<ChildCompilationResult> */
219
226
  let childCompilationResultPromise = Promise.resolve({
220
227
  dependencies: {
221
228
  fileDependencies: [],
222
229
  contextDependencies: [],
223
- missingDependencies: []
230
+ missingDependencies: [],
224
231
  },
225
- compiledEntries: {}
232
+ compiledEntries: {},
226
233
  });
227
234
  /**
228
235
  * The main compilation hash which will only be updated
229
236
  * if the childCompiler changes
230
237
  */
231
238
  /** @type {string} */
232
- let mainCompilationHashOfLastChildRecompile = '';
239
+ let mainCompilationHashOfLastChildRecompile = "";
233
240
  /** @type {Snapshot | undefined} */
234
241
  let previousFileSystemSnapshot;
235
242
  let compilationStartTime = new Date().getTime();
236
243
 
237
244
  compiler.hooks.make.tapAsync(
238
- 'PersistentChildCompilerSingletonPlugin',
245
+ "PersistentChildCompilerSingletonPlugin",
239
246
  (mainCompilation, callback) => {
240
- if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
241
- return callback(new Error('Child compilation has already started'));
247
+ if (
248
+ this.compilationState.isCompiling ||
249
+ this.compilationState.isVerifyingCache
250
+ ) {
251
+ return callback(new Error("Child compilation has already started"));
242
252
  }
243
253
 
244
254
  // Update the time to the current compile start time
@@ -250,83 +260,101 @@ class PersistentChildCompilerSingletonPlugin {
250
260
  isVerifyingCache: true,
251
261
  previousEntries: this.compilationState.compiledEntries,
252
262
  previousResult: this.compilationState.compilationResult,
253
- entries: this.compilationState.entries
263
+ entries: this.compilationState.entries,
254
264
  };
255
265
 
256
266
  // Validate cache:
257
- const isCacheValidPromise = this.isCacheValid(previousFileSystemSnapshot, mainCompilation);
267
+ const isCacheValidPromise = this.isCacheValid(
268
+ previousFileSystemSnapshot,
269
+ mainCompilation,
270
+ );
258
271
 
259
272
  let cachedResult = childCompilationResultPromise;
260
- childCompilationResultPromise = isCacheValidPromise.then((isCacheValid) => {
261
- // Reuse cache
262
- if (isCacheValid) {
263
- return cachedResult;
264
- }
265
- // Start the compilation
266
- const compiledEntriesPromise = this.compileEntries(
267
- mainCompilation,
268
- this.compilationState.entries
269
- );
270
- // Update snapshot as soon as we know the filedependencies
271
- // this might possibly cause bugs if files were changed inbetween
272
- // compilation start and snapshot creation
273
- compiledEntriesPromise.then((childCompilationResult) => {
274
- return PersistentChildCompilerSingletonPlugin.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
275
- }).then((snapshot) => {
276
- previousFileSystemSnapshot = snapshot;
277
- });
278
- return compiledEntriesPromise;
279
- });
273
+ childCompilationResultPromise = isCacheValidPromise.then(
274
+ (isCacheValid) => {
275
+ // Reuse cache
276
+ if (isCacheValid) {
277
+ return cachedResult;
278
+ }
279
+ // Start the compilation
280
+ const compiledEntriesPromise = this.compileEntries(
281
+ mainCompilation,
282
+ this.compilationState.entries,
283
+ );
284
+ // Update snapshot as soon as we know the fileDependencies
285
+ // this might possibly cause bugs if files were changed between
286
+ // compilation start and snapshot creation
287
+ compiledEntriesPromise
288
+ .then((childCompilationResult) => {
289
+ return PersistentChildCompilerSingletonPlugin.createSnapshot(
290
+ childCompilationResult.dependencies,
291
+ mainCompilation,
292
+ compilationStartTime,
293
+ );
294
+ })
295
+ .then((snapshot) => {
296
+ previousFileSystemSnapshot = snapshot;
297
+ });
298
+ return compiledEntriesPromise;
299
+ },
300
+ );
280
301
 
281
302
  // Add files to compilation which needs to be watched:
282
303
  mainCompilation.hooks.optimizeTree.tapAsync(
283
- 'PersistentChildCompilerSingletonPlugin',
304
+ "PersistentChildCompilerSingletonPlugin",
284
305
  (chunks, modules, callback) => {
285
- const handleCompilationDonePromise = childCompilationResultPromise.then(
286
- childCompilationResult => {
306
+ const handleCompilationDonePromise =
307
+ childCompilationResultPromise.then((childCompilationResult) => {
287
308
  this.watchFiles(
288
309
  mainCompilation,
289
- childCompilationResult.dependencies
310
+ childCompilationResult.dependencies,
290
311
  );
291
312
  });
292
- // @ts-ignore
293
- handleCompilationDonePromise.then(() => callback(null, chunks, modules), callback);
294
- }
313
+ handleCompilationDonePromise.then(
314
+ // @ts-ignore
315
+ () => callback(null, chunks, modules),
316
+ callback,
317
+ );
318
+ },
295
319
  );
296
320
 
297
321
  // Store the final compilation once the main compilation hash is known
298
322
  mainCompilation.hooks.additionalAssets.tapAsync(
299
- 'PersistentChildCompilerSingletonPlugin',
323
+ "PersistentChildCompilerSingletonPlugin",
300
324
  (callback) => {
301
- const didRecompilePromise = Promise.all([childCompilationResultPromise, cachedResult]).then(
302
- ([childCompilationResult, cachedResult]) => {
303
- // Update if childCompilation changed
304
- return (cachedResult !== childCompilationResult);
305
- }
306
- );
325
+ const didRecompilePromise = Promise.all([
326
+ childCompilationResultPromise,
327
+ cachedResult,
328
+ ]).then(([childCompilationResult, cachedResult]) => {
329
+ // Update if childCompilation changed
330
+ return cachedResult !== childCompilationResult;
331
+ });
307
332
 
308
- const handleCompilationDonePromise = Promise.all([childCompilationResultPromise, didRecompilePromise]).then(
309
- ([childCompilationResult, didRecompile]) => {
310
- // Update hash and snapshot if childCompilation changed
311
- if (didRecompile) {
312
- mainCompilationHashOfLastChildRecompile = /** @type {string} */ (mainCompilation.hash);
313
- }
314
- this.compilationState = {
315
- isCompiling: false,
316
- isVerifyingCache: false,
317
- entries: this.compilationState.entries,
318
- compiledEntries: this.compilationState.entries,
319
- compilationResult: childCompilationResult,
320
- mainCompilationHash: mainCompilationHashOfLastChildRecompile
321
- };
322
- });
333
+ const handleCompilationDonePromise = Promise.all([
334
+ childCompilationResultPromise,
335
+ didRecompilePromise,
336
+ ]).then(([childCompilationResult, didRecompile]) => {
337
+ // Update hash and snapshot if childCompilation changed
338
+ if (didRecompile) {
339
+ mainCompilationHashOfLastChildRecompile =
340
+ /** @type {string} */ (mainCompilation.hash);
341
+ }
342
+ this.compilationState = {
343
+ isCompiling: false,
344
+ isVerifyingCache: false,
345
+ entries: this.compilationState.entries,
346
+ compiledEntries: this.compilationState.entries,
347
+ compilationResult: childCompilationResult,
348
+ mainCompilationHash: mainCompilationHashOfLastChildRecompile,
349
+ };
350
+ });
323
351
  handleCompilationDonePromise.then(() => callback(null), callback);
324
- }
352
+ },
325
353
  );
326
354
 
327
355
  // Continue compilation:
328
356
  callback(null);
329
- }
357
+ },
330
358
  );
331
359
  }
332
360
 
@@ -334,12 +362,15 @@ class PersistentChildCompilerSingletonPlugin {
334
362
  * Add a new entry to the next compile run
335
363
  * @param {string} entry
336
364
  */
337
- addEntry (entry) {
338
- if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
365
+ addEntry(entry) {
366
+ if (
367
+ this.compilationState.isCompiling ||
368
+ this.compilationState.isVerifyingCache
369
+ ) {
339
370
  throw new Error(
340
- 'The child compiler has already started to compile. ' +
341
- "Please add entries before the main compiler 'make' phase has started or " +
342
- 'after the compilation is done.'
371
+ "The child compiler has already started to compile. " +
372
+ "Please add entries before the main compiler 'make' phase has started or " +
373
+ "after the compilation is done.",
343
374
  );
344
375
  }
345
376
  if (this.compilationState.entries.indexOf(entry) === -1) {
@@ -347,17 +378,20 @@ class PersistentChildCompilerSingletonPlugin {
347
378
  }
348
379
  }
349
380
 
350
- getLatestResult () {
351
- if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
381
+ getLatestResult() {
382
+ if (
383
+ this.compilationState.isCompiling ||
384
+ this.compilationState.isVerifyingCache
385
+ ) {
352
386
  throw new Error(
353
- 'The child compiler is not done compiling. ' +
354
- "Please access the result after the compiler 'make' phase has started or " +
355
- 'after the compilation is done.'
387
+ "The child compiler is not done compiling. " +
388
+ "Please access the result after the compiler 'make' phase has started or " +
389
+ "after the compilation is done.",
356
390
  );
357
391
  }
358
392
  return {
359
393
  mainCompilationHash: this.compilationState.mainCompilationHash,
360
- compilationResult: this.compilationState.compilationResult
394
+ compilationResult: this.compilationState.compilationResult,
361
395
  };
362
396
  }
363
397
 
@@ -368,16 +402,22 @@ class PersistentChildCompilerSingletonPlugin {
368
402
  * @param {Compilation} mainCompilation
369
403
  * @returns {Promise<boolean | undefined>}
370
404
  */
371
- isCacheValid (snapshot, mainCompilation) {
405
+ isCacheValid(snapshot, mainCompilation) {
372
406
  if (!this.compilationState.isVerifyingCache) {
373
- return Promise.reject(new Error('Cache validation can only be done right before the compilation starts'));
407
+ return Promise.reject(
408
+ new Error(
409
+ "Cache validation can only be done right before the compilation starts",
410
+ ),
411
+ );
374
412
  }
375
413
  // If there are no entries we don't need a new child compilation
376
414
  if (this.compilationState.entries.length === 0) {
377
415
  return Promise.resolve(true);
378
416
  }
379
417
  // If there are new entries the cache is invalid
380
- if (this.compilationState.entries !== this.compilationState.previousEntries) {
418
+ if (
419
+ this.compilationState.entries !== this.compilationState.previousEntries
420
+ ) {
381
421
  return Promise.resolve(false);
382
422
  }
383
423
  // Mark the cache as invalid if there is no snapshot
@@ -385,7 +425,10 @@ class PersistentChildCompilerSingletonPlugin {
385
425
  return Promise.resolve(false);
386
426
  }
387
427
 
388
- return PersistentChildCompilerSingletonPlugin.isSnapshotValid(snapshot, mainCompilation);
428
+ return PersistentChildCompilerSingletonPlugin.isSnapshotValid(
429
+ snapshot,
430
+ mainCompilation,
431
+ );
389
432
  }
390
433
 
391
434
  /**
@@ -396,29 +439,32 @@ class PersistentChildCompilerSingletonPlugin {
396
439
  * @param {string[]} entries
397
440
  * @returns {Promise<ChildCompilationResult>}
398
441
  */
399
- compileEntries (mainCompilation, entries) {
442
+ compileEntries(mainCompilation, entries) {
400
443
  const compiler = new HtmlWebpackChildCompiler(entries);
401
- return compiler.compileTemplates(mainCompilation).then((result) => {
402
- return {
403
- // The compiled sources to render the content
404
- compiledEntries: result,
444
+ return compiler.compileTemplates(mainCompilation).then(
445
+ (result) => {
446
+ return {
447
+ // The compiled sources to render the content
448
+ compiledEntries: result,
449
+ // The file dependencies to find out if a
450
+ // recompilation is required
451
+ dependencies: compiler.fileDependencies,
452
+ // The main compilation hash can be used to find out
453
+ // if this compilation was done during the current compilation
454
+ mainCompilationHash: mainCompilation.hash,
455
+ };
456
+ },
457
+ (error) => ({
458
+ // The compiled sources to render the content
459
+ error,
405
460
  // The file dependencies to find out if a
406
461
  // recompilation is required
407
462
  dependencies: compiler.fileDependencies,
408
463
  // The main compilation hash can be used to find out
409
464
  // if this compilation was done during the current compilation
410
- mainCompilationHash: mainCompilation.hash
411
- };
412
- }, error => ({
413
- // The compiled sources to render the content
414
- error,
415
- // The file dependencies to find out if a
416
- // recompilation is required
417
- dependencies: compiler.fileDependencies,
418
- // The main compilation hash can be used to find out
419
- // if this compilation was done during the current compilation
420
- mainCompilationHash: mainCompilation.hash
421
- }));
465
+ mainCompilationHash: mainCompilation.hash,
466
+ }),
467
+ );
422
468
  }
423
469
 
424
470
  /**
@@ -426,11 +472,11 @@ class PersistentChildCompilerSingletonPlugin {
426
472
  * @param {Compilation} mainCompilation
427
473
  * @param {FileDependencies} files
428
474
  */
429
- watchFiles (mainCompilation, files) {
475
+ watchFiles(mainCompilation, files) {
430
476
  PersistentChildCompilerSingletonPlugin.watchFiles(mainCompilation, files);
431
477
  }
432
478
  }
433
479
 
434
480
  module.exports = {
435
- CachedChildCompilation
481
+ CachedChildCompilation,
436
482
  };