html-webpack-plugin 4.1.0 → 4.3.0

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,44 @@
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
+ # [4.3.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.2...v4.3.0) (2020-04-30)
6
+
7
+
8
+ ### Features
9
+
10
+ * Allow to use console.log inside templates ([c3f2fdc](https://github.com/jantimon/html-webpack-plugin/commit/c3f2fdc))
11
+
12
+
13
+
14
+ ## [4.2.2](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.1...v4.2.2) (2020-04-30)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * Prevent "cannot read property info of undefined" when reading meta information from assets ([253ce30](https://github.com/jantimon/html-webpack-plugin/commit/253ce30))
20
+ * use modern icon tag rel attribute for favicons ([c40dd85](https://github.com/jantimon/html-webpack-plugin/commit/c40dd85))
21
+
22
+
23
+
24
+ ## [4.2.1](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.0...v4.2.1) (2020-04-28)
25
+
26
+
27
+ ### Bug Fixes
28
+
29
+ * don't add dependencies twice to the webpack 5 watcher api ([ceafe14](https://github.com/jantimon/html-webpack-plugin/commit/ceafe143650749a5f53a14411dc1b762e252ec44))
30
+ * prevent scripts marked as hotModuleReplacement from being added to the html file ([119252a](https://github.com/jantimon/html-webpack-plugin/commit/119252a381bf43dea37c1be64f90c10bebc21302))
31
+
32
+
33
+
34
+ # [4.2.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.1.0...v4.2.0) (2020-04-09)
35
+
36
+
37
+ ### Features
38
+
39
+ * Add template content ([#1401](https://github.com/jantimon/html-webpack-plugin/issues/1401)) ([4740bf7](https://github.com/jantimon/html-webpack-plugin/commit/4740bf7))
40
+
41
+
42
+
5
43
  # [4.1.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.0.4...v4.1.0) (2020-04-09)
6
44
 
7
45
 
package/README.md CHANGED
@@ -134,7 +134,8 @@ Allowed values are as follows
134
134
  |**`title`**|`{String}`|`Webpack App`|The title to use for the generated HTML document|
135
135
  |**`filename`**|`{String}`|`'index.html'`|The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`)|
136
136
  |**`template`**|`{String}`|``|`webpack` relative or absolute path to the template. By default it will use `src/index.ejs` if it exists. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
137
- |**`templateParameters`**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
137
+ |**`templateContent`**|`{string\|Function\|false}`|false| Can be used instead of `template` to provide an inline template - please read the [Writing Your Own Templates](https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates) section |
138
+ |**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) |
138
139
  |**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
139
140
  |**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
140
141
  |**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
@@ -145,7 +146,7 @@ Allowed values are as follows
145
146
  |**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|
146
147
  |**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|
147
148
  |**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|
148
- |**[`chunksSortMode`](#plugins)**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'dependency' \| 'manual' \| {Function}`|
149
+ |**[`chunksSortMode`](#plugins)**|`{String\|Function}`|`auto`|Allows to control how chunks should be sorted before they are included to the HTML. Allowed values are `'none' \| 'auto' \| 'manual' \| {Function}`|
149
150
  |**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|
150
151
  |**`xhtml`**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)|
151
152
 
@@ -243,40 +244,37 @@ plugins: [
243
244
 
244
245
  You can use the `lodash` syntax out of the box. If the `inject` feature doesn't fit your needs and you want full control over the asset placement use the [default template](https://github.com/jaketrent/html-webpack-template/blob/86f285d5c790a6c15263f5cc50fd666d51f974fd/index.html) of the [html-webpack-template project](https://github.com/jaketrent/html-webpack-template) as a starting point for writing your own.
245
246
 
246
- The following variables are available in the template:
247
+ The following variables are available in the template by default (you can extend them using the `templateParameters` option):
248
+
247
249
  - `htmlWebpackPlugin`: data specific to this plugin
248
- - `htmlWebpackPlugin.files`: a massaged representation of the
249
- `assetsByChunkName` attribute of webpack's [stats](https://github.com/webpack/docs/wiki/node.js-api#stats)
250
- object. It contains a mapping from entry point name to the bundle filename, eg:
251
- ```json
252
- "htmlWebpackPlugin": {
253
- "files": {
254
- "css": [ "main.css" ],
255
- "js": [ "assets/head_bundle.js", "assets/main_bundle.js"],
256
- "chunks": {
257
- "head": {
258
- "entry": "assets/head_bundle.js",
259
- "css": [ "main.css" ]
260
- },
261
- "main": {
262
- "entry": "assets/main_bundle.js",
263
- "css": []
264
- },
265
- }
266
- }
267
- }
268
- ```
269
- If you've set a publicPath in your webpack config this will be reflected
270
- correctly in this assets hash.
271
250
 
272
251
  - `htmlWebpackPlugin.options`: the options hash that was passed to
273
252
  the plugin. In addition to the options actually used by this plugin,
274
253
  you can use this hash to pass arbitrary data through to your template.
275
254
 
276
- - `webpack`: the webpack [stats](https://github.com/webpack/docs/wiki/node.js-api#stats)
277
- object. Note that this is the stats object as it was at the time the HTML template
278
- was emitted and as such may not have the full set of stats that are available
279
- after the webpack run is complete.
255
+ - `htmlWebpackPlugin.tags`: the prepared `headTags` and `bodyTags` Array to render the `<base>`, `<meta>`, `<script>` and `<link>` tags.
256
+ Can be used directly in templates and literals. For example:
257
+ ```html
258
+ <html>
259
+ <head>
260
+ <%= htmlWebpackPlugin.tags.headTags %>
261
+ </head>
262
+ <body>
263
+ <%= htmlWebpackPlugin.tags.bodyTags %>
264
+ </body>
265
+ </html>
266
+ ```
267
+
268
+ - `htmlWebpackPlugin.files`: direct access to the files used during the compilation.
269
+
270
+ ```typescript
271
+ publicPath: string;
272
+ js: string[];
273
+ css: string[];
274
+ manifest?: string;
275
+ favicon?: string;
276
+ ```
277
+
280
278
 
281
279
  - `webpackConfig`: the webpack configuration that was used for this compilation. This
282
280
  can be used, for example, to get the `publicPath` (`webpackConfig.output.publicPath`).
@@ -287,6 +285,43 @@ The following variables are available in the template:
287
285
  (see [the inline template example](examples/inline/template.pug)).
288
286
 
289
287
 
288
+ The template can also be directly inlined directly into the options object.
289
+ ⚠️ **`templateContent` does not allow to use webpack loaders for your template and will not watch for template file changes**
290
+
291
+ **webpack.config.js**
292
+ ```js
293
+ new HtmlWebpackPlugin({
294
+ templateContent: `
295
+ <html>
296
+ <body>
297
+ <h1>Hello World</h1>
298
+ </body>
299
+ </html>
300
+ `
301
+ })
302
+ ```
303
+
304
+ The `templateContent` can also access all `templateParameters` values.
305
+ ⚠️ **`templateContent` does not allow to use webpack loaders for your template and will not watch for template file changes**
306
+
307
+ **webpack.config.js**
308
+ ```js
309
+ new HtmlWebpackPlugin({
310
+ inject: false,
311
+ templateContent: ({htmlWebpackPlugin}) => `
312
+ <html>
313
+ <head>
314
+ ${htmlWebpackPlugin.tags.headTags}
315
+ </head>
316
+ <body>
317
+ <h1>Hello World</h1>
318
+ ${htmlWebpackPlugin.tags.bodyTags}
319
+ </body>
320
+ </html>
321
+ `
322
+ })
323
+ ```
324
+
290
325
  ### Filtering Chunks
291
326
 
292
327
  To include only certain chunks you can limit the chunks being used
package/index.js CHANGED
@@ -18,7 +18,7 @@ const path = require('path');
18
18
  const loaderUtils = require('loader-utils');
19
19
  const { CachedChildCompilation } = require('./lib/cached-child-compiler');
20
20
 
21
- const { createHtmlTagObject, htmlTagObjectToString } = require('./lib/html-tags');
21
+ const { createHtmlTagObject, htmlTagObjectToString, HtmlTagArray } = require('./lib/html-tags');
22
22
 
23
23
  const prettyError = require('./lib/errors.js');
24
24
  const chunkSorter = require('./lib/chunksorter.js');
@@ -306,7 +306,7 @@ class HtmlWebpackPlugin {
306
306
  // To extract the result during the evaluation this part has to be removed.
307
307
  source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '');
308
308
  const template = this.options.template.replace(/^.+!/, '').replace(/\?.+$/, '');
309
- const vmContext = vm.createContext(_.extend({ HTML_WEBPACK_PLUGIN: true, require: require }, global));
309
+ const vmContext = vm.createContext(_.extend({ HTML_WEBPACK_PLUGIN: true, require: require, console: console }, global));
310
310
  const vmScript = new vm.Script(source, { filename: template });
311
311
  // Evaluate code and cast to string
312
312
  let newSource;
@@ -574,7 +574,22 @@ class HtmlWebpackPlugin {
574
574
  const extensionRegexp = /\.(css|js|mjs)(\?|$)/;
575
575
  for (let i = 0; i < entryNames.length; i++) {
576
576
  const entryName = entryNames[i];
577
- const entryPointFiles = compilation.entrypoints.get(entryName).getFiles();
577
+ /** entryPointUnfilteredFiles - also includes hot module update files */
578
+ const entryPointUnfilteredFiles = compilation.entrypoints.get(entryName).getFiles();
579
+
580
+ const entryPointFiles = entryPointUnfilteredFiles.filter((chunkFile) => {
581
+ // compilation.getAsset was introduced in webpack 4.4.0
582
+ // once the support pre webpack 4.4.0 is dropped please
583
+ // remove the following guard:
584
+ const asset = compilation.getAsset && compilation.getAsset(chunkFile);
585
+ if (!asset) {
586
+ return true;
587
+ }
588
+ // Prevent hot-module files from beeing included:
589
+ const assetMetaInformation = asset.info || {};
590
+ return !(assetMetaInformation.hotModuleReplacement || assetMetaInformation.development);
591
+ });
592
+
578
593
  // Prepend the publicPath and append the hash depending on the
579
594
  // webpack.output.publicPath and hashOptions
580
595
  // E.g. bundle.js -> /bundle.js?hash
@@ -771,7 +786,7 @@ class HtmlWebpackPlugin {
771
786
  tagName: 'link',
772
787
  voidTag: true,
773
788
  attributes: {
774
- rel: 'shortcut icon',
789
+ rel: 'icon',
775
790
  href: faviconPath
776
791
  }
777
792
  }];
@@ -822,17 +837,13 @@ class HtmlWebpackPlugin {
822
837
  */
823
838
  prepareAssetTagGroupForRendering (assetTagGroup) {
824
839
  const xhtml = this.options.xhtml;
825
- const preparedTags = assetTagGroup.map((assetTag) => {
840
+ return HtmlTagArray.from(assetTagGroup.map((assetTag) => {
826
841
  const copiedAssetTag = Object.assign({}, assetTag);
827
842
  copiedAssetTag.toString = function () {
828
843
  return htmlTagObjectToString(this, xhtml);
829
844
  };
830
845
  return copiedAssetTag;
831
- });
832
- preparedTags.toString = function () {
833
- return this.join('');
834
- };
835
- return preparedTags;
846
+ }));
836
847
  }
837
848
 
838
849
  /**
package/lib/html-tags.js CHANGED
@@ -65,7 +65,31 @@ function createHtmlTagObject (tagName, attributes, innerHTML) {
65
65
  };
66
66
  }
67
67
 
68
+ /**
69
+ * The `HtmlTagArray Array with a custom `.toString()` method.
70
+ *
71
+ * This allows the following:
72
+ * ```
73
+ * const tags = HtmlTagArray.from([tag1, tag2]);
74
+ * const scriptTags = tags.filter((tag) => tag.tagName === 'script');
75
+ * const html = scriptTags.toString();
76
+ * ```
77
+ *
78
+ * Or inside a string literal:
79
+ * ```
80
+ * const tags = HtmlTagArray.from([tag1, tag2]);
81
+ * const html = `<html><body>${tags.filter((tag) => tag.tagName === 'script')}</body></html>`;
82
+ * ```
83
+ *
84
+ */
85
+ class HtmlTagArray extends Array {
86
+ toString () {
87
+ return this.join('');
88
+ }
89
+ }
90
+
68
91
  module.exports = {
92
+ HtmlTagArray: HtmlTagArray,
69
93
  createHtmlTagObject: createHtmlTagObject,
70
94
  htmlTagObjectToString: htmlTagObjectToString
71
95
  };
@@ -58,7 +58,6 @@ function isSnapShotValid (snapshot, mainCompilation) {
58
58
  function watchFiles (mainCompilation, fileDependencies) {
59
59
  Object.keys(fileDependencies).forEach((depencyTypes) => {
60
60
  fileDependencies[depencyTypes].forEach(fileDependency => {
61
- mainCompilation.fileDependencies.add(fileDependency);
62
61
  mainCompilation[depencyTypes].add(fileDependency);
63
62
  });
64
63
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-webpack-plugin",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
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)",
package/typings.d.ts CHANGED
@@ -109,6 +109,7 @@ declare namespace HtmlWebpackPlugin {
109
109
  templateContent:
110
110
  | false // Use the template option instead to load a file
111
111
  | string
112
+ | ((templateParameters: { [option: string]: any }) => (string | Promise<string>))
112
113
  | Promise<string>;
113
114
  /**
114
115
  * Allows to overwrite the parameters used in the template