UNPKG

39.4 kBJavaScriptView Raw
1// @ts-check
2// Import types
3/** @typedef {import("./typings").HtmlTagObject} HtmlTagObject */
4/** @typedef {import("./typings").Options} HtmlWebpackOptions */
5/** @typedef {import("./typings").ProcessedOptions} ProcessedHtmlWebpackOptions */
6/** @typedef {import("./typings").TemplateParameter} TemplateParameter */
7/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
8/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
9'use strict';
10
11// use Polyfill for util.promisify in node versions < v8
12const promisify = require('util.promisify');
13
14const vm = require('vm');
15const fs = require('fs');
16const _ = require('lodash');
17const path = require('path');
18const loaderUtils = require('loader-utils');
19const { CachedChildCompilation } = require('./lib/cached-child-compiler');
20
21const { createHtmlTagObject, htmlTagObjectToString, HtmlTagArray } = require('./lib/html-tags');
22
23const prettyError = require('./lib/errors.js');
24const chunkSorter = require('./lib/chunksorter.js');
25const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPluginHooks;
26
27const fsStatAsync = promisify(fs.stat);
28const fsReadFileAsync = promisify(fs.readFile);
29
30class HtmlWebpackPlugin {
31 /**
32 * @param {HtmlWebpackOptions} [options]
33 */
34 constructor (options) {
35 /** @type {HtmlWebpackOptions} */
36 const userOptions = options || {};
37
38 // Default options
39 /** @type {ProcessedHtmlWebpackOptions} */
40 const defaultOptions = {
41 template: 'auto',
42 templateContent: false,
43 templateParameters: templateParametersGenerator,
44 filename: 'index.html',
45 hash: false,
46 inject: userOptions.scriptLoading !== 'defer' ? 'body' : 'head',
47 scriptLoading: 'blocking',
48 compile: true,
49 favicon: false,
50 minify: 'auto',
51 cache: true,
52 showErrors: true,
53 chunks: 'all',
54 excludeChunks: [],
55 chunksSortMode: 'auto',
56 meta: {},
57 base: false,
58 title: 'Webpack App',
59 xhtml: false
60 };
61
62 /** @type {ProcessedHtmlWebpackOptions} */
63 this.options = Object.assign(defaultOptions, userOptions);
64
65 // Default metaOptions if no template is provided
66 if (!userOptions.template && this.options.templateContent === false && this.options.meta) {
67 const defaultMeta = {
68 // From https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag
69 viewport: 'width=device-width, initial-scale=1'
70 };
71 this.options.meta = Object.assign({}, this.options.meta, defaultMeta, userOptions.meta);
72 }
73
74 // Instance variables to keep caching information
75 // for multiple builds
76 this.childCompilerHash = undefined;
77 this.assetJson = undefined;
78 this.hash = undefined;
79 this.version = HtmlWebpackPlugin.version;
80 }
81
82 /**
83 * apply is called by the webpack main compiler during the start phase
84 * @param {WebpackCompiler} compiler
85 */
86 apply (compiler) {
87 const self = this;
88
89 this.options.template = this.getFullTemplatePath(this.options.template, compiler.context);
90
91 // Inject child compiler plugin
92 const childCompilerPlugin = new CachedChildCompilation(compiler);
93 if (!this.options.templateContent) {
94 childCompilerPlugin.addEntry(this.options.template);
95 }
96
97 // convert absolute filename into relative so that webpack can
98 // generate it at correct location
99 const filename = this.options.filename;
100 if (path.resolve(filename) === path.normalize(filename)) {
101 this.options.filename = path.relative(compiler.options.output.path, filename);
102 }
103
104 // `contenthash` is introduced in webpack v4.3
105 // which conflicts with the plugin's existing `contenthash` method,
106 // hence it is renamed to `templatehash` to avoid conflicts
107 this.options.filename = this.options.filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (match) => {
108 return match.replace('contenthash', 'templatehash');
109 });
110
111 // Check if webpack is running in production mode
112 // @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14
113 const isProductionLikeMode = compiler.options.mode === 'production' || !compiler.options.mode;
114
115 const minify = this.options.minify;
116 if (minify === true || (minify === 'auto' && isProductionLikeMode)) {
117 /** @type { import('html-minifier-terser').Options } */
118 this.options.minify = {
119 // https://www.npmjs.com/package/html-minifier-terser#options-quick-reference
120 collapseWhitespace: true,
121 keepClosingSlash: true,
122 removeComments: true,
123 removeRedundantAttributes: true,
124 removeScriptTypeAttributes: true,
125 removeStyleLinkTypeAttributes: true,
126 useShortDoctype: true
127 };
128 }
129
130 compiler.hooks.emit.tapAsync('HtmlWebpackPlugin',
131 /**
132 * Hook into the webpack emit phase
133 * @param {WebpackCompilation} compilation
134 * @param {(err?: Error) => void} callback
135 */
136 (compilation, callback) => {
137 // Get all entry point names for this html file
138 const entryNames = Array.from(compilation.entrypoints.keys());
139 const filteredEntryNames = self.filterChunks(entryNames, self.options.chunks, self.options.excludeChunks);
140 const sortedEntryNames = self.sortEntryChunks(filteredEntryNames, this.options.chunksSortMode, compilation);
141
142 const templateResult = this.options.templateContent
143 ? { mainCompilationHash: compilation.hash }
144 : childCompilerPlugin.getCompilationEntryResult(this.options.template);
145
146 this.childCompilerHash = templateResult.mainCompilationHash;
147
148 if ('error' in templateResult) {
149 compilation.errors.push(prettyError(templateResult.error, compiler.context).toString());
150 }
151
152 const childCompilationOutputName = compilation.mainTemplate.getAssetPath(this.options.filename, 'compiledEntry' in templateResult ? {
153 hash: templateResult.compiledEntry.hash,
154 chunk: templateResult.compiledEntry.entry
155 } : {
156 hash: templateResult.mainCompilationHash
157 });
158
159 // If the child compilation was not executed during a previous main compile run
160 // it is a cached result
161 const isCompilationCached = templateResult.mainCompilationHash !== compilation.hash;
162
163 // Turn the entry point names into file paths
164 const assets = self.htmlWebpackPluginAssets(compilation, childCompilationOutputName, sortedEntryNames);
165
166 // If the template and the assets did not change we don't have to emit the html
167 const assetJson = JSON.stringify(self.getAssetFiles(assets));
168 if (isCompilationCached && self.options.cache && assetJson === self.assetJson) {
169 return callback();
170 } else {
171 self.assetJson = assetJson;
172 }
173
174 // The html-webpack plugin uses a object representation for the html-tags which will be injected
175 // to allow altering them more easily
176 // Just before they are converted a third-party-plugin author might change the order and content
177 const assetsPromise = this.getFaviconPublicPath(this.options.favicon, compilation, assets.publicPath)
178 .then((faviconPath) => {
179 assets.favicon = faviconPath;
180 return getHtmlWebpackPluginHooks(compilation).beforeAssetTagGeneration.promise({
181 assets: assets,
182 outputName: childCompilationOutputName,
183 plugin: self
184 });
185 });
186
187 // Turn the js and css paths into grouped HtmlTagObjects
188 const assetTagGroupsPromise = assetsPromise
189 // And allow third-party-plugin authors to reorder and change the assetTags before they are grouped
190 .then(({ assets }) => getHtmlWebpackPluginHooks(compilation).alterAssetTags.promise({
191 assetTags: {
192 scripts: self.generatedScriptTags(assets.js),
193 styles: self.generateStyleTags(assets.css),
194 meta: [
195 ...self.generateBaseTag(self.options.base),
196 ...self.generatedMetaTags(self.options.meta),
197 ...self.generateFaviconTags(assets.favicon)
198 ]
199 },
200 outputName: childCompilationOutputName,
201 plugin: self
202 }))
203 .then(({ assetTags }) => {
204 // Inject scripts to body unless it set explictly to head
205 const scriptTarget = self.options.inject === 'head' ? 'head' : 'body';
206 // Group assets to `head` and `body` tag arrays
207 const assetGroups = this.generateAssetGroups(assetTags, scriptTarget);
208 // Allow third-party-plugin authors to reorder and change the assetTags once they are grouped
209 return getHtmlWebpackPluginHooks(compilation).alterAssetTagGroups.promise({
210 headTags: assetGroups.headTags,
211 bodyTags: assetGroups.bodyTags,
212 outputName: childCompilationOutputName,
213 plugin: self
214 });
215 });
216
217 // Turn the compiled tempalte into a nodejs function or into a nodejs string
218 const templateEvaluationPromise = Promise.resolve()
219 .then(() => {
220 if ('error' in templateResult) {
221 return self.options.showErrors ? prettyError(templateResult.error, compiler.context).toHtml() : 'ERROR';
222 }
223 // Allow to use a custom function / string instead
224 if (self.options.templateContent !== false) {
225 return self.options.templateContent;
226 }
227 // Once everything is compiled evaluate the html factory
228 // and replace it with its content
229 return ('compiledEntry' in templateResult)
230 ? self.evaluateCompilationResult(compilation, templateResult.compiledEntry.content)
231 : Promise.reject(new Error('Child compilation contained no compiledEntry'));
232 });
233
234 const templateExectutionPromise = Promise.all([assetsPromise, assetTagGroupsPromise, templateEvaluationPromise])
235 // Execute the template
236 .then(([assetsHookResult, assetTags, compilationResult]) => typeof compilationResult !== 'function'
237 ? compilationResult
238 : self.executeTemplate(compilationResult, assetsHookResult.assets, { headTags: assetTags.headTags, bodyTags: assetTags.bodyTags }, compilation));
239
240 const injectedHtmlPromise = Promise.all([assetTagGroupsPromise, templateExectutionPromise])
241 // Allow plugins to change the html before assets are injected
242 .then(([assetTags, html]) => {
243 const pluginArgs = { html, headTags: assetTags.headTags, bodyTags: assetTags.bodyTags, plugin: self, outputName: childCompilationOutputName };
244 return getHtmlWebpackPluginHooks(compilation).afterTemplateExecution.promise(pluginArgs);
245 })
246 .then(({ html, headTags, bodyTags }) => {
247 return self.postProcessHtml(html, assets, { headTags, bodyTags });
248 });
249
250 const emitHtmlPromise = injectedHtmlPromise
251 // Allow plugins to change the html after assets are injected
252 .then((html) => {
253 const pluginArgs = { html, plugin: self, outputName: childCompilationOutputName };
254 return getHtmlWebpackPluginHooks(compilation).beforeEmit.promise(pluginArgs)
255 .then(result => result.html);
256 })
257 .catch(err => {
258 // In case anything went wrong the promise is resolved
259 // with the error message and an error is logged
260 compilation.errors.push(prettyError(err, compiler.context).toString());
261 // Prevent caching
262 self.hash = null;
263 return self.options.showErrors ? prettyError(err, compiler.context).toHtml() : 'ERROR';
264 })
265 .then(html => {
266 // Allow to use [templatehash] as placeholder for the html-webpack-plugin name
267 // See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/
268 // From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214
269 const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => {
270 return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10));
271 });
272 // Add the evaluated html code to the webpack assets
273 compilation.assets[finalOutputName] = {
274 source: () => html,
275 size: () => html.length
276 };
277 return finalOutputName;
278 })
279 .then((finalOutputName) => getHtmlWebpackPluginHooks(compilation).afterEmit.promise({
280 outputName: finalOutputName,
281 plugin: self
282 }).catch(err => {
283 console.error(err);
284 return null;
285 }).then(() => null));
286
287 // Once all files are added to the webpack compilation
288 // let the webpack compiler continue
289 emitHtmlPromise.then(() => {
290 callback();
291 });
292 });
293 }
294
295 /**
296 * Evaluates the child compilation result
297 * @param {WebpackCompilation} compilation
298 * @param {string} source
299 * @returns {Promise<string | (() => string | Promise<string>)>}
300 */
301 evaluateCompilationResult (compilation, source) {
302 if (!source) {
303 return Promise.reject(new Error('The child compilation didn\'t provide a result'));
304 }
305 // The LibraryTemplatePlugin stores the template result in a local variable.
306 // To extract the result during the evaluation this part has to be removed.
307 source = source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '');
308 const template = this.options.template.replace(/^.+!/, '').replace(/\?.+$/, '');
309 const vmContext = vm.createContext(_.extend({ HTML_WEBPACK_PLUGIN: true, require: require, console: console }, global));
310 const vmScript = new vm.Script(source, { filename: template });
311 // Evaluate code and cast to string
312 let newSource;
313 try {
314 newSource = vmScript.runInContext(vmContext);
315 } catch (e) {
316 return Promise.reject(e);
317 }
318 if (typeof newSource === 'object' && newSource.__esModule && newSource.default) {
319 newSource = newSource.default;
320 }
321 return typeof newSource === 'string' || typeof newSource === 'function'
322 ? Promise.resolve(newSource)
323 : Promise.reject(new Error('The loader "' + this.options.template + '" didn\'t return html.'));
324 }
325
326 /**
327 * Generate the template parameters for the template function
328 * @param {WebpackCompilation} compilation
329 * @param {{
330 publicPath: string,
331 js: Array<string>,
332 css: Array<string>,
333 manifest?: string,
334 favicon?: string
335 }} assets
336 * @param {{
337 headTags: HtmlTagObject[],
338 bodyTags: HtmlTagObject[]
339 }} assetTags
340 * @returns {Promise<{[key: any]: any}>}
341 */
342 getTemplateParameters (compilation, assets, assetTags) {
343 const templateParameters = this.options.templateParameters;
344 if (templateParameters === false) {
345 return Promise.resolve({});
346 }
347 if (typeof templateParameters !== 'function' && typeof templateParameters !== 'object') {
348 throw new Error('templateParameters has to be either a function or an object');
349 }
350 const templateParameterFunction = typeof templateParameters === 'function'
351 // A custom function can overwrite the entire template parameter preparation
352 ? templateParameters
353 // If the template parameters is an object merge it with the default values
354 : (compilation, assets, assetTags, options) => Object.assign({},
355 templateParametersGenerator(compilation, assets, assetTags, options),
356 templateParameters
357 );
358 const preparedAssetTags = {
359 headTags: this.prepareAssetTagGroupForRendering(assetTags.headTags),
360 bodyTags: this.prepareAssetTagGroupForRendering(assetTags.bodyTags)
361 };
362 return Promise
363 .resolve()
364 .then(() => templateParameterFunction(compilation, assets, preparedAssetTags, this.options));
365 }
366
367 /**
368 * This function renders the actual html by executing the template function
369 *
370 * @param {(templateParameters) => string | Promise<string>} templateFunction
371 * @param {{
372 publicPath: string,
373 js: Array<string>,
374 css: Array<string>,
375 manifest?: string,
376 favicon?: string
377 }} assets
378 * @param {{
379 headTags: HtmlTagObject[],
380 bodyTags: HtmlTagObject[]
381 }} assetTags
382 * @param {WebpackCompilation} compilation
383 *
384 * @returns Promise<string>
385 */
386 executeTemplate (templateFunction, assets, assetTags, compilation) {
387 // Template processing
388 const templateParamsPromise = this.getTemplateParameters(compilation, assets, assetTags);
389 return templateParamsPromise.then((templateParams) => {
390 try {
391 // If html is a promise return the promise
392 // If html is a string turn it into a promise
393 return templateFunction(templateParams);
394 } catch (e) {
395 compilation.errors.push(new Error('Template execution failed: ' + e));
396 return Promise.reject(e);
397 }
398 });
399 }
400
401 /**
402 * Html Post processing
403 *
404 * @param {any} html
405 * The input html
406 * @param {any} assets
407 * @param {{
408 headTags: HtmlTagObject[],
409 bodyTags: HtmlTagObject[]
410 }} assetTags
411 * The asset tags to inject
412 *
413 * @returns {Promise<string>}
414 */
415 postProcessHtml (html, assets, assetTags) {
416 if (typeof html !== 'string') {
417 return Promise.reject(new Error('Expected html to be a string but got ' + JSON.stringify(html)));
418 }
419 const htmlAfterInjection = this.options.inject
420 ? this.injectAssetsIntoHtml(html, assets, assetTags)
421 : html;
422 const htmlAfterMinification = this.minifyHtml(htmlAfterInjection);
423 return Promise.resolve(htmlAfterMinification);
424 }
425
426 /*
427 * Pushes the content of the given filename to the compilation assets
428 * @param {string} filename
429 * @param {WebpackCompilation} compilation
430 *
431 * @returns {string} file basename
432 */
433 addFileToAssets (filename, compilation) {
434 filename = path.resolve(compilation.compiler.context, filename);
435 return Promise.all([
436 fsStatAsync(filename),
437 fsReadFileAsync(filename)
438 ])
439 .then(([size, source]) => {
440 return {
441 size,
442 source
443 };
444 })
445 .catch(() => Promise.reject(new Error('HtmlWebpackPlugin: could not load file ' + filename)))
446 .then(results => {
447 const basename = path.basename(filename);
448 compilation.fileDependencies.add(filename);
449 compilation.assets[basename] = {
450 source: () => results.source,
451 size: () => results.size.size
452 };
453 return basename;
454 });
455 }
456
457 /**
458 * Helper to sort chunks
459 * @param {string[]} entryNames
460 * @param {string|((entryNameA: string, entryNameB: string) => number)} sortMode
461 * @param {WebpackCompilation} compilation
462 */
463 sortEntryChunks (entryNames, sortMode, compilation) {
464 // Custom function
465 if (typeof sortMode === 'function') {
466 return entryNames.sort(sortMode);
467 }
468 // Check if the given sort mode is a valid chunkSorter sort mode
469 if (typeof chunkSorter[sortMode] !== 'undefined') {
470 return chunkSorter[sortMode](entryNames, compilation, this.options);
471 }
472 throw new Error('"' + sortMode + '" is not a valid chunk sort mode');
473 }
474
475 /**
476 * Return all chunks from the compilation result which match the exclude and include filters
477 * @param {any} chunks
478 * @param {string[]|'all'} includedChunks
479 * @param {string[]} excludedChunks
480 */
481 filterChunks (chunks, includedChunks, excludedChunks) {
482 return chunks.filter(chunkName => {
483 // Skip if the chunks should be filtered and the given chunk was not added explicity
484 if (Array.isArray(includedChunks) && includedChunks.indexOf(chunkName) === -1) {
485 return false;
486 }
487 // Skip if the chunks should be filtered and the given chunk was excluded explicity
488 if (Array.isArray(excludedChunks) && excludedChunks.indexOf(chunkName) !== -1) {
489 return false;
490 }
491 // Add otherwise
492 return true;
493 });
494 }
495
496 /**
497 * Check if the given asset object consists only of hot-update.js files
498 *
499 * @param {{
500 publicPath: string,
501 js: Array<string>,
502 css: Array<string>,
503 manifest?: string,
504 favicon?: string
505 }} assets
506 */
507 isHotUpdateCompilation (assets) {
508 return assets.js.length && assets.js.every((assetPath) => /\.hot-update\.js$/.test(assetPath));
509 }
510
511 /**
512 * The htmlWebpackPluginAssets extracts the asset information of a webpack compilation
513 * for all given entry names
514 * @param {WebpackCompilation} compilation
515 * @param {string[]} entryNames
516 * @returns {{
517 publicPath: string,
518 js: Array<string>,
519 css: Array<string>,
520 manifest?: string,
521 favicon?: string
522 }}
523 */
524 htmlWebpackPluginAssets (compilation, childCompilationOutputName, entryNames) {
525 const compilationHash = compilation.hash;
526
527 /**
528 * @type {string} the configured public path to the asset root
529 * if a path publicPath is set in the current webpack config use it otherwise
530 * fallback to a realtive path
531 */
532 const webpackPublicPath = compilation.mainTemplate.getPublicPath({ hash: compilationHash });
533 const isPublicPathDefined = webpackPublicPath.trim() !== '';
534 let publicPath = isPublicPathDefined
535 // If a hard coded public path exists use it
536 ? webpackPublicPath
537 // If no public path was set get a relative url path
538 : path.relative(path.resolve(compilation.options.output.path, path.dirname(childCompilationOutputName)), compilation.options.output.path)
539 .split(path.sep).join('/');
540
541 if (publicPath.length && publicPath.substr(-1, 1) !== '/') {
542 publicPath += '/';
543 }
544
545 /**
546 * @type {{
547 publicPath: string,
548 js: Array<string>,
549 css: Array<string>,
550 manifest?: string,
551 favicon?: string
552 }}
553 */
554 const assets = {
555 // The public path
556 publicPath: publicPath,
557 // Will contain all js and mjs files
558 js: [],
559 // Will contain all css files
560 css: [],
561 // Will contain the html5 appcache manifest files if it exists
562 manifest: Object.keys(compilation.assets).find(assetFile => path.extname(assetFile) === '.appcache'),
563 // Favicon
564 favicon: undefined
565 };
566
567 // Append a hash for cache busting
568 if (this.options.hash && assets.manifest) {
569 assets.manifest = this.appendHash(assets.manifest, compilationHash);
570 }
571
572 // Extract paths to .js, .mjs and .css files from the current compilation
573 const entryPointPublicPathMap = {};
574 const extensionRegexp = /\.(css|js|mjs)(\?|$)/;
575 for (let i = 0; i < entryNames.length; i++) {
576 const entryName = entryNames[i];
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
593 // Prepend the publicPath and append the hash depending on the
594 // webpack.output.publicPath and hashOptions
595 // E.g. bundle.js -> /bundle.js?hash
596 const entryPointPublicPaths = entryPointFiles
597 .map(chunkFile => {
598 const entryPointPublicPath = publicPath + this.urlencodePath(chunkFile);
599 return this.options.hash
600 ? this.appendHash(entryPointPublicPath, compilationHash)
601 : entryPointPublicPath;
602 });
603
604 entryPointPublicPaths.forEach((entryPointPublicPath) => {
605 const extMatch = extensionRegexp.exec(entryPointPublicPath);
606 // Skip if the public path is not a .css, .mjs or .js file
607 if (!extMatch) {
608 return;
609 }
610 // Skip if this file is already known
611 // (e.g. because of common chunk optimizations)
612 if (entryPointPublicPathMap[entryPointPublicPath]) {
613 return;
614 }
615 entryPointPublicPathMap[entryPointPublicPath] = true;
616 // ext will contain .js or .css, because .mjs recognizes as .js
617 const ext = extMatch[1] === 'mjs' ? 'js' : extMatch[1];
618 assets[ext].push(entryPointPublicPath);
619 });
620 }
621 return assets;
622 }
623
624 /**
625 * Converts a favicon file from disk to a webpack ressource
626 * and returns the url to the ressource
627 *
628 * @param {string|false} faviconFilePath
629 * @param {WebpackCompilation} compilation
630 * @param {string} publicPath
631 * @returns {Promise<string|undefined>}
632 */
633 getFaviconPublicPath (faviconFilePath, compilation, publicPath) {
634 if (!faviconFilePath) {
635 return Promise.resolve(undefined);
636 }
637 return this.addFileToAssets(faviconFilePath, compilation)
638 .then((faviconName) => {
639 const faviconPath = publicPath + faviconName;
640 if (this.options.hash) {
641 return this.appendHash(faviconPath, compilation.hash);
642 }
643 return faviconPath;
644 });
645 }
646
647 /**
648 * Generate meta tags
649 * @returns {HtmlTagObject[]}
650 */
651 getMetaTags () {
652 const metaOptions = this.options.meta;
653 if (metaOptions === false) {
654 return [];
655 }
656 // Make tags self-closing in case of xhtml
657 // Turn { "viewport" : "width=500, initial-scale=1" } into
658 // [{ name:"viewport" content:"width=500, initial-scale=1" }]
659 const metaTagAttributeObjects = Object.keys(metaOptions)
660 .map((metaName) => {
661 const metaTagContent = metaOptions[metaName];
662 return (typeof metaTagContent === 'string') ? {
663 name: metaName,
664 content: metaTagContent
665 } : metaTagContent;
666 })
667 .filter((attribute) => attribute !== false);
668 // Turn [{ name:"viewport" content:"width=500, initial-scale=1" }] into
669 // the html-webpack-plugin tag structure
670 return metaTagAttributeObjects.map((metaTagAttributes) => {
671 if (metaTagAttributes === false) {
672 throw new Error('Invalid meta tag');
673 }
674 return {
675 tagName: 'meta',
676 voidTag: true,
677 attributes: metaTagAttributes
678 };
679 });
680 }
681
682 /**
683 * Generate all tags script for the given file paths
684 * @param {Array<string>} jsAssets
685 * @returns {Array<HtmlTagObject>}
686 */
687 generatedScriptTags (jsAssets) {
688 return jsAssets.map(scriptAsset => ({
689 tagName: 'script',
690 voidTag: false,
691 attributes: {
692 defer: this.options.scriptLoading !== 'blocking',
693 src: scriptAsset
694 }
695 }));
696 }
697
698 /**
699 * Generate all style tags for the given file paths
700 * @param {Array<string>} cssAssets
701 * @returns {Array<HtmlTagObject>}
702 */
703 generateStyleTags (cssAssets) {
704 return cssAssets.map(styleAsset => ({
705 tagName: 'link',
706 voidTag: true,
707 attributes: {
708 href: styleAsset,
709 rel: 'stylesheet'
710 }
711 }));
712 }
713
714 /**
715 * Generate an optional base tag
716 * @param { false
717 | string
718 | {[attributeName: string]: string} // attributes e.g. { href:"http://example.com/page.html" target:"_blank" }
719 } baseOption
720 * @returns {Array<HtmlTagObject>}
721 */
722 generateBaseTag (baseOption) {
723 if (baseOption === false) {
724 return [];
725 } else {
726 return [{
727 tagName: 'base',
728 voidTag: true,
729 attributes: (typeof baseOption === 'string') ? {
730 href: baseOption
731 } : baseOption
732 }];
733 }
734 }
735
736 /**
737 * Generate all meta tags for the given meta configuration
738 * @param {false | {
739 [name: string]:
740 false // disabled
741 | string // name content pair e.g. {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`
742 | {[attributeName: string]: string|boolean} // custom properties e.g. { name:"viewport" content:"width=500, initial-scale=1" }
743 }} metaOptions
744 * @returns {Array<HtmlTagObject>}
745 */
746 generatedMetaTags (metaOptions) {
747 if (metaOptions === false) {
748 return [];
749 }
750 // Make tags self-closing in case of xhtml
751 // Turn { "viewport" : "width=500, initial-scale=1" } into
752 // [{ name:"viewport" content:"width=500, initial-scale=1" }]
753 const metaTagAttributeObjects = Object.keys(metaOptions)
754 .map((metaName) => {
755 const metaTagContent = metaOptions[metaName];
756 return (typeof metaTagContent === 'string') ? {
757 name: metaName,
758 content: metaTagContent
759 } : metaTagContent;
760 })
761 .filter((attribute) => attribute !== false);
762 // Turn [{ name:"viewport" content:"width=500, initial-scale=1" }] into
763 // the html-webpack-plugin tag structure
764 return metaTagAttributeObjects.map((metaTagAttributes) => {
765 if (metaTagAttributes === false) {
766 throw new Error('Invalid meta tag');
767 }
768 return {
769 tagName: 'meta',
770 voidTag: true,
771 attributes: metaTagAttributes
772 };
773 });
774 }
775
776 /**
777 * Generate a favicon tag for the given file path
778 * @param {string| undefined} faviconPath
779 * @returns {Array<HtmlTagObject>}
780 */
781 generateFaviconTags (faviconPath) {
782 if (!faviconPath) {
783 return [];
784 }
785 return [{
786 tagName: 'link',
787 voidTag: true,
788 attributes: {
789 rel: 'icon',
790 href: faviconPath
791 }
792 }];
793 }
794
795 /**
796 * Group assets to head and bottom tags
797 *
798 * @param {{
799 scripts: Array<HtmlTagObject>;
800 styles: Array<HtmlTagObject>;
801 meta: Array<HtmlTagObject>;
802 }} assetTags
803 * @param {"body" | "head"} scriptTarget
804 * @returns {{
805 headTags: Array<HtmlTagObject>;
806 bodyTags: Array<HtmlTagObject>;
807 }}
808 */
809 generateAssetGroups (assetTags, scriptTarget) {
810 /** @type {{ headTags: Array<HtmlTagObject>; bodyTags: Array<HtmlTagObject>; }} */
811 const result = {
812 headTags: [
813 ...assetTags.meta,
814 ...assetTags.styles
815 ],
816 bodyTags: []
817 };
818 // Add script tags to head or body depending on
819 // the htmlPluginOptions
820 if (scriptTarget === 'body') {
821 result.bodyTags.push(...assetTags.scripts);
822 } else {
823 // If script loading is blocking add the scripts to the end of the head
824 // If script loading is non-blocking add the scripts infront of the css files
825 const insertPosition = this.options.scriptLoading === 'blocking' ? result.headTags.length : assetTags.meta.length;
826 result.headTags.splice(insertPosition, 0, ...assetTags.scripts);
827 }
828 return result;
829 }
830
831 /**
832 * Add toString methods for easier rendering
833 * inside the template
834 *
835 * @param {Array<HtmlTagObject>} assetTagGroup
836 * @returns {Array<HtmlTagObject>}
837 */
838 prepareAssetTagGroupForRendering (assetTagGroup) {
839 const xhtml = this.options.xhtml;
840 return HtmlTagArray.from(assetTagGroup.map((assetTag) => {
841 const copiedAssetTag = Object.assign({}, assetTag);
842 copiedAssetTag.toString = function () {
843 return htmlTagObjectToString(this, xhtml);
844 };
845 return copiedAssetTag;
846 }));
847 }
848
849 /**
850 * Injects the assets into the given html string
851 *
852 * @param {string} html
853 * The input html
854 * @param {any} assets
855 * @param {{
856 headTags: HtmlTagObject[],
857 bodyTags: HtmlTagObject[]
858 }} assetTags
859 * The asset tags to inject
860 *
861 * @returns {string}
862 */
863 injectAssetsIntoHtml (html, assets, assetTags) {
864 const htmlRegExp = /(<html[^>]*>)/i;
865 const headRegExp = /(<\/head\s*>)/i;
866 const bodyRegExp = /(<\/body\s*>)/i;
867 const body = assetTags.bodyTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, this.options.xhtml));
868 const head = assetTags.headTags.map((assetTagObject) => htmlTagObjectToString(assetTagObject, this.options.xhtml));
869
870 if (body.length) {
871 if (bodyRegExp.test(html)) {
872 // Append assets to body element
873 html = html.replace(bodyRegExp, match => body.join('') + match);
874 } else {
875 // Append scripts to the end of the file if no <body> element exists:
876 html += body.join('');
877 }
878 }
879
880 if (head.length) {
881 // Create a head tag if none exists
882 if (!headRegExp.test(html)) {
883 if (!htmlRegExp.test(html)) {
884 html = '<head></head>' + html;
885 } else {
886 html = html.replace(htmlRegExp, match => match + '<head></head>');
887 }
888 }
889
890 // Append assets to head element
891 html = html.replace(headRegExp, match => head.join('') + match);
892 }
893
894 // Inject manifest into the opening html tag
895 if (assets.manifest) {
896 html = html.replace(/(<html[^>]*)(>)/i, (match, start, end) => {
897 // Append the manifest only if no manifest was specified
898 if (/\smanifest\s*=/.test(match)) {
899 return match;
900 }
901 return start + ' manifest="' + assets.manifest + '"' + end;
902 });
903 }
904 return html;
905 }
906
907 /**
908 * Appends a cache busting hash to the query string of the url
909 * E.g. http://localhost:8080/ -> http://localhost:8080/?50c9096ba6183fd728eeb065a26ec175
910 * @param {string} url
911 * @param {string} hash
912 */
913 appendHash (url, hash) {
914 if (!url) {
915 return url;
916 }
917 return url + (url.indexOf('?') === -1 ? '?' : '&') + hash;
918 }
919
920 /**
921 * Encode each path component using `encodeURIComponent` as files can contain characters
922 * which needs special encoding in URLs like `+ `.
923 *
924 * Valid filesystem characters which need to be encoded for urls:
925 *
926 * # pound, % percent, & ampersand, { left curly bracket, } right curly bracket,
927 * \ back slash, < left angle bracket, > right angle bracket, * asterisk, ? question mark,
928 * blank spaces, $ dollar sign, ! exclamation point, ' single quotes, " double quotes,
929 * : colon, @ at sign, + plus sign, ` backtick, | pipe, = equal sign
930 *
931 * However the query string must not be encoded:
932 *
933 * fo:demonstration-path/very fancy+name.js?path=/home?value=abc&value=def#zzz
934 * ^ ^ ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^
935 * | | | | | | | || | | | | |
936 * encoded | | encoded | | || | | | | |
937 * ignored ignored ignored ignored ignored
938 *
939 * @param {string} filePath
940 */
941 urlencodePath (filePath) {
942 // People use the filepath in quite unexpected ways.
943 // Try to extract the first querystring of the url:
944 //
945 // some+path/demo.html?value=abc?def
946 //
947 const queryStringStart = filePath.indexOf('?');
948 const urlPath = queryStringStart === -1 ? filePath : filePath.substr(0, queryStringStart);
949 const queryString = filePath.substr(urlPath.length);
950 // Encode all parts except '/' which are not part of the querystring:
951 const encodedUrlPath = urlPath.split('/').map(encodeURIComponent).join('/');
952 return encodedUrlPath + queryString;
953 }
954
955 /**
956 * Helper to return the absolute template path with a fallback loader
957 * @param {string} template
958 * The path to the template e.g. './index.html'
959 * @param {string} context
960 * The webpack base resolution path for relative paths e.g. process.cwd()
961 */
962 getFullTemplatePath (template, context) {
963 if (template === 'auto') {
964 template = path.resolve(context, 'src/index.ejs');
965 if (!fs.existsSync(template)) {
966 template = path.join(__dirname, 'default_index.ejs');
967 }
968 }
969 // If the template doesn't use a loader use the lodash template loader
970 if (template.indexOf('!') === -1) {
971 template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template);
972 }
973 // Resolve template path
974 return template.replace(
975 /([!])([^/\\][^!?]+|[^/\\!?])($|\?[^!?\n]+$)/,
976 (match, prefix, filepath, postfix) => prefix + path.resolve(filepath) + postfix);
977 }
978
979 /**
980 * Minify the given string using html-minifier-terser
981 *
982 * As this is a breaking change to html-webpack-plugin 3.x
983 * provide an extended error message to explain how to get back
984 * to the old behaviour
985 *
986 * @param {string} html
987 */
988 minifyHtml (html) {
989 if (typeof this.options.minify !== 'object') {
990 return html;
991 }
992 try {
993 return require('html-minifier-terser').minify(html, this.options.minify);
994 } catch (e) {
995 const isParseError = String(e.message).indexOf('Parse Error') === 0;
996 if (isParseError) {
997 e.message = 'html-webpack-plugin could not minify the generated output.\n' +
998 'In production mode the html minifcation is enabled by default.\n' +
999 'If you are not generating a valid html output please disable it manually.\n' +
1000 'You can do so by adding the following setting to your HtmlWebpackPlugin config:\n|\n|' +
1001 ' minify: false\n|\n' +
1002 'See https://github.com/jantimon/html-webpack-plugin#options for details.\n\n' +
1003 'For parser dedicated bugs please create an issue here:\n' +
1004 'https://danielruf.github.io/html-minifier-terser/' +
1005 '\n' + e.message;
1006 }
1007 throw e;
1008 }
1009 }
1010
1011 /**
1012 * Helper to return a sorted unique array of all asset files out of the
1013 * asset object
1014 */
1015 getAssetFiles (assets) {
1016 const files = _.uniq(Object.keys(assets).filter(assetType => assetType !== 'chunks' && assets[assetType]).reduce((files, assetType) => files.concat(assets[assetType]), []));
1017 files.sort();
1018 return files;
1019 }
1020}
1021
1022/**
1023 * The default for options.templateParameter
1024 * Generate the template parameters
1025 *
1026 * Generate the template parameters for the template function
1027 * @param {WebpackCompilation} compilation
1028 * @param {{
1029 publicPath: string,
1030 js: Array<string>,
1031 css: Array<string>,
1032 manifest?: string,
1033 favicon?: string
1034 }} assets
1035 * @param {{
1036 headTags: HtmlTagObject[],
1037 bodyTags: HtmlTagObject[]
1038 }} assetTags
1039 * @param {ProcessedHtmlWebpackOptions} options
1040 * @returns {TemplateParameter}
1041 */
1042function templateParametersGenerator (compilation, assets, assetTags, options) {
1043 return {
1044 compilation: compilation,
1045 webpackConfig: compilation.options,
1046 htmlWebpackPlugin: {
1047 tags: assetTags,
1048 files: assets,
1049 options: options
1050 }
1051 };
1052}
1053
1054// Statics:
1055/**
1056 * The major version number of this plugin
1057 */
1058HtmlWebpackPlugin.version = 4;
1059
1060/**
1061 * A static helper to get the hooks for this plugin
1062 *
1063 * Usage: HtmlWebpackPlugin.getHooks(compilation).HOOK_NAME.tapAsync('YourPluginName', () => { ... });
1064 */
1065HtmlWebpackPlugin.getHooks = getHtmlWebpackPluginHooks;
1066HtmlWebpackPlugin.createHtmlTagObject = createHtmlTagObject;
1067
1068module.exports = HtmlWebpackPlugin;