log.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.errorFormatter = exports.removeWarnFormatter = exports.removeInfoFormatter = exports.h5ServeFormatter = exports.formatAtFilename = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const picocolors_1 = __importDefault(require("picocolors"));
  10. const shared_1 = require("@vue/shared");
  11. const utils_1 = require("../utils");
  12. const constants_1 = require("../constants");
  13. const ast_1 = require("../vite/utils/ast");
  14. const utils_2 = require("../vite/plugins/vitejs/utils");
  15. const SIGNAL_H5_LOCAL = ' ➜ Local:';
  16. const SIGNAL_H5_NETWORK = ' ➜ Network:';
  17. const networkLogs = [];
  18. const ZERO_WIDTH_CHAR = {
  19. NOTE: '',
  20. WARNING: '\u200B',
  21. ERROR: '\u200C',
  22. backup0: '\u200D',
  23. backup1: '\u200E',
  24. backup2: '\u200F',
  25. backup3: '\uFEFF',
  26. };
  27. function overridedConsole(name, oldFn, char) {
  28. console[name] = function (...args) {
  29. oldFn.apply(this, args.map((arg) => {
  30. let item;
  31. if (typeof arg !== 'object') {
  32. item = `${char}${arg}${char}`;
  33. }
  34. else {
  35. item = `${char}${JSON.stringify(arg)}${char}`;
  36. }
  37. return item;
  38. }));
  39. };
  40. }
  41. if (typeof console !== 'undefined') {
  42. overridedConsole('warn', console.log, ZERO_WIDTH_CHAR.WARNING);
  43. // overridedConsole('error', console.error, ZERO_WIDTH_CHAR.ERROR)
  44. }
  45. function formatAtFilename(filename, line, column) {
  46. const file = path_1.default.relative(process.env.UNI_INPUT_DIR, filename.replace('\x00', '').split('?')[0]);
  47. return `at ${picocolors_1.default.cyan((0, utils_1.normalizePath)(file === 'pages-json-uts' ? 'pages.json' : file) +
  48. ':' +
  49. (line || 1) +
  50. ':' +
  51. (column || 0))}`;
  52. }
  53. exports.formatAtFilename = formatAtFilename;
  54. exports.h5ServeFormatter = {
  55. test(msg) {
  56. return msg.includes(SIGNAL_H5_LOCAL) || msg.includes(SIGNAL_H5_NETWORK);
  57. },
  58. format(msg) {
  59. if (msg.includes(SIGNAL_H5_NETWORK)) {
  60. networkLogs.push(msg.replace('➜ ', '*'));
  61. process.nextTick(() => {
  62. if (networkLogs.length) {
  63. // 延迟打印所有 network,仅最后一个 network 替换 ➜ 为 -,通知 hbx
  64. const len = networkLogs.length - 1;
  65. networkLogs[len] = networkLogs[len].replace('* Network', '- Network');
  66. console.log(networkLogs.join('\n'));
  67. networkLogs.length = 0;
  68. }
  69. });
  70. return '';
  71. }
  72. if (msg.includes(SIGNAL_H5_LOCAL)) {
  73. return msg.replace('➜ ', '-');
  74. }
  75. return msg.replace('➜ ', '*');
  76. },
  77. };
  78. const REMOVED_MSGS = [
  79. 'build started...',
  80. (msg) => {
  81. return /built in [0-9]+ms\./.test(msg);
  82. },
  83. 'watching for file changes...',
  84. ];
  85. exports.removeInfoFormatter = {
  86. test(msg) {
  87. return !!REMOVED_MSGS.find((m) => ((0, shared_1.isString)(m) ? msg.includes(m) : m(msg)));
  88. },
  89. format() {
  90. return '';
  91. },
  92. };
  93. const REMOVED_WARN_MSGS = [];
  94. exports.removeWarnFormatter = {
  95. test(msg) {
  96. return !!REMOVED_WARN_MSGS.find((m) => msg.includes(m));
  97. },
  98. format() {
  99. return '';
  100. },
  101. };
  102. exports.errorFormatter = {
  103. test(_, opts) {
  104. return !!(opts && opts.error);
  105. },
  106. format(_, opts) {
  107. return buildErrorMessage(opts.error, [], false);
  108. },
  109. };
  110. function buildErrorMessage(err, args = [], includeStack = true) {
  111. if (err.plugin) {
  112. args.push(`${picocolors_1.default.magenta('[plugin:' + err.plugin + ']')} ${picocolors_1.default.red(err.message)}`);
  113. if (err.loc &&
  114. err.hook === 'transform' &&
  115. err.plugin === 'rollup-plugin-dynamic-import-variables' &&
  116. err.id &&
  117. constants_1.EXTNAME_VUE_RE.test(err.id)) {
  118. try {
  119. const ast = (0, ast_1.parseVue)(fs_1.default.readFileSync(err.id, 'utf8'), []);
  120. const scriptNode = ast.children.find((node) => node.type === 1 /* NodeTypes.ELEMENT */ && node.tag === 'script');
  121. if (scriptNode) {
  122. const scriptLoc = scriptNode.loc;
  123. args.push(picocolors_1.default.yellow(pad((0, utils_2.generateCodeFrame)(scriptLoc.source, err.loc))));
  124. // correct error location
  125. err.loc.line = scriptLoc.start.line + err.loc.line - 1;
  126. }
  127. }
  128. catch (e) { }
  129. }
  130. }
  131. else {
  132. args.push(picocolors_1.default.red(err.message));
  133. }
  134. if (err.id) {
  135. args.push(formatAtFilename(err.id, err.loc?.line, err.loc?.column));
  136. }
  137. if (err.frame) {
  138. args.push(picocolors_1.default.yellow(pad(err.frame)));
  139. }
  140. if (includeStack && err.stack) {
  141. args.push(pad(cleanStack(err.stack)));
  142. }
  143. return args.join('\n');
  144. }
  145. function cleanStack(stack) {
  146. return stack
  147. .split(/\n/g)
  148. .filter((l) => /^\s*at/.test(l))
  149. .join('\n');
  150. }
  151. const splitRE = /\r?\n/;
  152. function pad(source, n = 2) {
  153. const lines = source.split(splitRE);
  154. return lines.map((l) => ` `.repeat(n) + l).join(`\n`);
  155. }