webpack-formatter.js 1.6 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.createWebpackFormatter = void 0;
  7. const os_1 = __importDefault(require("os"));
  8. const path_1 = __importDefault(require("path"));
  9. const chalk_1 = __importDefault(require("chalk"));
  10. const issue_1 = require("../issue");
  11. const forward_slash_1 = require("../utils/path/forward-slash");
  12. const relative_to_context_1 = require("../utils/path/relative-to-context");
  13. function createWebpackFormatter(formatter, pathType) {
  14. // mimics webpack error formatter
  15. return function webpackFormatter(issue) {
  16. const color = issue.severity === 'warning' ? chalk_1.default.yellow.bold : chalk_1.default.red.bold;
  17. const severity = issue.severity.toUpperCase();
  18. if (issue.file) {
  19. let location = chalk_1.default.bold(pathType === 'absolute'
  20. ? (0, forward_slash_1.forwardSlash)(path_1.default.resolve(issue.file))
  21. : (0, relative_to_context_1.relativeToContext)(issue.file, process.cwd()));
  22. if (issue.location) {
  23. location += `:${chalk_1.default.green.bold((0, issue_1.formatIssueLocation)(issue.location))}`;
  24. }
  25. return [`${color(severity)} in ${location}`, formatter(issue), ''].join(os_1.default.EOL);
  26. }
  27. else {
  28. return [`${color(severity)} in ` + formatter(issue), ''].join(os_1.default.EOL);
  29. }
  30. };
  31. }
  32. exports.createWebpackFormatter = createWebpackFormatter;