Webpack through yarn: stop process and exit code 1 on warning

Viewed 2027

Running a build process with Webpack, I've got some warnings. I consider some of this warning error, and IMO, it should stop the build process.

> yarn build:staging
yarn run v1.10.1
$ webpack --env.staging --env.NODE_ENV=staging
Version: webpack 4.23.1

[...]

[795] ./src/index.css 1.05 KiB {0} [built]
    + 793 hidden modules

WARNING in ./src/assets/banner/3.jpg
Module build failed (from ./node_modules/image-webpack-loader/index.js):
Error: Command failed: /home/circleci/repo/node_modules/mozjpeg/vendor/cjpeg
/home/circleci/repo/node_modules/mozjpeg/vendor/cjpeg: error while loading shared libraries: libpng16.so.16: cannot open shared object file: No such file or directory


    at Promise.all.then.arr (/home/circleci/repo/node_modules/imagemin-mozjpeg/node_modules/execa/index.js:236:11)
    at process._tickCallback (internal/process/next_tick.js:68:7)
@ ./src/assets/banner sync ^\.\/.*\.jpg$ ./3.jpg
@ ./src/scenes/home/index.js
@ ./src/router.js
@ ./src/index.js
@ multi babel-polyfill ./src/index.js

[...]

Done in 26.03s.

This "warning" should stop the whole process and exit. Is there a way to tell to webpack with yarn as a driver to stop on certain kind of warning (and return an error code)? My CI process considers everything is alright, this is not the behavior I expected.

1 Answers

You could access errors and warnings out of a Webpack instance from its stats object and then choose to reject/exit the process: https://github.com/webpack/docs/wiki/node.js-api

For instance:

if(stats.errors.length > 0 || process.warning.length > 0) {
  process.exit()
Related