UNPKG

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