html-webpack-plugin 4.3.0 → 4.5.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 +41 -1
- package/README.md +20 -9
- package/index.js +45 -19
- package/lib/cached-child-compiler.js +1 -1
- package/lib/child-compiler.js +22 -7
- package/lib/file-watcher-api.js +1 -1
- package/lib/html-tags.js +3 -3
- package/package.json +3 -3
- package/typings.d.ts +23 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
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.5.1](https://github.com/jantimon/html-webpack-plugin/compare/v4.5.0...v4.5.1) (2021-01-03)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* inject javascripts in the <head> tag for inject:true and scriptLoading:'defer' ([4f7064e](https://github.com/jantimon/html-webpack-plugin/commit/4f7064ee56fe710e8f416018956647a72c270fb1))
|
|
11
|
+
|
|
12
|
+
# [4.5.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.4.1...v4.5.0) (2020-09-21)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* Add publicPath option to overrule the default path generation ([#1516](https://github.com/jantimon/html-webpack-plugin/issues/1516)) ([19b5122](https://github.com/jantimon/html-webpack-plugin/commit/19b5122))
|
|
18
|
+
* update webpack dependency range to allow installing webpack 5 beta ([f3ccdd5](https://github.com/jantimon/html-webpack-plugin/commit/f3ccdd5)), closes [#1504](https://github.com/jantimon/html-webpack-plugin/issues/1504)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## [4.4.1](https://github.com/jantimon/html-webpack-plugin/compare/v4.4.0...v4.4.1) (2020-08-30)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* 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))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# [4.4.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.3.0...v4.4.0) (2020-08-30)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Bug Fixes
|
|
35
|
+
|
|
36
|
+
* fix typos in comments ([#1484](https://github.com/jantimon/html-webpack-plugin/issues/1484)) ([6b0711e](https://github.com/jantimon/html-webpack-plugin/commit/6b0711e))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* 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)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
5
45
|
# [4.3.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.2.2...v4.3.0) (2020-04-30)
|
|
6
46
|
|
|
7
47
|
|
package/README.md
CHANGED
|
@@ -21,6 +21,19 @@
|
|
|
21
21
|
|
|
22
22
|
<h2 align="center">Install</h2>
|
|
23
23
|
|
|
24
|
+
<h3>Webpack 5</h3>
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm i --save-dev html-webpack-plugin@next
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
yarn add --dev html-webpack-plugin@next
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
<h3>Webpack 4</h3>
|
|
36
|
+
|
|
24
37
|
```bash
|
|
25
38
|
npm i --save-dev html-webpack-plugin
|
|
26
39
|
```
|
|
@@ -60,24 +73,21 @@ The `html-webpack-plugin` provides [hooks](https://github.com/jantimon/html-webp
|
|
|
60
73
|
* [webpack-subresource-integrity](https://www.npmjs.com/package/webpack-subresource-integrity) for enhanced asset security
|
|
61
74
|
* [appcache-webpack-plugin](https://github.com/lettertwo/appcache-webpack-plugin) for iOS and Android offline usage
|
|
62
75
|
* [favicons-webpack-plugin](https://github.com/jantimon/favicons-webpack-plugin) which generates favicons and icons for iOS, Android and desktop browsers
|
|
63
|
-
* [html-webpack-harddisk-plugin](https://github.com/jantimon/html-webpack-harddisk-plugin) can be used to always write to disk the html file, useful when webpack-dev-server / HMR are being used
|
|
64
|
-
* [html-webpack-inline-source-plugin](https://github.com/DustinJackson/html-webpack-inline-source-plugin) to inline your assets in the resulting HTML file
|
|
76
|
+
* [html-webpack-harddisk-plugin](https://github.com/jantimon/html-webpack-harddisk-plugin) can be used to always write to disk the html file, useful when webpack-dev-server / HMR are being used
|
|
65
77
|
* [html-webpack-inline-svg-plugin](https://github.com/thegc/html-webpack-inline-svg-plugin) to inline SVGs in the resulting HTML file.
|
|
66
78
|
* [html-webpack-exclude-assets-plugin](https://github.com/jamesjieye/html-webpack-exclude-assets-plugin) for excluding assets using regular expressions
|
|
67
79
|
* [html-webpack-include-assets-plugin](https://github.com/jharris4/html-webpack-include-assets-plugin) for including lists of js or css file paths (such as those copied by the copy-webpack-plugin).
|
|
68
|
-
* [script-ext-html-webpack-plugin](https://github.com/numical/script-ext-html-webpack-plugin) to add `async`, `defer` or `module` attributes to your `<script>` elements, or even inline them
|
|
69
|
-
* [style-ext-html-webpack-plugin](https://github.com/numical/style-ext-html-webpack-plugin) to convert your `<link>`s to external stylesheets into `<style>` elements containing internal CSS
|
|
70
80
|
* [html-webpack-injector](https://github.com/thearchitgarg/html-webpack-injector) to inject chunks in `head` or `body` (different locations ) of same html document.
|
|
71
81
|
* [resource-hints-webpack-plugin](https://github.com/jantimon/resource-hints-webpack-plugin) to add resource hints for faster initial page loads using `<link rel='preload'>` and `<link rel='prefetch'>`
|
|
72
82
|
* [preload-webpack-plugin](https://github.com/GoogleChrome/preload-webpack-plugin) for automatically wiring up asynchronous (and other types) of JavaScript chunks using `<link rel='preload'>` helping with lazy-loading
|
|
73
83
|
* [link-media-html-webpack-plugin](https://github.com/yaycmyk/link-media-html-webpack-plugin) allows for injected stylesheet `<link />` tags to have their media attribute set automatically; useful for providing specific desktop/mobile/print etc. stylesheets that the browser will conditionally download
|
|
74
|
-
* [inline-chunk-manifest-html-webpack-plugin](https://github.com/jouni-kantola/inline-chunk-manifest-html-webpack-plugin) for inlining webpack's chunk manifest. Default extracts manifest and inlines in `<head>`
|
|
75
84
|
* [html-webpack-inline-style-plugin](https://github.com/djaax/html-webpack-inline-style-plugin) for inlining styles to HTML elements using [juice](https://github.com/Automattic/juice). Useful for email generation automatisation.
|
|
76
85
|
* [html-webpack-exclude-empty-assets-plugin](https://github.com/KnisterPeter/html-webpack-exclude-empty-assets-plugin) removes empty assets from being added to the html. This fixes some problems with extract-text-plugin with webpack 4.
|
|
77
86
|
* [webpack-concat-plugin](https://github.com/hxlniada/webpack-concat-plugin) for concat and uglify files that needn't to be webpack bundles(for legacy files) and inject to html-webpack-plugin.
|
|
78
87
|
* [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
88
|
* [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
89
|
* [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.
|
|
90
|
+
* [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
91
|
|
|
82
92
|
|
|
83
93
|
<h2 align="center">Usage</h2>
|
|
@@ -122,12 +132,12 @@ If you have multiple `webpack` entry points, they will all be included with `scr
|
|
|
122
132
|
If you have any CSS assets in webpack's output (for example, CSS extracted with the [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin))
|
|
123
133
|
then these will be included with `<link>` tags in the HTML head.
|
|
124
134
|
|
|
125
|
-
If you have plugins that make use of it, `html-webpack-plugin` should be ordered first before any of the integrated
|
|
135
|
+
If you have plugins that make use of it, `html-webpack-plugin` should be ordered first before any of the integrated Plugins.
|
|
126
136
|
|
|
127
137
|
<h2 align="center">Options</h2>
|
|
128
138
|
|
|
129
139
|
You can pass a hash of configuration options to `html-webpack-plugin`.
|
|
130
|
-
Allowed values are as follows
|
|
140
|
+
Allowed values are as follows:
|
|
131
141
|
|
|
132
142
|
|Name|Type|Default|Description|
|
|
133
143
|
|:--:|:--:|:-----:|:----------|
|
|
@@ -136,7 +146,8 @@ Allowed values are as follows
|
|
|
136
146
|
|**`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
147
|
|**`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
148
|
|**`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) |
|
|
139
|
-
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `
|
|
149
|
+
|**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element. Passing `true` will add it to the head/body depending on the `scriptLoading` option. Passing `false` will disable automatic injections. - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)|
|
|
150
|
+
|**`publicPath`**|`{String\|'auto'}`|`'auto'`|The publicPath used for script and link tags|
|
|
140
151
|
|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. |
|
|
141
152
|
|**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML|
|
|
142
153
|
|**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`|
|
|
@@ -146,7 +157,7 @@ Allowed values are as follows
|
|
|
146
157
|
|**`cache`**|`{Boolean}`|`true`|Emit the file only if it was changed|
|
|
147
158
|
|**`showErrors`**|`{Boolean}`|`true`|Errors details will be written into the HTML page|
|
|
148
159
|
|**`chunks`**|`{?}`|`?`|Allows you to add only some chunks (e.g only the unit-test chunk)|
|
|
149
|
-
|
|
160
|
+
|**`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
161
|
|**`excludeChunks`**|`{Array.<string>}`|``|Allows you to skip some chunks (e.g don't add the unit-test chunk)|
|
|
151
162
|
|**`xhtml`**|`{Boolean}`|`false`|If `true` render the `link` tags as self-closing (XHTML compliant)|
|
|
152
163
|
|
package/index.js
CHANGED
|
@@ -23,10 +23,13 @@ const { createHtmlTagObject, htmlTagObjectToString, HtmlTagArray } = require('./
|
|
|
23
23
|
const prettyError = require('./lib/errors.js');
|
|
24
24
|
const chunkSorter = require('./lib/chunksorter.js');
|
|
25
25
|
const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPluginHooks;
|
|
26
|
+
const { assert } = require('console');
|
|
26
27
|
|
|
27
28
|
const fsStatAsync = promisify(fs.stat);
|
|
28
29
|
const fsReadFileAsync = promisify(fs.readFile);
|
|
29
30
|
|
|
31
|
+
const webpackMajorVersion = Number(require('webpack/package.json').version.split('.')[0]);
|
|
32
|
+
|
|
30
33
|
class HtmlWebpackPlugin {
|
|
31
34
|
/**
|
|
32
35
|
* @param {HtmlWebpackOptions} [options]
|
|
@@ -42,6 +45,7 @@ class HtmlWebpackPlugin {
|
|
|
42
45
|
templateContent: false,
|
|
43
46
|
templateParameters: templateParametersGenerator,
|
|
44
47
|
filename: 'index.html',
|
|
48
|
+
publicPath: userOptions.publicPath === undefined ? 'auto' : userOptions.publicPath,
|
|
45
49
|
hash: false,
|
|
46
50
|
inject: userOptions.scriptLoading !== 'defer' ? 'body' : 'head',
|
|
47
51
|
scriptLoading: 'blocking',
|
|
@@ -62,6 +66,10 @@ class HtmlWebpackPlugin {
|
|
|
62
66
|
/** @type {ProcessedHtmlWebpackOptions} */
|
|
63
67
|
this.options = Object.assign(defaultOptions, userOptions);
|
|
64
68
|
|
|
69
|
+
// Assert correct option spelling
|
|
70
|
+
assert(this.options.scriptLoading === 'defer' || this.options.scriptLoading === 'blocking', 'scriptLoading needs to be set to "defer" or "blocking');
|
|
71
|
+
assert(this.options.inject === true || this.options.inject === false || this.options.inject === 'head' || this.options.inject === 'body', 'inject needs to be set to true, false, "head" or "body');
|
|
72
|
+
|
|
65
73
|
// Default metaOptions if no template is provided
|
|
66
74
|
if (!userOptions.template && this.options.templateContent === false && this.options.meta) {
|
|
67
75
|
const defaultMeta = {
|
|
@@ -149,19 +157,23 @@ class HtmlWebpackPlugin {
|
|
|
149
157
|
compilation.errors.push(prettyError(templateResult.error, compiler.context).toString());
|
|
150
158
|
}
|
|
151
159
|
|
|
152
|
-
const
|
|
160
|
+
const compiledEntries = 'compiledEntry' in templateResult ? {
|
|
153
161
|
hash: templateResult.compiledEntry.hash,
|
|
154
162
|
chunk: templateResult.compiledEntry.entry
|
|
155
163
|
} : {
|
|
156
164
|
hash: templateResult.mainCompilationHash
|
|
157
|
-
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const childCompilationOutputName = webpackMajorVersion === 4
|
|
168
|
+
? compilation.mainTemplate.getAssetPath(this.options.filename, compiledEntries)
|
|
169
|
+
: compilation.getAssetPath(this.options.filename, compiledEntries);
|
|
158
170
|
|
|
159
171
|
// If the child compilation was not executed during a previous main compile run
|
|
160
172
|
// it is a cached result
|
|
161
173
|
const isCompilationCached = templateResult.mainCompilationHash !== compilation.hash;
|
|
162
174
|
|
|
163
175
|
// Turn the entry point names into file paths
|
|
164
|
-
const assets = self.htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames);
|
|
176
|
+
const assets = self.htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames, this.options.publicPath);
|
|
165
177
|
|
|
166
178
|
// If the template and the assets did not change we don't have to emit the html
|
|
167
179
|
const assetJson = JSON.stringify(self.getAssetFiles(assets));
|
|
@@ -201,8 +213,9 @@ class HtmlWebpackPlugin {
|
|
|
201
213
|
plugin: self
|
|
202
214
|
}))
|
|
203
215
|
.then(({ assetTags }) => {
|
|
204
|
-
// Inject scripts to body unless it set
|
|
205
|
-
const scriptTarget = self.options.inject === 'head'
|
|
216
|
+
// Inject scripts to body unless it set explicitly to head
|
|
217
|
+
const scriptTarget = self.options.inject === 'head' ||
|
|
218
|
+
(self.options.inject !== 'body' && self.options.scriptLoading === 'defer') ? 'head' : 'body';
|
|
206
219
|
// Group assets to `head` and `body` tag arrays
|
|
207
220
|
const assetGroups = this.generateAssetGroups(assetTags, scriptTarget);
|
|
208
221
|
// Allow third-party-plugin authors to reorder and change the assetTags once they are grouped
|
|
@@ -214,7 +227,7 @@ class HtmlWebpackPlugin {
|
|
|
214
227
|
});
|
|
215
228
|
});
|
|
216
229
|
|
|
217
|
-
// Turn the compiled
|
|
230
|
+
// Turn the compiled template into a nodejs function or into a nodejs string
|
|
218
231
|
const templateEvaluationPromise = Promise.resolve()
|
|
219
232
|
.then(() => {
|
|
220
233
|
if ('error' in templateResult) {
|
|
@@ -513,6 +526,7 @@ class HtmlWebpackPlugin {
|
|
|
513
526
|
* for all given entry names
|
|
514
527
|
* @param {WebpackCompilation} compilation
|
|
515
528
|
* @param {string[]} entryNames
|
|
529
|
+
* @param {string | 'auto'} customPublicPath
|
|
516
530
|
* @returns {{
|
|
517
531
|
publicPath: string,
|
|
518
532
|
js: Array<string>,
|
|
@@ -521,22 +535,34 @@ class HtmlWebpackPlugin {
|
|
|
521
535
|
favicon?: string
|
|
522
536
|
}}
|
|
523
537
|
*/
|
|
524
|
-
htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames) {
|
|
538
|
+
htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames, customPublicPath) {
|
|
525
539
|
const compilationHash = compilation.hash;
|
|
526
540
|
|
|
527
541
|
/**
|
|
528
542
|
* @type {string} the configured public path to the asset root
|
|
529
543
|
* if a path publicPath is set in the current webpack config use it otherwise
|
|
530
|
-
* fallback to a
|
|
544
|
+
* fallback to a relative path
|
|
531
545
|
*/
|
|
532
|
-
const webpackPublicPath =
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
546
|
+
const webpackPublicPath = webpackMajorVersion === 4
|
|
547
|
+
? compilation.mainTemplate.getPublicPath({ hash: compilationHash })
|
|
548
|
+
: compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash });
|
|
549
|
+
|
|
550
|
+
const isPublicPathDefined = webpackMajorVersion === 4
|
|
551
|
+
? webpackPublicPath.trim() !== ''
|
|
552
|
+
// Webpack 5 introduced "auto" - however it can not be retrieved at runtime
|
|
553
|
+
: webpackPublicPath.trim() !== '' && webpackPublicPath !== 'auto';
|
|
554
|
+
|
|
555
|
+
let publicPath =
|
|
556
|
+
// If the html-webpack-plugin options contain a custom public path uset it
|
|
557
|
+
customPublicPath !== 'auto'
|
|
558
|
+
? customPublicPath
|
|
559
|
+
: (isPublicPathDefined
|
|
560
|
+
// If a hard coded public path exists use it
|
|
561
|
+
? webpackPublicPath
|
|
562
|
+
// If no public path was set get a relative url path
|
|
563
|
+
: path.relative(path.resolve(compilation.options.output.path, path.dirname(childCompilationOutputName)), compilation.options.output.path)
|
|
564
|
+
.split(path.sep).join('/')
|
|
565
|
+
);
|
|
540
566
|
|
|
541
567
|
if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
|
|
542
568
|
publicPath += '/';
|
|
@@ -585,7 +611,7 @@ class HtmlWebpackPlugin {
|
|
|
585
611
|
if (!asset) {
|
|
586
612
|
return true;
|
|
587
613
|
}
|
|
588
|
-
// Prevent hot-module files from
|
|
614
|
+
// Prevent hot-module files from being included:
|
|
589
615
|
const assetMetaInformation = asset.info || {};
|
|
590
616
|
return !(assetMetaInformation.hotModuleReplacement || assetMetaInformation.development);
|
|
591
617
|
});
|
|
@@ -622,8 +648,8 @@ class HtmlWebpackPlugin {
|
|
|
622
648
|
}
|
|
623
649
|
|
|
624
650
|
/**
|
|
625
|
-
* Converts a favicon file from disk to a webpack
|
|
626
|
-
* and returns the url to the
|
|
651
|
+
* Converts a favicon file from disk to a webpack resource
|
|
652
|
+
* and returns the url to the resource
|
|
627
653
|
*
|
|
628
654
|
* @param {string|false} faviconFilePath
|
|
629
655
|
* @param {WebpackCompilation} compilation
|
|
@@ -58,7 +58,7 @@ class CachedChildCompilation {
|
|
|
58
58
|
* @type {WebpackCompiler}
|
|
59
59
|
*/
|
|
60
60
|
this.compiler = compiler;
|
|
61
|
-
// Create a
|
|
61
|
+
// Create a singleton instance for the compiler
|
|
62
62
|
// if there is none
|
|
63
63
|
if (compilerMap.has(compiler)) {
|
|
64
64
|
return;
|
package/lib/child-compiler.js
CHANGED
|
@@ -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
|
|
22
|
-
* for
|
|
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
|
-
* @
|
|
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
|
|
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 =>
|
|
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
|
-
|
|
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) => {
|
package/lib/file-watcher-api.js
CHANGED
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
|
|
6
|
-
*
|
|
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
|
|
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.
|
|
3
|
+
"version": "4.5.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)",
|
|
@@ -55,13 +55,13 @@
|
|
|
55
55
|
"@types/webpack": "^4.41.8",
|
|
56
56
|
"html-minifier-terser": "^5.0.1",
|
|
57
57
|
"loader-utils": "^1.2.3",
|
|
58
|
-
"lodash": "^4.17.
|
|
58
|
+
"lodash": "^4.17.20",
|
|
59
59
|
"pretty-error": "^2.1.1",
|
|
60
60
|
"tapable": "^1.1.3",
|
|
61
61
|
"util.promisify": "1.0.0"
|
|
62
62
|
},
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"webpack": "
|
|
64
|
+
"webpack": "^4.0.0 || ^5.0.0"
|
|
65
65
|
},
|
|
66
66
|
"keywords": [
|
|
67
67
|
"webpack",
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
53
|
+
* List all entries which should not be injected
|
|
43
54
|
*/
|
|
44
55
|
excludeChunks: string[];
|
|
45
56
|
/**
|
|
@@ -48,10 +59,15 @@ 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;
|
|
66
|
+
/**
|
|
67
|
+
* By default the public path is set to `auto` - that way the html-webpack-plugin will try
|
|
68
|
+
* to set the publicPath according to the current filename and the webpack publicPath setting
|
|
69
|
+
*/
|
|
70
|
+
publicPath: string | 'auto';
|
|
55
71
|
/**
|
|
56
72
|
* If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
|
|
57
73
|
* This is useful for cache busting
|
|
@@ -70,7 +86,7 @@ declare namespace HtmlWebpackPlugin {
|
|
|
70
86
|
* blocking will result in <script src="..."></script>
|
|
71
87
|
* defer will result in <script defer src="..."></script>
|
|
72
88
|
*
|
|
73
|
-
*
|
|
89
|
+
* @default 'blocking'
|
|
74
90
|
*/
|
|
75
91
|
scriptLoading:
|
|
76
92
|
| "blocking"
|
|
@@ -87,11 +103,11 @@ declare namespace HtmlWebpackPlugin {
|
|
|
87
103
|
| { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
|
|
88
104
|
};
|
|
89
105
|
/**
|
|
90
|
-
* HTML Minification options accepts the following
|
|
106
|
+
* HTML Minification options accepts the following values:
|
|
91
107
|
* - Set to `false` to disable minifcation
|
|
92
108
|
* - Set to `'auto'` to enable minifcation only for production mode
|
|
93
109
|
* - Set to custom minification according to
|
|
94
|
-
* @https://github.com/kangax/html-minifier#options-quick-reference
|
|
110
|
+
* {@link https://github.com/kangax/html-minifier#options-quick-reference}
|
|
95
111
|
*/
|
|
96
112
|
minify: 'auto' | boolean | MinifyOptions;
|
|
97
113
|
/**
|