@@ -446,9 +446,7 @@ class HtmlWebpackPlugin {
446
446
const htmlAfterInjection = this . options . inject
447
447
? this . injectAssetsIntoHtml ( html , assets , assetTags )
448
448
: 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 ) ;
452
450
return Promise . resolve ( htmlAfterMinification ) ;
453
451
}
454
452
@@ -965,6 +963,38 @@ class HtmlWebpackPlugin {
965
963
( match , prefix , filepath , postfix ) => prefix + path . resolve ( filepath ) + postfix ) ;
966
964
}
967
965
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
+
968
998
/**
969
999
* Helper to return a sorted unique array of all asset files out of the
970
1000
* asset object
0 commit comments