logs-D6uA606S.mjs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //#region src/utils/code-frame.ts
  2. function spaces(index) {
  3. let result = "";
  4. while (index--) result += " ";
  5. return result;
  6. }
  7. function tabsToSpaces(value) {
  8. return value.replace(/^\t+/, (match) => match.split(" ").join(" "));
  9. }
  10. const LINE_TRUNCATE_LENGTH = 120;
  11. const MIN_CHARACTERS_SHOWN_AFTER_LOCATION = 10;
  12. const ELLIPSIS = "...";
  13. function getCodeFrame(source, line, column) {
  14. let lines = source.split("\n");
  15. if (line > lines.length) return "";
  16. const maxLineLength = Math.max(tabsToSpaces(lines[line - 1].slice(0, column)).length + MIN_CHARACTERS_SHOWN_AFTER_LOCATION + 3, LINE_TRUNCATE_LENGTH);
  17. const frameStart = Math.max(0, line - 3);
  18. let frameEnd = Math.min(line + 2, lines.length);
  19. lines = lines.slice(frameStart, frameEnd);
  20. while (!/\S/.test(lines[lines.length - 1])) {
  21. lines.pop();
  22. frameEnd -= 1;
  23. }
  24. const digits = String(frameEnd).length;
  25. return lines.map((sourceLine, index) => {
  26. const isErrorLine = frameStart + index + 1 === line;
  27. let lineNumber = String(index + frameStart + 1);
  28. while (lineNumber.length < digits) lineNumber = ` ${lineNumber}`;
  29. let displayedLine = tabsToSpaces(sourceLine);
  30. if (displayedLine.length > maxLineLength) displayedLine = `${displayedLine.slice(0, maxLineLength - 3)}${ELLIPSIS}`;
  31. if (isErrorLine) {
  32. const indicator = spaces(digits + 2 + tabsToSpaces(sourceLine.slice(0, column)).length) + "^";
  33. return `${lineNumber}: ${displayedLine}\n${indicator}`;
  34. }
  35. return `${lineNumber}: ${displayedLine}`;
  36. }).join("\n");
  37. }
  38. //#endregion
  39. //#region src/log/locate-character/index.js
  40. /** @typedef {import('./types').Location} Location */
  41. /**
  42. * @param {import('./types').Range} range
  43. * @param {number} index
  44. */
  45. function rangeContains(range, index) {
  46. return range.start <= index && index < range.end;
  47. }
  48. /**
  49. * @param {string} source
  50. * @param {import('./types').Options} [options]
  51. */
  52. function getLocator(source, options = {}) {
  53. const { offsetLine = 0, offsetColumn = 0 } = options;
  54. let start = 0;
  55. const ranges = source.split("\n").map((line, i) => {
  56. const end = start + line.length + 1;
  57. /** @type {import('./types').Range} */
  58. const range = {
  59. start,
  60. end,
  61. line: i
  62. };
  63. start = end;
  64. return range;
  65. });
  66. let i = 0;
  67. /**
  68. * @param {string | number} search
  69. * @param {number} [index]
  70. * @returns {Location | undefined}
  71. */
  72. function locator(search, index) {
  73. if (typeof search === "string") search = source.indexOf(search, index ?? 0);
  74. if (search === -1) return void 0;
  75. let range = ranges[i];
  76. const d = search >= range.end ? 1 : -1;
  77. while (range) {
  78. if (rangeContains(range, search)) return {
  79. line: offsetLine + range.line,
  80. column: offsetColumn + search - range.start,
  81. character: search
  82. };
  83. i += d;
  84. range = ranges[i];
  85. }
  86. }
  87. return locator;
  88. }
  89. /**
  90. * @param {string} source
  91. * @param {string | number} search
  92. * @param {import('./types').Options} [options]
  93. * @returns {Location | undefined}
  94. */
  95. function locate(source, search, options) {
  96. return getLocator(source, options)(search, options && options.startIndex);
  97. }
  98. //#endregion
  99. //#region src/log/logs.ts
  100. const INVALID_LOG_POSITION = "INVALID_LOG_POSITION";
  101. const PLUGIN_ERROR = "PLUGIN_ERROR";
  102. const INPUT_HOOK_IN_OUTPUT_PLUGIN = "INPUT_HOOK_IN_OUTPUT_PLUGIN";
  103. const CYCLE_LOADING = "CYCLE_LOADING";
  104. const MULTIPLE_WATCHER_OPTION = "MULTIPLE_WATCHER_OPTION";
  105. const MISSING_CODE_SPLITTING_GROUP_DEBUG_NAME = "MISSING_CODE_SPLITTING_GROUP_DEBUG_NAME";
  106. const PARSE_ERROR = "PARSE_ERROR";
  107. const VALIDATION_ERROR = "VALIDATION_ERROR";
  108. function logParseError(message, id, pos) {
  109. return {
  110. code: PARSE_ERROR,
  111. id,
  112. message,
  113. pos
  114. };
  115. }
  116. function logFailedValidation(message) {
  117. return {
  118. code: VALIDATION_ERROR,
  119. message
  120. };
  121. }
  122. function logInvalidLogPosition(pluginName) {
  123. return {
  124. code: INVALID_LOG_POSITION,
  125. message: `Plugin "${pluginName}" tried to add a file position to a log or warning. This is only supported in the "transform" hook at the moment and will be ignored.`
  126. };
  127. }
  128. function logInputHookInOutputPlugin(pluginName, hookName) {
  129. return {
  130. code: INPUT_HOOK_IN_OUTPUT_PLUGIN,
  131. message: `The "${hookName}" hook used by the output plugin ${pluginName} is a build time hook and will not be run for that plugin. Either this plugin cannot be used as an output plugin, or it should have an option to configure it as an output plugin.`
  132. };
  133. }
  134. function logCycleLoading(pluginName, moduleId) {
  135. return {
  136. code: CYCLE_LOADING,
  137. message: `Found the module "${moduleId}" cycle loading at ${pluginName} plugin, it maybe blocking fetching modules.`
  138. };
  139. }
  140. function logMultipleWatcherOption() {
  141. return {
  142. code: MULTIPLE_WATCHER_OPTION,
  143. message: `Found multiple watcher options at watch options, using first one to start watcher.`
  144. };
  145. }
  146. function logMissingCodeSplittingGroupDebugName(groupPath) {
  147. return {
  148. code: MISSING_CODE_SPLITTING_GROUP_DEBUG_NAME,
  149. message: `\`${groupPath}.name\` is a function. Set \`${groupPath}.debugName\` so the bundler timing report can identify this group.`
  150. };
  151. }
  152. function logPluginError(error, plugin, { hook, id } = {}) {
  153. try {
  154. const code = error.code;
  155. if (!error.pluginCode && code != null && (typeof code !== "string" || !code.startsWith("PLUGIN_"))) error.pluginCode = code;
  156. error.code = PLUGIN_ERROR;
  157. error.plugin = plugin;
  158. if (hook) error.hook = hook;
  159. if (id) error.id = id;
  160. } catch (_) {} finally {
  161. return error;
  162. }
  163. }
  164. function error(base) {
  165. if (!(base instanceof Error)) {
  166. base = Object.assign(new Error(base.message), base);
  167. Object.defineProperty(base, "name", {
  168. value: "RolldownError",
  169. writable: true
  170. });
  171. }
  172. throw base;
  173. }
  174. function augmentCodeLocation(properties, pos, source, id) {
  175. if (typeof pos === "object") {
  176. const { line, column } = pos;
  177. properties.loc = {
  178. column,
  179. file: id,
  180. line
  181. };
  182. } else {
  183. properties.pos = pos;
  184. const location = locate(source, pos, { offsetLine: 1 });
  185. if (!location) return;
  186. const { line, column } = location;
  187. properties.loc = {
  188. column,
  189. file: id,
  190. line
  191. };
  192. }
  193. if (properties.frame === void 0) {
  194. const { line, column } = properties.loc;
  195. properties.frame = getCodeFrame(source, line, column);
  196. }
  197. }
  198. //#endregion
  199. export { logInputHookInOutputPlugin as a, logMultipleWatcherOption as c, locate as d, getCodeFrame as f, logFailedValidation as i, logParseError as l, error as n, logInvalidLogPosition as o, logCycleLoading as r, logMissingCodeSplittingGroupDebugName as s, augmentCodeLocation as t, logPluginError as u };