From 23f083a19b43be3bfded194d9a825654bc625773 Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Tue, 5 May 2020 16:42:14 +0900 Subject: [PATCH] Fail Storybook build if there are webpack compilation warnings Create a Webpack plugin which looks for warnings at the end of the build. Reference doc: https://webpack.js.org/contribute/writing-a-plugin/ --- packages/storybook/.storybook/main.js | 4 +++ .../webpack-plugin-fail-build-on-warning.js | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 packages/storybook/.storybook/webpack-plugin-fail-build-on-warning.js diff --git a/packages/storybook/.storybook/main.js b/packages/storybook/.storybook/main.js index c187dc5f56..cf003d0981 100644 --- a/packages/storybook/.storybook/main.js +++ b/packages/storybook/.storybook/main.js @@ -1,4 +1,5 @@ const path = require('path'); +const WebpackPluginFailBuildOnWarning = require('./webpack-plugin-fail-build-on-warning'); module.exports = { stories: [ @@ -53,6 +54,9 @@ module.exports = { ({ constructor }) => constructor.name !== 'ProgressPlugin', ); + // Fail storybook build if there are webpack warnings + config.plugins.push(new WebpackPluginFailBuildOnWarning()) + return config; }, }; diff --git a/packages/storybook/.storybook/webpack-plugin-fail-build-on-warning.js b/packages/storybook/.storybook/webpack-plugin-fail-build-on-warning.js new file mode 100644 index 0000000000..4359035161 --- /dev/null +++ b/packages/storybook/.storybook/webpack-plugin-fail-build-on-warning.js @@ -0,0 +1,30 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class WebpackPluginFailBuildOnWarning { + apply(compiler) { + compiler.hooks.done.tap('FailBuildOnWarning', stats => { + if (stats.compilation.warnings.length > 0) { + process.on('beforeExit', () => { + console.log(`You have ${stats.compilation.warnings.length} warning(s) in your webpack build. Exiting process as error.`) + process.exit(1); + }); + } + }) + } +} + +module.exports = WebpackPluginFailBuildOnWarning;