webpack-compiler.js 998 B

123456789101112131415161718192021222324252627282930313233
  1. const webpack = require('webpack');
  2. const debug = require('debug')('app:build:webpack-compiler');
  3. function webpackCompiler(webpackConfig) {
  4. return new Promise((resolve, reject) => {
  5. const compiler = webpack(webpackConfig);
  6. compiler.run((err, stats) => {
  7. if (err) {
  8. debug('Webpack compiler encountered a fatal error.', err);
  9. return reject(err);
  10. }
  11. const jsonStats = stats.toJson();
  12. debug('Webpack compile completed.');
  13. if (jsonStats.errors.length > 0) {
  14. debug('Webpack compiler encountered errors.');
  15. debug(jsonStats.errors.join('\n'));
  16. return reject(new Error('Webpack compiler encountered errors'));
  17. }
  18. if (jsonStats.warnings.length > 0) {
  19. debug('Webpack compiler encountered warnings.');
  20. debug(jsonStats.warnings.join('\n'));
  21. } else {
  22. debug('No errors or warnings encountered.');
  23. }
  24. return resolve(jsonStats);
  25. });
  26. });
  27. }
  28. module.exports = webpackCompiler;