From 7df269fd2a840d0800cb259bd559edb0b766e7ab Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Wed, 29 Jan 2020 18:49:15 +0100 Subject: [PATCH] feat: Provide a verbose error message if html minification failed --- index.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index c8698518..50e7e917 100644 --- a/index.js +++ b/index.js @@ -446,9 +446,7 @@ class HtmlWebpackPlugin { const htmlAfterInjection = this.options.inject ? this.injectAssetsIntoHtml(html, assets, assetTags) : html; - const htmlAfterMinification = typeof this.options.minify === 'object' - ? require('html-minifier-terser').minify(htmlAfterInjection, this.options.minify) - : htmlAfterInjection; + const htmlAfterMinification = this.minifyHtml(htmlAfterInjection); return Promise.resolve(htmlAfterMinification); } @@ -965,6 +963,38 @@ class HtmlWebpackPlugin { (match, prefix, filepath, postfix) => prefix + path.resolve(filepath) + postfix); } + /** + * Minify the given string using html-minifier-terser + * + * As this is a breaking change to html-webpack-plugin 3.x + * provide an extended error message to explain how to get back + * to the old behaviour + * + * @param {string} html + */ + minifyHtml (html) { + if (typeof this.options.minify !== 'object') { + return html; + } + try { + return require('html-minifier-terser').minify(html, this.options.minify); + } catch (e) { + const isParseError = String(e.message).indexOf('Parse Error') === 0; + if (isParseError) { + e.message = 'html-webpack-plugin could not minify the generated output.\n' + + 'In production mode the html minifcation is enabled by default.\n' + + 'If you are not generating a valid html output please disable it manually.\n' + + 'You can do so by adding the following setting to your HtmlWebpackPlugin config:\n|\n|' + + ' minify: false\n|\n' + + 'See https://github.com/jantimon/html-webpack-plugin#options for details.\n\n' + + 'For parser dedicated bugs please create an issue here:\n' + + 'https://danielruf.github.io/html-minifier-terser/' + + '\n' + e.message; + } + throw e; + } + } + /** * Helper to return a sorted unique array of all asset files out of the * asset object