trace-mapping.umd.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. (function (global, factory) {
  2. if (typeof exports === 'object' && typeof module !== 'undefined') {
  3. factory(module, require('@jridgewell/resolve-uri'), require('@jridgewell/sourcemap-codec'));
  4. module.exports = def(module);
  5. } else if (typeof define === 'function' && define.amd) {
  6. define(['module', '@jridgewell/resolve-uri', '@jridgewell/sourcemap-codec'], function(mod) {
  7. factory.apply(this, arguments);
  8. mod.exports = def(mod);
  9. });
  10. } else {
  11. const mod = { exports: {} };
  12. factory(mod, global.resolveURI, global.sourcemapCodec);
  13. global = typeof globalThis !== 'undefined' ? globalThis : global || self;
  14. global.traceMapping = def(mod);
  15. }
  16. function def(m) { return 'default' in m.exports ? m.exports.default : m.exports; }
  17. })(this, (function (module, require_resolveURI, require_sourcemapCodec) {
  18. "use strict";
  19. var __create = Object.create;
  20. var __defProp = Object.defineProperty;
  21. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  22. var __getOwnPropNames = Object.getOwnPropertyNames;
  23. var __getProtoOf = Object.getPrototypeOf;
  24. var __hasOwnProp = Object.prototype.hasOwnProperty;
  25. var __commonJS = (cb, mod) => function __require() {
  26. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  27. };
  28. var __export = (target, all) => {
  29. for (var name in all)
  30. __defProp(target, name, { get: all[name], enumerable: true });
  31. };
  32. var __copyProps = (to, from, except, desc) => {
  33. if (from && typeof from === "object" || typeof from === "function") {
  34. for (let key of __getOwnPropNames(from))
  35. if (!__hasOwnProp.call(to, key) && key !== except)
  36. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  37. }
  38. return to;
  39. };
  40. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  41. // If the importer is in node compatibility mode or this is not an ESM
  42. // file that has been converted to a CommonJS file using a Babel-
  43. // compatible transform (i.e. "__esModule" has not been set), then set
  44. // "default" to the CommonJS "module.exports" for node compatibility.
  45. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  46. mod
  47. ));
  48. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  49. // umd:@jridgewell/sourcemap-codec
  50. var require_sourcemap_codec = __commonJS({
  51. "umd:@jridgewell/sourcemap-codec"(exports, module2) {
  52. module2.exports = require_sourcemapCodec;
  53. }
  54. });
  55. // umd:@jridgewell/resolve-uri
  56. var require_resolve_uri = __commonJS({
  57. "umd:@jridgewell/resolve-uri"(exports, module2) {
  58. module2.exports = require_resolveURI;
  59. }
  60. });
  61. // src/trace-mapping.ts
  62. var trace_mapping_exports = {};
  63. __export(trace_mapping_exports, {
  64. AnyMap: () => FlattenMap,
  65. FlattenMap: () => FlattenMap,
  66. GREATEST_LOWER_BOUND: () => GREATEST_LOWER_BOUND,
  67. LEAST_UPPER_BOUND: () => LEAST_UPPER_BOUND,
  68. TraceMap: () => TraceMap,
  69. allGeneratedPositionsFor: () => allGeneratedPositionsFor,
  70. decodedMap: () => decodedMap,
  71. decodedMappings: () => decodedMappings,
  72. eachMapping: () => eachMapping,
  73. encodedMap: () => encodedMap,
  74. encodedMappings: () => encodedMappings,
  75. generatedPositionFor: () => generatedPositionFor,
  76. isIgnored: () => isIgnored,
  77. originalPositionFor: () => originalPositionFor,
  78. presortedDecodedMap: () => presortedDecodedMap,
  79. sourceContentFor: () => sourceContentFor,
  80. traceSegment: () => traceSegment
  81. });
  82. module.exports = __toCommonJS(trace_mapping_exports);
  83. var import_sourcemap_codec = __toESM(require_sourcemap_codec());
  84. // src/resolve.ts
  85. var import_resolve_uri = __toESM(require_resolve_uri());
  86. // src/strip-filename.ts
  87. function stripFilename(path) {
  88. if (!path) return "";
  89. const index = path.lastIndexOf("/");
  90. return path.slice(0, index + 1);
  91. }
  92. // src/resolve.ts
  93. function resolver(mapUrl, sourceRoot) {
  94. const from = stripFilename(mapUrl);
  95. const prefix = sourceRoot ? sourceRoot + "/" : "";
  96. return (source) => (0, import_resolve_uri.default)(prefix + (source || ""), from);
  97. }
  98. // src/sourcemap-segment.ts
  99. var COLUMN = 0;
  100. var SOURCES_INDEX = 1;
  101. var SOURCE_LINE = 2;
  102. var SOURCE_COLUMN = 3;
  103. var NAMES_INDEX = 4;
  104. var REV_GENERATED_LINE = 1;
  105. var REV_GENERATED_COLUMN = 2;
  106. // src/sort.ts
  107. function maybeSort(mappings, owned) {
  108. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  109. if (unsortedIndex === mappings.length) return mappings;
  110. if (!owned) mappings = mappings.slice();
  111. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  112. mappings[i] = sortSegments(mappings[i], owned);
  113. }
  114. return mappings;
  115. }
  116. function nextUnsortedSegmentLine(mappings, start) {
  117. for (let i = start; i < mappings.length; i++) {
  118. if (!isSorted(mappings[i])) return i;
  119. }
  120. return mappings.length;
  121. }
  122. function isSorted(line) {
  123. for (let j = 1; j < line.length; j++) {
  124. if (line[j][COLUMN] < line[j - 1][COLUMN]) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. function sortSegments(line, owned) {
  131. if (!owned) line = line.slice();
  132. return line.sort(sortComparator);
  133. }
  134. function sortComparator(a, b) {
  135. return a[COLUMN] - b[COLUMN];
  136. }
  137. // src/by-source.ts
  138. function buildBySources(decoded, memos) {
  139. const sources = memos.map(() => []);
  140. for (let i = 0; i < decoded.length; i++) {
  141. const line = decoded[i];
  142. for (let j = 0; j < line.length; j++) {
  143. const seg = line[j];
  144. if (seg.length === 1) continue;
  145. const sourceIndex2 = seg[SOURCES_INDEX];
  146. const sourceLine = seg[SOURCE_LINE];
  147. const sourceColumn = seg[SOURCE_COLUMN];
  148. const source = sources[sourceIndex2];
  149. const segs = source[sourceLine] || (source[sourceLine] = []);
  150. segs.push([sourceColumn, i, seg[COLUMN]]);
  151. }
  152. }
  153. for (let i = 0; i < sources.length; i++) {
  154. const source = sources[i];
  155. for (let j = 0; j < source.length; j++) {
  156. const line = source[j];
  157. if (line) line.sort(sortComparator);
  158. }
  159. }
  160. return sources;
  161. }
  162. // src/binary-search.ts
  163. var found = false;
  164. function binarySearch(haystack, needle, low, high) {
  165. while (low <= high) {
  166. const mid = low + (high - low >> 1);
  167. const cmp = haystack[mid][COLUMN] - needle;
  168. if (cmp === 0) {
  169. found = true;
  170. return mid;
  171. }
  172. if (cmp < 0) {
  173. low = mid + 1;
  174. } else {
  175. high = mid - 1;
  176. }
  177. }
  178. found = false;
  179. return low - 1;
  180. }
  181. function upperBound(haystack, needle, index) {
  182. for (let i = index + 1; i < haystack.length; index = i++) {
  183. if (haystack[i][COLUMN] !== needle) break;
  184. }
  185. return index;
  186. }
  187. function lowerBound(haystack, needle, index) {
  188. for (let i = index - 1; i >= 0; index = i--) {
  189. if (haystack[i][COLUMN] !== needle) break;
  190. }
  191. return index;
  192. }
  193. function memoizedState() {
  194. return {
  195. lastKey: -1,
  196. lastNeedle: -1,
  197. lastIndex: -1
  198. };
  199. }
  200. function memoizedBinarySearch(haystack, needle, state, key) {
  201. const { lastKey, lastNeedle, lastIndex } = state;
  202. let low = 0;
  203. let high = haystack.length - 1;
  204. if (key === lastKey) {
  205. if (needle === lastNeedle) {
  206. found = lastIndex !== -1 && haystack[lastIndex][COLUMN] === needle;
  207. return lastIndex;
  208. }
  209. if (needle >= lastNeedle) {
  210. low = lastIndex === -1 ? 0 : lastIndex;
  211. } else {
  212. high = lastIndex;
  213. }
  214. }
  215. state.lastKey = key;
  216. state.lastNeedle = needle;
  217. return state.lastIndex = binarySearch(haystack, needle, low, high);
  218. }
  219. // src/types.ts
  220. function parse(map) {
  221. return typeof map === "string" ? JSON.parse(map) : map;
  222. }
  223. // src/flatten-map.ts
  224. var FlattenMap = function(map, mapUrl) {
  225. const parsed = parse(map);
  226. if (!("sections" in parsed)) {
  227. return new TraceMap(parsed, mapUrl);
  228. }
  229. const mappings = [];
  230. const sources = [];
  231. const sourcesContent = [];
  232. const names = [];
  233. const ignoreList = [];
  234. recurse(
  235. parsed,
  236. mapUrl,
  237. mappings,
  238. sources,
  239. sourcesContent,
  240. names,
  241. ignoreList,
  242. 0,
  243. 0,
  244. Infinity,
  245. Infinity
  246. );
  247. const joined = {
  248. version: 3,
  249. file: parsed.file,
  250. names,
  251. sources,
  252. sourcesContent,
  253. mappings,
  254. ignoreList
  255. };
  256. return presortedDecodedMap(joined);
  257. };
  258. function recurse(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  259. const { sections } = input;
  260. for (let i = 0; i < sections.length; i++) {
  261. const { map, offset } = sections[i];
  262. let sl = stopLine;
  263. let sc = stopColumn;
  264. if (i + 1 < sections.length) {
  265. const nextOffset = sections[i + 1].offset;
  266. sl = Math.min(stopLine, lineOffset + nextOffset.line);
  267. if (sl === stopLine) {
  268. sc = Math.min(stopColumn, columnOffset + nextOffset.column);
  269. } else if (sl < stopLine) {
  270. sc = columnOffset + nextOffset.column;
  271. }
  272. }
  273. addSection(
  274. map,
  275. mapUrl,
  276. mappings,
  277. sources,
  278. sourcesContent,
  279. names,
  280. ignoreList,
  281. lineOffset + offset.line,
  282. columnOffset + offset.column,
  283. sl,
  284. sc
  285. );
  286. }
  287. }
  288. function addSection(input, mapUrl, mappings, sources, sourcesContent, names, ignoreList, lineOffset, columnOffset, stopLine, stopColumn) {
  289. const parsed = parse(input);
  290. if ("sections" in parsed) return recurse(...arguments);
  291. const map = new TraceMap(parsed, mapUrl);
  292. const sourcesOffset = sources.length;
  293. const namesOffset = names.length;
  294. const decoded = decodedMappings(map);
  295. const { resolvedSources, sourcesContent: contents, ignoreList: ignores } = map;
  296. append(sources, resolvedSources);
  297. append(names, map.names);
  298. if (contents) append(sourcesContent, contents);
  299. else for (let i = 0; i < resolvedSources.length; i++) sourcesContent.push(null);
  300. if (ignores) for (let i = 0; i < ignores.length; i++) ignoreList.push(ignores[i] + sourcesOffset);
  301. for (let i = 0; i < decoded.length; i++) {
  302. const lineI = lineOffset + i;
  303. if (lineI > stopLine) return;
  304. const out = getLine(mappings, lineI);
  305. const cOffset = i === 0 ? columnOffset : 0;
  306. const line = decoded[i];
  307. for (let j = 0; j < line.length; j++) {
  308. const seg = line[j];
  309. const column = cOffset + seg[COLUMN];
  310. if (lineI === stopLine && column >= stopColumn) return;
  311. if (seg.length === 1) {
  312. out.push([column]);
  313. continue;
  314. }
  315. const sourcesIndex = sourcesOffset + seg[SOURCES_INDEX];
  316. const sourceLine = seg[SOURCE_LINE];
  317. const sourceColumn = seg[SOURCE_COLUMN];
  318. out.push(
  319. seg.length === 4 ? [column, sourcesIndex, sourceLine, sourceColumn] : [column, sourcesIndex, sourceLine, sourceColumn, namesOffset + seg[NAMES_INDEX]]
  320. );
  321. }
  322. }
  323. }
  324. function append(arr, other) {
  325. for (let i = 0; i < other.length; i++) arr.push(other[i]);
  326. }
  327. function getLine(arr, index) {
  328. for (let i = arr.length; i <= index; i++) arr[i] = [];
  329. return arr[index];
  330. }
  331. // src/trace-mapping.ts
  332. var LINE_GTR_ZERO = "`line` must be greater than 0 (lines start at line 1)";
  333. var COL_GTR_EQ_ZERO = "`column` must be greater than or equal to 0 (columns start at column 0)";
  334. var LEAST_UPPER_BOUND = -1;
  335. var GREATEST_LOWER_BOUND = 1;
  336. var TraceMap = class {
  337. constructor(map, mapUrl) {
  338. const isString = typeof map === "string";
  339. if (!isString && map._decodedMemo) return map;
  340. const parsed = parse(map);
  341. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  342. this.version = version;
  343. this.file = file;
  344. this.names = names || [];
  345. this.sourceRoot = sourceRoot;
  346. this.sources = sources;
  347. this.sourcesContent = sourcesContent;
  348. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || void 0;
  349. const resolve = resolver(mapUrl, sourceRoot);
  350. this.resolvedSources = sources.map(resolve);
  351. const { mappings } = parsed;
  352. if (typeof mappings === "string") {
  353. this._encoded = mappings;
  354. this._decoded = void 0;
  355. } else if (Array.isArray(mappings)) {
  356. this._encoded = void 0;
  357. this._decoded = maybeSort(mappings, isString);
  358. } else if (parsed.sections) {
  359. throw new Error(`TraceMap passed sectioned source map, please use FlattenMap export instead`);
  360. } else {
  361. throw new Error(`invalid source map: ${JSON.stringify(parsed)}`);
  362. }
  363. this._decodedMemo = memoizedState();
  364. this._bySources = void 0;
  365. this._bySourceMemos = void 0;
  366. }
  367. };
  368. function cast(map) {
  369. return map;
  370. }
  371. function encodedMappings(map) {
  372. var _a, _b;
  373. return (_b = (_a = cast(map))._encoded) != null ? _b : _a._encoded = (0, import_sourcemap_codec.encode)(cast(map)._decoded);
  374. }
  375. function decodedMappings(map) {
  376. var _a;
  377. return (_a = cast(map))._decoded || (_a._decoded = (0, import_sourcemap_codec.decode)(cast(map)._encoded));
  378. }
  379. function traceSegment(map, line, column) {
  380. const decoded = decodedMappings(map);
  381. if (line >= decoded.length) return null;
  382. const segments = decoded[line];
  383. const index = traceSegmentInternal(
  384. segments,
  385. cast(map)._decodedMemo,
  386. line,
  387. column,
  388. GREATEST_LOWER_BOUND
  389. );
  390. return index === -1 ? null : segments[index];
  391. }
  392. function originalPositionFor(map, needle) {
  393. let { line, column, bias } = needle;
  394. line--;
  395. if (line < 0) throw new Error(LINE_GTR_ZERO);
  396. if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
  397. const decoded = decodedMappings(map);
  398. if (line >= decoded.length) return OMapping(null, null, null, null);
  399. const segments = decoded[line];
  400. const index = traceSegmentInternal(
  401. segments,
  402. cast(map)._decodedMemo,
  403. line,
  404. column,
  405. bias || GREATEST_LOWER_BOUND
  406. );
  407. if (index === -1) return OMapping(null, null, null, null);
  408. const segment = segments[index];
  409. if (segment.length === 1) return OMapping(null, null, null, null);
  410. const { names, resolvedSources } = map;
  411. return OMapping(
  412. resolvedSources[segment[SOURCES_INDEX]],
  413. segment[SOURCE_LINE] + 1,
  414. segment[SOURCE_COLUMN],
  415. segment.length === 5 ? names[segment[NAMES_INDEX]] : null
  416. );
  417. }
  418. function generatedPositionFor(map, needle) {
  419. const { source, line, column, bias } = needle;
  420. return generatedPosition(map, source, line, column, bias || GREATEST_LOWER_BOUND, false);
  421. }
  422. function allGeneratedPositionsFor(map, needle) {
  423. const { source, line, column, bias } = needle;
  424. return generatedPosition(map, source, line, column, bias || LEAST_UPPER_BOUND, true);
  425. }
  426. function eachMapping(map, cb) {
  427. const decoded = decodedMappings(map);
  428. const { names, resolvedSources } = map;
  429. for (let i = 0; i < decoded.length; i++) {
  430. const line = decoded[i];
  431. for (let j = 0; j < line.length; j++) {
  432. const seg = line[j];
  433. const generatedLine = i + 1;
  434. const generatedColumn = seg[0];
  435. let source = null;
  436. let originalLine = null;
  437. let originalColumn = null;
  438. let name = null;
  439. if (seg.length !== 1) {
  440. source = resolvedSources[seg[1]];
  441. originalLine = seg[2] + 1;
  442. originalColumn = seg[3];
  443. }
  444. if (seg.length === 5) name = names[seg[4]];
  445. cb({
  446. generatedLine,
  447. generatedColumn,
  448. source,
  449. originalLine,
  450. originalColumn,
  451. name
  452. });
  453. }
  454. }
  455. }
  456. function sourceIndex(map, source) {
  457. const { sources, resolvedSources } = map;
  458. let index = sources.indexOf(source);
  459. if (index === -1) index = resolvedSources.indexOf(source);
  460. return index;
  461. }
  462. function sourceContentFor(map, source) {
  463. const { sourcesContent } = map;
  464. if (sourcesContent == null) return null;
  465. const index = sourceIndex(map, source);
  466. return index === -1 ? null : sourcesContent[index];
  467. }
  468. function isIgnored(map, source) {
  469. const { ignoreList } = map;
  470. if (ignoreList == null) return false;
  471. const index = sourceIndex(map, source);
  472. return index === -1 ? false : ignoreList.includes(index);
  473. }
  474. function presortedDecodedMap(map, mapUrl) {
  475. const tracer = new TraceMap(clone(map, []), mapUrl);
  476. cast(tracer)._decoded = map.mappings;
  477. return tracer;
  478. }
  479. function decodedMap(map) {
  480. return clone(map, decodedMappings(map));
  481. }
  482. function encodedMap(map) {
  483. return clone(map, encodedMappings(map));
  484. }
  485. function clone(map, mappings) {
  486. return {
  487. version: map.version,
  488. file: map.file,
  489. names: map.names,
  490. sourceRoot: map.sourceRoot,
  491. sources: map.sources,
  492. sourcesContent: map.sourcesContent,
  493. mappings,
  494. ignoreList: map.ignoreList || map.x_google_ignoreList
  495. };
  496. }
  497. function OMapping(source, line, column, name) {
  498. return { source, line, column, name };
  499. }
  500. function GMapping(line, column) {
  501. return { line, column };
  502. }
  503. function traceSegmentInternal(segments, memo, line, column, bias) {
  504. let index = memoizedBinarySearch(segments, column, memo, line);
  505. if (found) {
  506. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  507. } else if (bias === LEAST_UPPER_BOUND) index++;
  508. if (index === -1 || index === segments.length) return -1;
  509. return index;
  510. }
  511. function sliceGeneratedPositions(segments, memo, line, column, bias) {
  512. let min = traceSegmentInternal(segments, memo, line, column, GREATEST_LOWER_BOUND);
  513. if (!found && bias === LEAST_UPPER_BOUND) min++;
  514. if (min === -1 || min === segments.length) return [];
  515. const matchedColumn = found ? column : segments[min][COLUMN];
  516. if (!found) min = lowerBound(segments, matchedColumn, min);
  517. const max = upperBound(segments, matchedColumn, min);
  518. const result = [];
  519. for (; min <= max; min++) {
  520. const segment = segments[min];
  521. result.push(GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]));
  522. }
  523. return result;
  524. }
  525. function generatedPosition(map, source, line, column, bias, all) {
  526. var _a, _b;
  527. line--;
  528. if (line < 0) throw new Error(LINE_GTR_ZERO);
  529. if (column < 0) throw new Error(COL_GTR_EQ_ZERO);
  530. const { sources, resolvedSources } = map;
  531. let sourceIndex2 = sources.indexOf(source);
  532. if (sourceIndex2 === -1) sourceIndex2 = resolvedSources.indexOf(source);
  533. if (sourceIndex2 === -1) return all ? [] : GMapping(null, null);
  534. const bySourceMemos = (_a = cast(map))._bySourceMemos || (_a._bySourceMemos = sources.map(memoizedState));
  535. const generated = (_b = cast(map))._bySources || (_b._bySources = buildBySources(decodedMappings(map), bySourceMemos));
  536. const segments = generated[sourceIndex2][line];
  537. if (segments == null) return all ? [] : GMapping(null, null);
  538. const memo = bySourceMemos[sourceIndex2];
  539. if (all) return sliceGeneratedPositions(segments, memo, line, column, bias);
  540. const index = traceSegmentInternal(segments, memo, line, column, bias);
  541. if (index === -1) return GMapping(null, null);
  542. const segment = segments[index];
  543. return GMapping(segment[REV_GENERATED_LINE] + 1, segment[REV_GENERATED_COLUMN]);
  544. }
  545. }));
  546. //# sourceMappingURL=trace-mapping.umd.js.map