From 8f7a6d71c88f7ab9d94f9dc8d1ae6abdf7dc70c3 Mon Sep 17 00:00:00 2001 From: Florian Dreier Date: Sun, 6 Dec 2020 12:50:25 +0100 Subject: [PATCH 1/3] Fail build when WebPack produces errors and optionally on warnings Fixes #1527 --- plugins/plugin-webpack/README.md | 1 + plugins/plugin-webpack/plugin.js | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/plugin-webpack/README.md b/plugins/plugin-webpack/README.md index 1fde4cce4b..d31defec5f 100644 --- a/plugins/plugin-webpack/README.md +++ b/plugins/plugin-webpack/README.md @@ -57,6 +57,7 @@ The options object is optional. - `extendConfig: (config: WebpackConfig) => WebpackConfig` - extend your webpack config, see below. - `manifest: boolean | string` - Enable generating a manifest file. The default value is `false`, the default file name is `./asset-manifest.json` if setting manifest to `true`. The relative path is resolved from the output directory. - `htmlMinifierOptions: boolean | object` - [See below](#minify-html). +- `ignoreWarnings: boolean` - Does not fail the build when Webpack emits warnings. The default value is `true`. #### Extending The Default Webpack Config diff --git a/plugins/plugin-webpack/plugin.js b/plugins/plugin-webpack/plugin.js index f8711f27bf..244cd0615d 100644 --- a/plugins/plugin-webpack/plugin.js +++ b/plugins/plugin-webpack/plugin.js @@ -199,6 +199,8 @@ module.exports = function plugin(config, args = {}) { ? './asset-manifest.json' : undefined; + const ignoreWarnings = args.ignoreWarnings === undefined ? true : args.ignoreWarnings + // Webpack handles minification for us, so its safe to always // disable Snowpack's default minifier. config.buildOptions.minify = false; @@ -361,10 +363,11 @@ module.exports = function plugin(config, args = {}) { reject(err); return; } - if (stats.hasErrors()) { + if (stats.hasErrors() || (stats.hasWarnings() && !ignoreWarnings)) { const info = stats.toJson(extendedConfig.stats); console.error(info.warnings.join('\n-----\n')); console.error(info.errors.join('\n-----\n')); + reject(Error(`Webpack build failed with ${info.errors} error(s) and ${info.warnings} warning(s)`)); } resolve(stats); }); From bf8861bb0f4a0d8b6ca0174b1db326b2f3ca59c7 Mon Sep 17 00:00:00 2001 From: Florian Dreier Date: Mon, 7 Dec 2020 08:11:53 +0100 Subject: [PATCH 2/3] Rework --- plugins/plugin-webpack/README.md | 2 +- plugins/plugin-webpack/plugin.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/plugins/plugin-webpack/README.md b/plugins/plugin-webpack/README.md index d31defec5f..b1031c6495 100644 --- a/plugins/plugin-webpack/README.md +++ b/plugins/plugin-webpack/README.md @@ -57,7 +57,7 @@ The options object is optional. - `extendConfig: (config: WebpackConfig) => WebpackConfig` - extend your webpack config, see below. - `manifest: boolean | string` - Enable generating a manifest file. The default value is `false`, the default file name is `./asset-manifest.json` if setting manifest to `true`. The relative path is resolved from the output directory. - `htmlMinifierOptions: boolean | object` - [See below](#minify-html). -- `ignoreWarnings: boolean` - Does not fail the build when Webpack emits warnings. The default value is `true`. +- `failOnWarnings: boolean` - Does fail the build when Webpack emits warnings. The default value is `false`. #### Extending The Default Webpack Config diff --git a/plugins/plugin-webpack/plugin.js b/plugins/plugin-webpack/plugin.js index 244cd0615d..0453891759 100644 --- a/plugins/plugin-webpack/plugin.js +++ b/plugins/plugin-webpack/plugin.js @@ -199,8 +199,6 @@ module.exports = function plugin(config, args = {}) { ? './asset-manifest.json' : undefined; - const ignoreWarnings = args.ignoreWarnings === undefined ? true : args.ignoreWarnings - // Webpack handles minification for us, so its safe to always // disable Snowpack's default minifier. config.buildOptions.minify = false; @@ -363,11 +361,16 @@ module.exports = function plugin(config, args = {}) { reject(err); return; } - if (stats.hasErrors() || (stats.hasWarnings() && !ignoreWarnings)) { - const info = stats.toJson(extendedConfig.stats); - console.error(info.warnings.join('\n-----\n')); - console.error(info.errors.join('\n-----\n')); - reject(Error(`Webpack build failed with ${info.errors} error(s) and ${info.warnings} warning(s)`)); + const info = stats.toJson(extendedConfig.stats); + if (stats.hasErrors()) { + console.error('Webpack errors:\n' + info.errors.join('\n-----\n')); + reject(Error(`Webpack failed with ${info.errors} error(s).`)); + } + if (stats.hasWarnings()) { + console.error('Webpack warnings:\n' + info.warnings.join('\n-----\n')); + if (args.failOnWarnings) { + reject(Error(`Webpack failed with ${info.warnings} warnings(s).`)); + } } resolve(stats); }); From 74f260d78a77bbcbeda2027a67b0aac81501d0a3 Mon Sep 17 00:00:00 2001 From: Florian Dreier Date: Mon, 7 Dec 2020 22:13:34 +0100 Subject: [PATCH 3/3] Added returns --- plugins/plugin-webpack/plugin.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/plugin-webpack/plugin.js b/plugins/plugin-webpack/plugin.js index 0453891759..2db7d7a178 100644 --- a/plugins/plugin-webpack/plugin.js +++ b/plugins/plugin-webpack/plugin.js @@ -365,11 +365,13 @@ module.exports = function plugin(config, args = {}) { if (stats.hasErrors()) { console.error('Webpack errors:\n' + info.errors.join('\n-----\n')); reject(Error(`Webpack failed with ${info.errors} error(s).`)); + return; } if (stats.hasWarnings()) { console.error('Webpack warnings:\n' + info.warnings.join('\n-----\n')); if (args.failOnWarnings) { reject(Error(`Webpack failed with ${info.warnings} warnings(s).`)); + return; } } resolve(stats);