createStatsFormatter.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = createStatsFormatter;
  6. var _os = require('os');
  7. var _chalk = require('chalk');
  8. var _chalk2 = _interopRequireDefault(_chalk);
  9. var _RequestShortener = require('webpack/lib/RequestShortener');
  10. var _RequestShortener2 = _interopRequireDefault(_RequestShortener);
  11. var _formatUtil = require('./formatUtil');
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var createGetFile = function createGetFile(requestShortener) {
  14. return function (e) {
  15. /* istanbul ignore if */
  16. if (e.file) {
  17. // webpack does this also, so there must be case when this happens
  18. return e.file;
  19. } else if (e.module && e.module.readableIdentifier && typeof e.module.readableIdentifier === 'function') {
  20. // if we got a module, build a file path to the module without loader information
  21. return (0, _formatUtil.stripLoaderFromPath)(e.module.readableIdentifier(requestShortener));
  22. }
  23. /* istanbul ignore next */
  24. return null;
  25. };
  26. };
  27. // helper to transform strings in errors
  28. var ensureWebpackErrors = function ensureWebpackErrors(errors) {
  29. return errors.map(function (e) {
  30. /* istanbul ignore if */
  31. if (typeof e === 'string') {
  32. // webpack does this also, so there must be case when this happens
  33. return { message: e };
  34. }
  35. return e;
  36. });
  37. };
  38. var prependWarning = function prependWarning(message) {
  39. return _chalk2.default.yellow('Warning') + ' ' + message;
  40. };
  41. var prependError = function prependError(message) {
  42. return _chalk2.default.red('Error') + ' ' + message;
  43. };
  44. function createStatsFormatter(rootPath) {
  45. var requestShortener = new _RequestShortener2.default(rootPath);
  46. var getFile = createGetFile(requestShortener);
  47. var formatError = function formatError(err) {
  48. var lines = [];
  49. var file = getFile(err);
  50. /* istanbul ignore else */
  51. if (file != null) {
  52. lines.push('in ' + _chalk2.default.underline(file));
  53. lines.push('');
  54. } else {
  55. // got no file, that happens only for more generic errors like the following from node-sass
  56. // Missing binding /mocha-webpack-example/node_modules/node-sass/vendor/linux-x64-48/binding.node
  57. // Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 6.x
  58. // ...
  59. // just print 2 lines like file
  60. lines.push('');
  61. lines.push('');
  62. }
  63. lines.push((0, _formatUtil.formatErrorMessage)(err.message));
  64. return lines.join(_os.EOL);
  65. };
  66. return function statsFormatter(stats) {
  67. var compilation = stats.compilation;
  68. return {
  69. errors: ensureWebpackErrors(compilation.errors).map(formatError).map(prependError),
  70. warnings: ensureWebpackErrors(compilation.warnings).map(formatError).map(prependWarning)
  71. };
  72. };
  73. }