html-webpack-plugin 5.3.0 → 5.3.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [5.3.1](https://github.com/jantimon/html-webpack-plugin/compare/v5.3.0...v5.3.1) (2021-03-09)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * remove loader-utils from plugin core ([82d0ee8](https://github.com/jantimon/html-webpack-plugin/commit/82d0ee8ddf146f17d71e98c1b44b2f2ec7420051))
11
+
5
12
  ## [5.3.0](https://github.com/jantimon/html-webpack-plugin/compare/v5.2.0...v5.3.0) (2021-03-07)
6
13
 
7
14
 
package/README.md CHANGED
@@ -464,7 +464,7 @@ which will inject the element `<base href="http://example.com/some/page.html" ta
464
464
 
465
465
  ### Long Term Caching
466
466
 
467
- For long term caching add `contenthash/templatehash` to the filename.
467
+ For long term caching add `contenthash` to the filename.
468
468
 
469
469
  **Example:**
470
470
 
@@ -476,15 +476,9 @@ plugins: [
476
476
  ]
477
477
  ```
478
478
 
479
- `contenthash/templatehash` is the hash of the content of the output file.
479
+ `contenthash` is the hash of the content of the output file.
480
480
 
481
- Optionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]`
482
-
483
- * `hashType` - one of `sha1`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
484
- * `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
485
- * `maxlength` - maximum length of the generated hash in chars
486
-
487
- **Defaults:** `[md5:contenthash:hex:9999]`
481
+ Refer webpack's [Template Strings](https://webpack.js.org/configuration/output/#template-strings) for more details
488
482
 
489
483
  ### Events
490
484
 
package/index.js CHANGED
@@ -14,7 +14,6 @@ const vm = require('vm');
14
14
  const fs = require('fs');
15
15
  const _ = require('lodash');
16
16
  const path = require('path');
17
- const loaderUtils = require('loader-utils');
18
17
  const { CachedChildCompilation } = require('./lib/cached-child-compiler');
19
18
 
20
19
  const { createHtmlTagObject, htmlTagObjectToString, HtmlTagArray } = require('./lib/html-tags');
@@ -132,8 +131,7 @@ class HtmlWebpackPlugin {
132
131
  ...global,
133
132
  HTML_WEBPACK_PLUGIN: true,
134
133
  require: require,
135
- htmlWebpackPluginPublicPath:
136
- publicPath,
134
+ htmlWebpackPluginPublicPath: publicPath,
137
135
  URL: require('url').URL,
138
136
  __filename: templateWithoutLoaders
139
137
  });
@@ -189,13 +187,6 @@ function hookIntoCompiler (compiler, options, plugin) {
189
187
  options.filename = path.relative(outputPath, filename);
190
188
  }
191
189
 
192
- // `contenthash` is introduced in webpack v4.3
193
- // which conflicts with the plugin's existing `contenthash` method,
194
- // hence it is renamed to `templatehash` to avoid conflicts
195
- options.filename = options.filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (match) => {
196
- return match.replace('contenthash', 'templatehash');
197
- });
198
-
199
190
  // Check if webpack is running in production mode
200
191
  // @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
201
192
  const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
@@ -249,21 +240,12 @@ function hookIntoCompiler (compiler, options, plugin) {
249
240
  compilation.errors.push(prettyError(templateResult.error, compiler.context).toString());
250
241
  }
251
242
 
252
- const compiledEntries = 'compiledEntry' in templateResult ? {
253
- hash: templateResult.compiledEntry.hash,
254
- chunk: templateResult.compiledEntry.entry
255
- } : {
256
- hash: templateResult.mainCompilationHash
257
- };
258
-
259
- const childCompilationOutputName = compilation.getAssetPath(options.filename, compiledEntries);
260
-
261
243
  // If the child compilation was not executed during a previous main compile run
262
244
  // it is a cached result
263
245
  const isCompilationCached = templateResult.mainCompilationHash !== compilation.hash;
264
246
 
265
247
  /** The public path used inside the html file */
266
- const htmlPublicPath = getPublicPath(compilation, childCompilationOutputName, options.publicPath);
248
+ const htmlPublicPath = getPublicPath(compilation, options.filename, options.publicPath);
267
249
 
268
250
  /** Generated file paths from the entry point names */
269
251
  const assets = htmlWebpackPluginAssets(compilation, sortedEntryNames, htmlPublicPath);
@@ -288,7 +270,7 @@ function hookIntoCompiler (compiler, options, plugin) {
288
270
  assets.favicon = faviconPath;
289
271
  return getHtmlWebpackPluginHooks(compilation).beforeAssetTagGeneration.promise({
290
272
  assets: assets,
291
- outputName: childCompilationOutputName,
273
+ outputName: options.filename,
292
274
  plugin: plugin
293
275
  });
294
276
  });
@@ -306,7 +288,7 @@ function hookIntoCompiler (compiler, options, plugin) {
306
288
  ...generateFaviconTags(assets.favicon)
307
289
  ]
308
290
  },
309
- outputName: childCompilationOutputName,
291
+ outputName: options.filename,
310
292
  publicPath: htmlPublicPath,
311
293
  plugin: plugin
312
294
  }))
