html-webpack-plugin 4.2.1 → 4.4.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,48 @@
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.4.1](https://github.com/jantimon/html-webpack-plugin/compare/v4.4.0...v4.4.1) (2020-08-30)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * broken typings.d.ts in v4.4.0 ([#1503](https://github.com/jantimon/html-webpack-plugin/issues/1503)) ([98ad756](https://github.com/jantimon/html-webpack-plugin/commit/98ad756))
11
+
12
+
13
+
14
+ # [4.4.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.3.0...v4.4.0) (2020-08-30)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * fix typos in comments ([#1484](https://github.com/jantimon/html-webpack-plugin/issues/1484)) ([6b0711e](https://github.com/jantimon/html-webpack-plugin/commit/6b0711e))
20
+
21
+
22
+ ### Features
23
+
24
+ * added v5 compilation support and deleted depreciation warnings ([4ae7be8](https://github.com/jantimon/html-webpack-plugin/commit/4ae7be8)), closes [#1454](https://github.com/jantimon/html-webpack-plugin/issues/1454)
25
+
26
+
27
+
28
+ # [4.3.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.2...v4.3.0) (2020-04-30)
29
+
30
+
31
+ ### Features
32
+
33
+ * Allow to use console.log inside templates ([c3f2fdc](https://github.com/jantimon/html-webpack-plugin/commit/c3f2fdc))
34
+
35
+
36
+
37
+ ## [4.2.2](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.1...v4.2.2) (2020-04-30)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * Prevent "cannot read property info of undefined" when reading meta information from assets ([253ce30](https://github.com/jantimon/html-webpack-plugin/commit/253ce30))
43
+ * use modern icon tag rel attribute for favicons ([c40dd85](https://github.com/jantimon/html-webpack-plugin/commit/c40dd85))
44
+
45
+
46
+
5
47
  ## [4.2.1](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.0...v4.2.1) (2020-04-28)
6
48
 
7
49
 
package/README.md CHANGED
@@ -78,6 +78,7 @@ The `html-webpack-plugin` provides [hooks](https://github.com/jantimon/html-webp
78
78
  * [html-webpack-link-type-plugin](https://github.com/steadyapp/html-webpack-link-type-plugin) adds a configurable mimetype to resources injected as links (such as adding type="text/css" to external stylesheets) for compatibility with "strict mode".
79
79
  * [csp-html-webpack-plugin](https://github.com/slackhq/csp-html-webpack-plugin) to add [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) meta tags to the HTML output
80
80
  * [webpack-nomodule-plugin](https://github.com/swimmadude66/webpack-nomodule-plugin) allows you to add a `nomodule` attribute to specific injected scripts, which prevents the scripts from being loaded by newer browsers. Good for limiting loads of polyfills.
81
+ * [html-webpack-skip-assets-plugin](https://github.com/swimmadude66/html-webpack-skip-assets-plugin) Skip adding certain output files to the html file. Built as a drop-in replacement for [html-webpack-exclude-assets-plugin](https://www.npmjs.com/package/html-webpack-exclude-assets-plugin) and works with newer [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) versions
81
82
 
82
83
 
83
84
  <h2 align="center">Usage</h2>
@@ -146,7 +147,7 @@ Allowed values are as follows
146
147
  |**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|
147
148
  |**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|
148
149
  |**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|
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}`|
150
+ |**`chunksSortMode`**|`{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}`|
150
151
  |**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|
151
152
  |**`xhtml`**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)|
152
153
 
@@ -275,9 +276,6 @@ The following variables are available in the template by default (you can extend
275
276
  favicon?: string;
276
277
  ```
277
278
 
278
- - `htmlWebpackPlugin.options`: the options hash that was passed to
279
- the plugin. In addition to the options actually used by this plugin,
280
- you can use this hash to pass arbitrary data through to your template.
281
279
 
282
280
  - `webpackConfig`: the webpack configuration that was used for this compilation. This
283
281
  can be used, for example, to get the `publicPath` (`webpackConfig.output.publicPath`).
package/index.js CHANGED
@@ -27,6 +27,8 @@ const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPlugin
27
27
  const fsStatAsync = promisify(fs.stat);
28
28
  const fsReadFileAsync = promisify(fs.readFile);
29
29
 
30
+ const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
31
+
30
32
  class HtmlWebpackPlugin {
31
33
  /**
32
34
  * @param {HtmlWebpackOptions} [options]
@@ -149,12 +151,16 @@ class HtmlWebpackPlugin {
149
151
  compilation.errors.push(prettyError(templateResult.error, compiler.context).toString());
150
152
  }
151
153
 
152
- const childCompilationOutputName = compilation.mainTemplate.getAssetPath(this.options.filename, 'compiledEntry' in templateResult ? {
154
+ const compiledEntries = 'compiledEntry' in templateResult ? {
153
155
  hash: templateResult.compiledEntry.hash,
154
156
  chunk: templateResult.compiledEntry.entry
155
157
  } : {
156
158
  hash: templateResult.mainCompilationHash
157
- });
159
+ };
160
+
161
+ const childCompilationOutputName = webpackMajorVersion === 4
162
+ ? compilation.mainTemplate.getAssetPath(this.options.filename, compiledEntries)
163
+ : compilation.getAssetPath(this.options.filename, compiledEntries);
158
164
 
159
165
  // If the child compilation was not executed during a previous main compile run
160
166
  // it is a cached result
@@ -201,7 +207,7 @@ class HtmlWebpackPlugin {
201
207
  plugin: self
202
208
  }))
203
209
  .then(({ assetTags }) => {
204
- // Inject scripts to body unless it set explictly to head
210
+ // Inject scripts to body unless it set explicitly to head
205
211
  const scriptTarget = self.options.inject === 'head' ? 'head' : 'body';
206
212
  // Group assets to `head` and `body` tag arrays
207
213
  const assetGroups = this.generateAssetGroups(assetTags, scriptTarget);
@@ -214,7 +220,7 @@ class HtmlWebpackPlugin {
214
220
  });
215
221
  });
216
222
 
217
- // Turn the compiled tempalte into a nodejs function or into a nodejs string
223
+ // Turn the compiled template into a nodejs function or into a nodejs string
218
224
  const templateEvaluationPromise = Promise.resolve()
219
225
  .then(() => {
220
226
  if ('error' in templateResult) {
@@ -306,7 +312,7 @@ class HtmlWebpackPlugin {
306
312
  // To extract the result during the evaluation this part has to be removed.
307
313
  source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '');
308
314
  const template = this.options.template.replace(/^.+!/, '').replace(/\?.+$/, '');
309
- const vmContext = vm.createContext(_.extend({ HTML_WEBPACK_PLUGIN: true, require: require }, global));
315
+ const vmContext = vm.createContext(_.extend({ HTML_WEBPACK_PLUGIN: true, require: require, console: console }, global));
310
316
  const vmScript = new vm.Script(source, { filename: template });
311
317
  // Evaluate code and cast to string
312
318
  let newSource;
@@ -527,9 +533,12 @@ class HtmlWebpackPlugin {
527
533
  /**
528
534
  * @type {string} the configured public path to the asset root
529
535
  * if a path publicPath is set in the current webpack config use it otherwise
530
- * fallback to a realtive path
536
+ * fallback to a relative path
531
537
  */
532
- const webpackPublicPath = compilation.mainTemplate.getPublicPath({ hash: compilationHash });
538
+ const webpackPublicPath = webpackMajorVersion === 4
539
+ ? compilation.mainTemplate.getPublicPath({ hash: compilationHash })
540
+ : compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });
541
+
533
542
  const isPublicPathDefined = webpackPublicPath.trim() !== '';
534
543
  let publicPath = isPublicPathDefined
535
544
  // If a hard coded public path exists use it
@@ -581,11 +590,12 @@ class HtmlWebpackPlugin {
581
590
  // compilation.getAsset was introduced in webpack 4.4.0
582
591
  // once the support pre webpack 4.4.0 is dropped please
583
592
  // remove the following guard:
584
- if (!compilation.getAsset) {
593
+ const asset = compilation.getAsset && compilation.getAsset(chunkFile);
594
+ if (!asset) {
585
595
  return true;
586
596
  }
587
- // Prevent hot-module files from beeing included:
588
- const assetMetaInformation = compilation.getAsset(chunkFile).info || {};
597
+ // Prevent hot-module files from being included:
598
+ const assetMetaInformation = asset.info || {};
589
599
  return !(assetMetaInformation.hotModuleReplacement || assetMetaInformation.development);
590
600
  });
591
601
 
@@ -621,8 +631,8 @@ class HtmlWebpackPlugin {
621
631
  }
622
632
 
623
633
  /**
624
- * Converts a favicon file from disk to a webpack ressource
625
- * and returns the url to the ressource
634
+ * Converts a favicon file from disk to a webpack resource
635
+ * and returns the url to the resource
626
636
  *
627
637
  * @param {string|false} faviconFilePath
628
638
  * @param {WebpackCompilation} compilation
@@ -785,7 +795,7 @@ class HtmlWebpackPlugin {
785
795
  tagName: 'link',
786
796
  voidTag: true,
787
797
  attributes: {
788
- rel: 'shortcut icon',
798
+ rel: 'icon',
789
799
  href: faviconPath
790
800
  }
791
801
  }];
@@ -58,7 +58,7 @@ class CachedChildCompilation {
58
58
  * @type {WebpackCompiler}
59
59
  */
60
60
  this.compiler = compiler;
61
- // Create a singlton instance for the compiler
61
+ // Create a singleton instance for the compiler
62
62
  // if there is none
63
63
  if (compilerMap.has(compiler)) {
64
64
  return;
@@ -18,8 +18,8 @@ const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
18
18
  const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
19
19
 
20
20
  /**
21
- * The HtmlWebpackChildCompiler is a helper to allow resusing one childCompiler
22
- * for multile HtmlWebpackPlugin instances to improve the compilation performance.
21
+ * The HtmlWebpackChildCompiler is a helper to allow reusing one childCompiler
22
+ * for multiple HtmlWebpackPlugin instances to improve the compilation performance.
23
23
  */
24
24
  class HtmlWebpackChildCompiler {
25
25
  /**
@@ -53,14 +53,14 @@ class HtmlWebpackChildCompiler {
53
53
 
54
54
  /**
55
55
  * Returns true if the childCompiler is currently compiling
56
- * @retuns {boolean}
56
+ * @returns {boolean}
57
57
  */
58
58
  isCompiling () {
59
59
  return !this.didCompile() && this.compilationStartedTimestamp !== undefined;
60
60
  }
61
61
 
62
62
  /**
63
- * Returns true if the childCOmpiler is done compiling
63
+ * Returns true if the childCompiler is done compiling
64
64
  */
65
65
  didCompile () {
66
66
  return this.compilationEndedTimestamp !== undefined;
@@ -118,7 +118,16 @@ class HtmlWebpackChildCompiler {
118
118
  }
119
119
  // Reject the promise if the childCompilation contains error
120
120
  if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
121
- const errorDetails = childCompilation.errors.map(error => error.message + (error.error ? ':\n' + error.error : '')).join('\n');
121
+ const errorDetails = childCompilation.errors.map(error => {
122
+ let message = error.message;
123
+ if (error.error) {
124
+ message += ':\n' + error.error;
125
+ }
126
+ if (error.stack) {
127
+ message += '\n' + error.stack;
128
+ }
129
+ return message;
130
+ }).join('\n');
122
131
  reject(new Error('Child compilation failed:\n' + errorDetails));
123
132
  return;
124
133
  }
@@ -159,12 +168,18 @@ class HtmlWebpackChildCompiler {
159
168
  * @returns Array<string>
160
169
  */
161
170
  function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) {
171
+ const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
172
+
162
173
  const helperAssetNames = childEntryChunks.map((entryChunk, index) => {
163
- return mainCompilation.mainTemplate.getAssetPath(filename, {
174
+ const entryConfig = {
164
175
  hash: childCompilation.hash,
165
176
  chunk: entryChunk,
166
177
  name: `HtmlWebpackPlugin_${index}`
167
- });
178
+ };
179
+
180
+ return webpackMajorVersion === 4
181
+ ? mainCompilation.mainTemplate.getAssetPath(filename, entryConfig)
182
+ : mainCompilation.getAssetPath(filename, entryConfig);
168
183
  });
169
184
 
170
185
  helperAssetNames.forEach((helperFileName) => {
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
2
  /**
3
- * To use the availble webpack core api
3
+ * To use the available webpack core api
4
4
  * we have to use different child compilers
5
5
  * depending on the used webpack version
6
6
  */
package/lib/html-tags.js CHANGED
@@ -2,8 +2,8 @@
2
2
  /** @typedef {import("../typings").HtmlTagObject} HtmlTagObject */
3
3
  /**
4
4
  * @file
5
- * This file provides to helper to create html as a object repesentation as
6
- * thoses objects are easier to modify than pure string representations
5
+ * This file provides to helper to create html as a object representation as
6
+ * those objects are easier to modify than pure string representations
7
7
  *
8
8
  * Usage:
9
9
  * ```
@@ -47,7 +47,7 @@ function htmlTagObjectToString (tagDefinition, xhtml) {
47
47
  * Static helper to create a tag object to be get injected into the dom
48
48
  *
49
49
  * @param {string} tagName
50
- * the name of the tage e.g. 'div'
50
+ * the name of the tag e.g. 'div'
51
51
  *
52
52
  * @param {{[attributeName: string]: string|boolean}} [attributes]
53
53
  * tag attributes e.g. `{ 'class': 'example', disabled: true }`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "html-webpack-plugin",
3
- "version": "4.2.1",
3
+ "version": "4.4.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)",
package/typings.d.ts CHANGED
@@ -10,6 +10,17 @@ declare class HtmlWebpackPlugin {
10
10
  apply(compiler: Compiler): void;
11
11
 
12
12
  static getHooks(compilation: compilation.Compilation): HtmlWebpackPlugin.Hooks;
13
+
14
+ /**
15
+ * Static helper to create a tag object to be get injected into the dom
16
+ */
17
+ static createHtmlTagObject(
18
+ tagName: string,
19
+ attributes?: { [attributeName: string]: string | boolean },
20
+ innerHTML?: string
21
+ ): HtmlWebpackPlugin.HtmlTagObject;
22
+
23
+ static readonly version: number;
13
24
  }
14
25
 
15
26
  declare namespace HtmlWebpackPlugin {
@@ -23,7 +34,7 @@ declare namespace HtmlWebpackPlugin {
23
34
  interface ProcessedOptions {
24
35
  /**
25
36
  * Emit the file only if it was changed.
26
- * Default: `true`.
37
+ * @default true
27
38
  */
28
39
  cache: boolean;
29
40
  /**
@@ -32,14 +43,14 @@ declare namespace HtmlWebpackPlugin {
32
43
  chunks: "all" | string[];
33
44
  /**
34
45
  * Allows to control how chunks should be sorted before they are included to the html.
35
- * Default: `'auto'`.
46
+ * @default 'auto'
36
47
  */
37
48
  chunksSortMode:
38
49
  | "auto"
39
50
  | "manual"
40
51
  | (((entryNameA: string, entryNameB: string) => number));
41
52
  /**
42
- * List all entries which should not be injeccted
53
+ * List all entries which should not be injected
43
54
  */
44
55
  excludeChunks: string[];
45
56
  /**
@@ -48,8 +59,8 @@ declare namespace HtmlWebpackPlugin {
48
59
  favicon: false | string;
49
60
  /**
50
61
  * The file to write the HTML to.
51
- * Defaults to `index.html`.
52
62
  * Supports subdirectories eg: `assets/admin.html`
63
+ * @default 'index.html'
53
64
  */
54
65
  filename: string;
55
66
  /**
@@ -70,7 +81,7 @@ declare namespace HtmlWebpackPlugin {
70
81
  * blocking will result in <script src="..."></script>
71
82
  * defer will result in <script defer src="..."></script>
72
83
  *
73
- * The default behaviour is blocking
84
+ * @default 'blocking'
74
85
  */
75
86
  scriptLoading:
76
87
  | "blocking"
@@ -87,11 +98,11 @@ declare namespace HtmlWebpackPlugin {
87
98
  | { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
88
99
  };
89
100
  /**
90
- * HTML Minification options accepts the following valeus:
101
+ * HTML Minification options accepts the following values:
91
102
  * - Set to `false` to disable minifcation
92
103
  * - Set to `'auto'` to enable minifcation only for production mode
93
104
  * - Set to custom minification according to
94
- * @https://github.com/kangax/html-minifier#options-quick-reference
105
+ * {@link https://github.com/kangax/html-minifier#options-quick-reference}
95
106
  */
96
107
  minify: 'auto' | boolean | MinifyOptions;
97
108
  /**