Skip to content

Commit 7df269f

Browse files
committedMar 17, 2020
feat: Provide a verbose error message if html minification failed
1 parent 1d66e53 commit 7df269f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed
 

‎index.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,7 @@ class HtmlWebpackPlugin {
446446
const htmlAfterInjection = this.options.inject
447447
? this.injectAssetsIntoHtml(html, assets, assetTags)
448448
: html;
449-
const htmlAfterMinification = typeof this.options.minify === 'object'
450-
? require('html-minifier-terser').minify(htmlAfterInjection, this.options.minify)
451-
: htmlAfterInjection;
449+
const htmlAfterMinification = this.minifyHtml(htmlAfterInjection);
452450
return Promise.resolve(htmlAfterMinification);
453451
}
454452

@@ -965,6 +963,38 @@ class HtmlWebpackPlugin {
965963
(match, prefix, filepath, postfix) => prefix + path.resolve(filepath) + postfix);
966964
}
967965

966+
/**
967+
* Minify the given string using html-minifier-terser
968+
*
969+
* As this is a breaking change to html-webpack-plugin 3.x
970+
* provide an extended error message to explain how to get back
971+
* to the old behaviour
972+
*
973+
* @param {string} html
974+
*/
975+
minifyHtml (html) {
976+
if (typeof this.options.minify !== 'object') {
977+
return html;
978+
}
979+
try {
980+
return require('html-minifier-terser').minify(html, this.options.minify);
981+
} catch (e) {
982+
const isParseError = String(e.message).indexOf('Parse Error') === 0;
983+
if (isParseError) {
984+
e.message = 'html-webpack-plugin could not minify the generated output.\n' +
985+
'In production mode the html minifcation is enabled by default.\n' +
986+
'If you are not generating a valid html output please disable it manually.\n' +
987+
'You can do so by adding the following setting to your HtmlWebpackPlugin config:\n|\n|' +
988+
' minify: false\n|\n' +
989+
'See https://github.com/jantimon/html-webpack-plugin#options for details.\n\n' +
990+
'For parser dedicated bugs please create an issue here:\n' +
991+
'https://danielruf.github.io/html-minifier-terser/' +
992+
'\n' + e.message;
993+
}
994+
throw e;
995+
}
996+
}
997+
968998
/**
969999
* Helper to return a sorted unique array of all asset files out of the
9701000
* asset object

0 commit comments

Comments
 (0)
Please sign in to comment.