@@ -320,7 +302,7 @@ function hookIntoCompiler (compiler, options, plugin) {
320
302
  return getHtmlWebpackPluginHooks(compilation).alterAssetTagGroups.promise({
321
303
  headTags: assetGroups.headTags,
322
304
  bodyTags: assetGroups.bodyTags,
323
- outputName: childCompilationOutputName,
305
+ outputName: options.filename,
324
306
  publicPath: htmlPublicPath,
325
307
  plugin: plugin
326
308
  });
@@ -351,7 +333,7 @@ function hookIntoCompiler (compiler, options, plugin) {
351
333
  const injectedHtmlPromise = Promise.all([assetTagGroupsPromise, templateExectutionPromise])
352
334
  // Allow plugins to change the html before assets are injected
353
335
  .then(([assetTags, html]) => {
354
- const pluginArgs = { html, headTags: assetTags.headTags, bodyTags: assetTags.bodyTags, plugin: plugin, outputName: childCompilationOutputName };
336
+ const pluginArgs = { html, headTags: assetTags.headTags, bodyTags: assetTags.bodyTags, plugin: plugin, outputName: options.filename };
355
337
  return getHtmlWebpackPluginHooks(compilation).afterTemplateExecution.promise(pluginArgs);
356
338
  })
357
339
  .then(({ html, headTags, bodyTags }) => {
@@ -361,7 +343,7 @@ function hookIntoCompiler (compiler, options, plugin) {
361
343
  const emitHtmlPromise = injectedHtmlPromise
362
344
  // Allow plugins to change the html after assets are injected
363
345
  .then((html) => {
364
- const pluginArgs = { html, plugin: plugin, outputName: childCompilationOutputName };
346
+ const pluginArgs = { html, plugin: plugin, outputName: options.filename };
365
347
  return getHtmlWebpackPluginHooks(compilation).beforeEmit.promise(pluginArgs)
366
348
  .then(result => result.html);
367
349
  })
@@ -372,16 +354,15 @@ function hookIntoCompiler (compiler, options, plugin) {
372
354
  return options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
373
355
  })
374
356
  .then(html => {
375
- // Allow to use [templatehash] as placeholder for the html-webpack-plugin name
376
- // See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
377
- // From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214
378
- const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => {
379
- return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10));
380
- });
357
+ const filename = options.filename.replace(/\[templatehash([^\]]*)\]/g, require('util').deprecate(
358
+ (match, options) => `[contenthash${options}]`,
359
+ '[templatehash] is now [contenthash]')
360
+ );
361
+ const replacedFilename = replacePlaceholdersInFilename(filename, html, compilation);
381
362
  // Add the evaluated html code to the webpack assets
382
- compilation.emitAsset(finalOutputName, new webpack.sources.RawSource(html, false));
383
- previousEmittedAssets.push({ name: finalOutputName, html });
384
- return finalOutputName;
363
+ compilation.emitAsset(replacedFilename.path, new webpack.sources.RawSource(html, false), replacedFilename.info);
364
+ previousEmittedAssets.push({ name: replacedFilename.path, html });
365
+ return replacedFilename.path;
385
366
  })
386
367
  .then((finalOutputName) => getHtmlWebpackPluginHooks(compilation).afterEmit.promise({
387
368
  outputName: finalOutputName,
@@ -519,6 +500,38 @@ function hookIntoCompiler (compiler, options, plugin) {
519
500
  });
520
501
  }
521
502
 
503
+ /**
504
+ * Replace [contenthash] in filename
505
+ *
506
+ * @see https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
507
+ *
508
+ * @param {string} filename
509
+ * @param {string|Buffer} fileContent
510
+ * @param {WebpackCompilation} compilation
511
+ * @returns {{ path: string, info: {} }}
512
+ */
513
+ function replacePlaceholdersInFilename (filename, fileContent, compilation) {
514
+ if (/\[\\*([\w:]+)\\*\]/i.test(filename) === false) {
515
+ return { path: filename, info: {} };
516
+ }
517
+ const hash = compiler.webpack.util.createHash(compilation.outputOptions.hashFunction);
518
+ hash.update(fileContent);
519
+ if (compilation.outputOptions.hashSalt) {
520
+ hash.update(compilation.outputOptions.hashSalt);
521
+ }
522
+ const contentHash = hash.digest(compilation.outputOptions.hashDigest).slice(0, compilation.outputOptions.hashDigestLength);
523
+ return compilation.getPathWithInfo(
524
+ filename,
525
+ {
526
+ contentHash,
527
+ chunk: {
528
+ hash: contentHash,
529
+ contentHash
530
+ }
531
+ }
532
+ );
533
+ }
534
+
522
535
  /**
523
536
  * Helper to sort chunks
524
537
  * @param {string[]} entryNames
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-webpack-plugin",
3
- "version": "5.3.0",
3
+ "version": "5.3.1",
4
4
  "license": "MIT",
5
5
  "description": "Simplifies creation of HTML files to serve your webpack bundles",
6
6
  "author": "Jan Nicklas <j.nicklas@me.com> (https://github.com/jantimon)",
@@ -28,7 +28,6 @@
28
28
  ]
29
29
  },
30
30
  "devDependencies": {
31
- "@types/loader-utils": "2.0.1",
32
31
  "@types/node": "11.13.9",
33
32
  "commitizen": "4.2.1",
34
33
  "css-loader": "5.0.1",
@@ -45,9 +44,9 @@
45
44
  "standard-version": "9.1.0",
46
45
  "style-loader": "2.0.0",
47
46
  "typescript": "4.1.3",
48
- "webpack": "5.23.0",
47
+ "webpack": "5.24.3",
49
48
  "webpack-recompilation-simulator": "3.2.0",
50
- "webpack-cli": "4.2.0"
49
+ "webpack-cli": "4.5.0"
51
50
  },
52
51
  "dependencies": {
53
52
  "@types/html-minifier-terser": "^5.0.0",