html-webpack-plugin 4.0.0-alpha.1 → 4.0.0-beta.11

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/lib/compiler.js CHANGED
@@ -31,15 +31,15 @@ class HtmlWebpackChildCompiler {
31
31
  /**
32
32
  * @type {Promise<{[templatePath: string]: { content: string, hash: string, entry: WebpackChunk }}>}
33
33
  */
34
- this.compilationPromise;
34
+ this.compilationPromise; // eslint-disable-line
35
35
  /**
36
36
  * @type {number}
37
37
  */
38
- this.compilationStartedTimestamp;
38
+ this.compilationStartedTimestamp; // eslint-disable-line
39
39
  /**
40
40
  * @type {number}
41
41
  */
42
- this.compilationEndedTimestamp;
42
+ this.compilationEndedTimestamp; // eslint-disable-line
43
43
  /**
44
44
  * All file dependencies of the child compiler
45
45
  * @type {string[]}
@@ -119,18 +119,6 @@ class HtmlWebpackChildCompiler {
119
119
  new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var').apply(childCompiler);
120
120
  new LoaderTargetPlugin('node').apply(childCompiler);
121
121
 
122
- // Fix for "Uncaught TypeError: __webpack_require__(...) is not a function"
123
- // Hot module replacement requires that every child compiler has its own
124
- // cache. @see https://github.com/ampedandwired/html-webpack-plugin/pull/179
125
- childCompiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => {
126
- if (compilation.cache) {
127
- if (!compilation.cache[compilerName]) {
128
- compilation.cache[compilerName] = {};
129
- }
130
- compilation.cache = compilation.cache[compilerName];
131
- }
132
- });
133
-
134
122
  // Add all templates
135
123
  this.templates.forEach((template, index) => {
136
124
  new SingleEntryPlugin(childCompiler.context, template, `HtmlWebpackPlugin_${index}`).apply(childCompiler);
@@ -145,7 +133,7 @@ class HtmlWebpackChildCompiler {
145
133
  : [];
146
134
  // Extract file dependencies
147
135
  if (entries) {
148
- this.fileDependencies = extractFileDependenciesFilesFromCompilation(entries);
136
+ this.fileDependencies = Array.from(childCompilation.fileDependencies);
149
137
  }
150
138
  // Reject the promise if the childCompilation contains error
151
139
  if (childCompilation && childCompilation.errors && childCompilation.errors.length) {
@@ -191,7 +179,7 @@ class HtmlWebpackChildCompiler {
191
179
  */
192
180
  function extractHelperFilesFromCompilation (mainCompilation, childCompilation, filename, childEntryChunks) {
193
181
  const helperAssetNames = childEntryChunks.map((entryChunk, index) => {
194
- return mainCompilation.mainTemplate.hooks.assetPath.call(filename, {
182
+ return mainCompilation.mainTemplate.getAssetPath(filename, {
195
183
  hash: childCompilation.hash,
196
184
  chunk: entryChunk,
197
185
  name: `HtmlWebpackPlugin_${index}`
@@ -209,21 +197,6 @@ function extractHelperFilesFromCompilation (mainCompilation, childCompilation, f
209
197
  return helperContents;
210
198
  }
211
199
 
212
- /**
213
- * Return all file dependencies from the given set of entries.
214
- * @param {WebpackChunk[]} entries
215
- * @returns {string[]}
216
- */
217
- function extractFileDependenciesFilesFromCompilation (entries) {
218
- const fileDependencies = new Map();
219
- entries.forEach((entry) => {
220
- entry.entryModule.buildInfo.fileDependencies.forEach((fileDependency) => {
221
- fileDependencies.set(fileDependency, true);
222
- });
223
- });
224
- return Array.from(fileDependencies.keys());
225
- }
226
-
227
200
  /**
228
201
  * @type {WeakMap<WebpackCompiler, HtmlWebpackChildCompiler>}}
229
202
  */
@@ -288,7 +261,7 @@ function compileTemplate (templatePath, outputFilename, mainCompilation) {
288
261
  if (!compiledTemplates[templatePath]) console.log(Object.keys(compiledTemplates), templatePath);
289
262
  const compiledTemplate = compiledTemplates[templatePath];
290
263
  // Replace [hash] placeholders in filename
291
- const outputName = mainCompilation.mainTemplate.hooks.assetPath.call(outputFilename, {
264
+ const outputName = mainCompilation.mainTemplate.getAssetPath(outputFilename, {
292
265
  hash: compiledTemplate.hash,
293
266
  chunk: compiledTemplate.entry
294
267
  });
package/lib/errors.js CHANGED
@@ -18,7 +18,14 @@ module.exports = function (err, context) {
18
18
  return JSON.stringify(this.toHtml());
19
19
  },
20
20
  toString: function () {
21
- return prettyError.render(err).replace(/webpack:\/\/\/\./g, context);
21
+ try {
22
+ return prettyError.render(err).replace(/webpack:\/\/\/\./g, context);
23
+ } catch (e) {
24
+ // This can sometimes fail. We don't know why, but returning the
25
+ // original error is better than returning the error thrown by
26
+ // pretty-error.
27
+ return err;
28
+ }
22
29
  }
23
30
  };
24
31
  };
package/lib/hooks.js CHANGED
@@ -1,7 +1,5 @@
1
1
  // @ts-check
2
- /* eslint-disable */
3
- /// <reference path="../typings.d.ts" />
4
- /* eslint-enable */
2
+ /** @typedef {import("../typings").Hooks} HtmlWebpackPluginHooks */
5
3
  'use strict';
6
4
  /**
7
5
  * This file provides access to all public htmlWebpackPlugin hooks
@@ -12,23 +10,21 @@
12
10
 
13
11
  const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
14
12
 
15
- // The following typedef holds the API definition for all available hooks
16
- // to allow easier access when using ts-check or typescript inside plugins
17
- /** @typedef {{
18
-
13
+ // The following is the API definition for all available hooks
14
+ // For the TypeScript definition, see the Hooks type in typings.d.ts
15
+ /**
19
16
  beforeAssetTagGeneration:
20
17
  AsyncSeriesWaterfallHook<{
21
18
  assets: {
22
19
  publicPath: string,
23
- js: Array<{entryName: string, path: string}>,
24
- css: Array<{entryName: string, path: string}>,
20
+ js: Array<string>,
21
+ css: Array<string>,
25
22
  favicon?: string | undefined,
26
23
  manifest?: string | undefined
27
24
  },
28
25
  outputName: string,
29
26
  plugin: HtmlWebpackPlugin
30
27
  }>,
31
-
32
28
  alterAssetTags:
33
29
  AsyncSeriesWaterfallHook<{
34
30
  assetTags: {
@@ -39,7 +35,6 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
39
35
  outputName: string,
40
36
  plugin: HtmlWebpackPlugin
41
37
  }>,
42
-
43
38
  alterAssetTagGroups:
44
39
  AsyncSeriesWaterfallHook<{
45
40
  headTags: Array<HtmlTagObject | HtmlTagObject>,
@@ -47,7 +42,6 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
47
42
  outputName: string,
48
43
  plugin: HtmlWebpackPlugin
49
44
  }>,
50
-
51
45
  afterTemplateExecution:
52
46
  AsyncSeriesWaterfallHook<{
53
47
  html: string,
@@ -56,22 +50,18 @@ const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;
56
50
  outputName: string,
57
51
  plugin: HtmlWebpackPlugin,
58
52
  }>,
59
-
60
53
  beforeEmit:
61
54
  AsyncSeriesWaterfallHook<{
62
55
  html: string,
63
56
  outputName: string,
64
57
  plugin: HtmlWebpackPlugin,
65
58
  }>,
66
-
67
59
  afterEmit:
68
60
  AsyncSeriesWaterfallHook<{
69
61
  outputName: string,
70
62
  plugin: HtmlWebpackPlugin
71
- }>,
72
-
73
- }} HtmlWebpackPluginHooks
74
- */
63
+ }>
64
+ */
75
65
 
76
66
  /**
77
67
  * @type {WeakMap<WebpackCompilation, HtmlWebpackPluginHooks>}}
package/lib/html-tags.js CHANGED
@@ -1,7 +1,5 @@
1
1
  // @ts-check
2
- /* eslint-disable */
3
- /// <reference path="../typings.d.ts" />
4
- /* eslint-enable */
2
+ /** @typedef {import("../typings").HtmlTagObject} HtmlTagObject */
5
3
  /**
6
4
  * @file
7
5
  * This file provides to helper to create html as a object repesentation as
package/lib/loader.js CHANGED
@@ -21,11 +21,12 @@ module.exports = function (source) {
21
21
  return source;
22
22
  }
23
23
 
24
- // The following part renders the template with lodash as aminimalistic loader
24
+ // The following part renders the template with lodash as a minimalistic loader
25
25
  //
26
26
  const template = _.template(source, _.defaults(options, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data' }));
27
- // Require !!lodash - using !! will disable all loaders (e.g. babel)
28
- return 'var _ = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('lodash')) + ');' +
27
+ // Use __non_webpack_require__ to enforce using the native nodejs require
28
+ // during template execution
29
+ return 'var _ = __non_webpack_require__(' + JSON.stringify(require.resolve('lodash')) + ');' +
29
30
  'module.exports = function (templateParams) { with(templateParams) {' +
30
31
  // Execute the lodash template
31
32
  'return (' + template.source + ')();' +
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "html-webpack-plugin",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-beta.11",
4
4
  "license": "MIT",
5
5
  "description": "Simplifies creation of HTML files to serve your webpack bundles",
6
- "author": "Charles Blaxland <charles.blaxland@gmail.com> (https://github.com/ampedandwired)",
6
+ "author": "Jan Nicklas <j.nicklas@me.com> (https://github.com/jantimon)",
7
7
  "main": "index.js",
8
+ "types": "typings.d.ts",
8
9
  "files": [
9
10
  "lib/",
10
11
  "index.js",
11
- "default_index.ejs"
12
+ "default_index.ejs",
13
+ "typings.d.ts"
12
14
  ],
13
15
  "scripts": {
14
16
  "pretest": "semistandard",
@@ -26,37 +28,35 @@
26
28
  ]
27
29
  },
28
30
  "devDependencies": {
31
+ "@types/html-minifier": "3.5.3",
29
32
  "@types/loader-utils": "1.1.3",
30
- "@types/node": "10.1.1",
31
- "appcache-webpack-plugin": "^1.3.0",
32
- "commitizen": "2.9.6",
33
- "css-loader": "^0.26.1",
33
+ "@types/node": "11.13.9",
34
+ "@types/tapable": "1.0.4",
35
+ "appcache-webpack-plugin": "1.4.0",
36
+ "commitizen": "4.0.3",
37
+ "css-loader": "2.1.1",
34
38
  "cz-conventional-changelog": "2.1.0",
35
- "dir-compare": "1.3.0",
36
- "extract-text-webpack-plugin": "^4.0.0-beta.0",
37
- "file-loader": "^0.9.0",
38
- "html-loader": "^0.4.4",
39
- "jest": "23.3.0",
39
+ "dir-compare": "1.7.2",
40
+ "file-loader": "3.0.1",
41
+ "html-loader": "0.5.5",
42
+ "jest": "24.7.1",
43
+ "mini-css-extract-plugin": "0.6.0",
40
44
  "pug": "2.0.3",
41
45
  "pug-loader": "2.4.0",
42
- "rimraf": "^2.5.4",
43
- "semistandard": "8.0.0",
44
- "standard-version": "^4.3.0",
45
- "style-loader": "^0.13.1",
46
- "typescript": "2.9.1",
47
- "underscore-template-loader": "^0.7.3",
48
- "url-loader": "^0.5.7",
49
- "webpack": "4.1.0",
50
- "webpack-cli": "2.0.12",
51
- "webpack-recompilation-simulator": "^3.0.0"
46
+ "rimraf": "2.6.3",
47
+ "semistandard": "13.0.1",
48
+ "standard-version": "5.0.2",
49
+ "style-loader": "0.23.1",
50
+ "typescript": "3.5.2",
51
+ "webpack": "4.35.2",
52
+ "webpack-recompilation-simulator": "3.0.0"
52
53
  },
53
54
  "dependencies": {
54
- "@types/tapable": "1.0.2",
55
- "html-minifier": "^3.2.3",
56
- "loader-utils": "^1.1.0",
57
- "lodash": "^4.17.10",
58
- "pretty-error": "^2.0.2",
59
- "tapable": "^1.0.0",
55
+ "html-minifier-terser": "^5.0.1",
56
+ "loader-utils": "^1.2.3",
57
+ "lodash": "^4.17.15",
58
+ "pretty-error": "^2.1.1",
59
+ "tapable": "^1.1.3",
60
60
  "util.promisify": "1.0.0"
61
61
  },
62
62
  "peerDependencies": {
package/typings.d.ts ADDED
@@ -0,0 +1,236 @@
1
+ import { AsyncSeriesWaterfallHook } from "tapable";
2
+ import { Compiler } from 'webpack';
3
+ import { Options as HtmlMinifierOptions } from "html-minifier";
4
+
5
+ export = HtmlWebpackPlugin;
6
+
7
+ declare class HtmlWebpackPlugin {
8
+ constructor(options?: HtmlWebpackPlugin.Options);
9
+
10
+ apply(compiler: Compiler): void;
11
+ }
12
+
13
+ declare namespace HtmlWebpackPlugin {
14
+ type MinifyOptions = HtmlMinifierOptions;
15
+
16
+ interface Options extends Partial<ProcessedOptions> {}
17
+
18
+ /**
19
+ * The plugin options after adding default values
20
+ */
21
+ interface ProcessedOptions {
22
+ /**
23
+ * Emit the file only if it was changed.
24
+ * Default: `true`.
25
+ */
26
+ cache: boolean;
27
+ /**
28
+ * List all entries which should be injected
29
+ */
30
+ chunks: "all" | string[];
31
+ /**
32
+ * Allows to control how chunks should be sorted before they are included to the html.
33
+ * Default: `'auto'`.
34
+ */
35
+ chunksSortMode:
36
+ | "auto"
37
+ | "manual"
38
+ | (((entryNameA: string, entryNameB: string) => number));
39
+ /**
40
+ * List all entries which should not be injeccted
41
+ */
42
+ excludeChunks: string[];
43
+ /**
44
+ * Path to the favicon icon
45
+ */
46
+ favicon: false | string;
47
+ /**
48
+ * The file to write the HTML to.
49
+ * Defaults to `index.html`.
50
+ * Supports subdirectories eg: `assets/admin.html`
51
+ */
52
+ filename: string;
53
+ /**
54
+ * If `true` then append a unique `webpack` compilation hash to all included scripts and CSS files.
55
+ * This is useful for cache busting
56
+ */
57
+ hash: boolean;
58
+ /**
59
+ * Inject all assets into the given `template` or `templateContent`.
60
+ */
61
+ inject:
62
+ | false // Don't inject scripts
63
+ | true // Inject scripts into body
64
+ | "body" // Inject scripts into body
65
+ | "head"; // Inject scripts into head
66
+ /**
67
+ * Inject meta tags
68
+ */
69
+ meta:
70
+ | false // Disable injection
71
+ | {
72
+ [name: string]:
73
+ | string
74
+ | false // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
75
+ | { [attributeName: string]: string | boolean }; // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
76
+ };
77
+ /**
78
+ * HTML Minification options accepts the following valeus:
79
+ * - Set to `false` to disable minifcation
80
+ * - Set to `'auto'` to enable minifcation only for production mode
81
+ * - Set to custom minification according to
82
+ * @https://github.com/kangax/html-minifier#options-quick-reference
83
+ */
84
+ minify: 'auto' | boolean | MinifyOptions;
85
+ /**
86
+ * Render errors into the HTML page
87
+ */
88
+ showErrors: boolean;
89
+ /**
90
+ * The `webpack` require path to the template.
91
+ * @see https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md
92
+ */
93
+ template: string;
94
+ /**
95
+ * Allow to use a html string instead of reading from a file
96
+ */
97
+ templateContent:
98
+ | false // Use the template option instead to load a file
99
+ | string
100
+ | Promise<string>;
101
+ /**
102
+ * Allows to overwrite the parameters used in the template
103
+ */
104
+ templateParameters:
105
+ | false // Pass an empty object to the template function
106
+ | ((
107
+ compilation: any,
108
+ assets: {
109
+ publicPath: string;
110
+ js: Array<string>;
111
+ css: Array<string>;
112
+ manifest?: string;
113
+ favicon?: string;
114
+ },
115
+ assetTags: {
116
+ headTags: HtmlTagObject[];
117
+ bodyTags: HtmlTagObject[];
118
+ },
119
+ options: ProcessedOptions
120
+ ) => { [option: string]: any } | Promise<{ [option: string]: any }>)
121
+ | { [option: string]: any };
122
+ /**
123
+ * The title to use for the generated HTML document
124
+ */
125
+ title: string;
126
+ /**
127
+ * Enforce self closing tags e.g. <link />
128
+ */
129
+ xhtml: boolean;
130
+ /**
131
+ * In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through
132
+ * to your template.
133
+ */
134
+ [option: string]: any;
135
+ }
136
+
137
+ /**
138
+ * The values which are available during template execution
139
+ *
140
+ * Please keep in mind that the `templateParameter` options allows to change them
141
+ */
142
+ interface TemplateParameter {
143
+ compilation: any;
144
+ htmlWebpackPlugin: {
145
+ tags: {
146
+ headTags: HtmlTagObject[];
147
+ bodyTags: HtmlTagObject[];
148
+ };
149
+ files: {
150
+ publicPath: string;
151
+ js: Array<string>;
152
+ css: Array<string>;
153
+ manifest?: string;
154
+ favicon?: string;
155
+ };
156
+ options: Options;
157
+ };
158
+ webpackConfig: any;
159
+ }
160
+
161
+ interface Hooks {
162
+ alterAssetTags: AsyncSeriesWaterfallHook<{
163
+ assetTags: {
164
+ scripts: HtmlTagObject[];
165
+ styles: HtmlTagObject[];
166
+ meta: HtmlTagObject[];
167
+ };
168
+ outputName: string;
169
+ plugin: HtmlWebpackPlugin;
170
+ }>;
171
+
172
+ alterAssetTagGroups: AsyncSeriesWaterfallHook<{
173
+ headTags: HtmlTagObject[];
174
+ bodyTags: HtmlTagObject[];
175
+ outputName: string;
176
+ plugin: HtmlWebpackPlugin;
177
+ }>;
178
+
179
+ afterTemplateExecution: AsyncSeriesWaterfallHook<{
180
+ html: string;
181
+ headTags: HtmlTagObject[];
182
+ bodyTags: HtmlTagObject[];
183
+ outputName: string;
184
+ plugin: HtmlWebpackPlugin;
185
+ }>;
186
+
187
+ beforeAssetTagGeneration: AsyncSeriesWaterfallHook<{
188
+ assets: {
189
+ publicPath: string;
190
+ js: Array<string>;
191
+ css: Array<string>;
192
+ favicon?: string;
193
+ manifest?: string;
194
+ };
195
+ outputName: string;
196
+ plugin: HtmlWebpackPlugin;
197
+ }>;
198
+
199
+ beforeEmit: AsyncSeriesWaterfallHook<{
200
+ html: string;
201
+ outputName: string;
202
+ plugin: HtmlWebpackPlugin;
203
+ }>;
204
+
205
+ afterEmit: AsyncSeriesWaterfallHook<{
206
+ outputName: string;
207
+ plugin: HtmlWebpackPlugin;
208
+ }>;
209
+ }
210
+
211
+ /**
212
+ * A tag element according to the htmlWebpackPlugin object notation
213
+ */
214
+ interface HtmlTagObject {
215
+ /**
216
+ * Attributes of the html tag
217
+ * E.g. `{'disabled': true, 'value': 'demo'}`
218
+ */
219
+ attributes: {
220
+ [attributeName: string]: string | boolean;
221
+ };
222
+ /**
223
+ * The tag name e.g. `'div'`
224
+ */
225
+ tagName: string;
226
+ /**
227
+ * The inner HTML
228
+ */
229
+ innerHTML?: string;
230
+ /**
231
+ * Whether this html must not contain innerHTML
232
+ * @see https://www.w3.org/TR/html5/syntax.html#void-elements
233
+ */
234
+ voidTag: boolean;
235
+ }
236
+ }