UNPKG

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