gen-mapping.umd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. (function (global, factory, m) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(module, require('@jridgewell/sourcemap-codec'), require('@jridgewell/trace-mapping')) :
  3. typeof define === 'function' && define.amd ? define(['module', '@jridgewell/sourcemap-codec', '@jridgewell/trace-mapping'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(m = { exports: {} }, global.sourcemapCodec, global.traceMapping), global.genMapping = 'default' in m.exports ? m.exports.default : m.exports);
  5. })(this, (function (module, require_sourcemapCodec, require_traceMapping) {
  6. "use strict";
  7. var __create = Object.create;
  8. var __defProp = Object.defineProperty;
  9. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  10. var __getOwnPropNames = Object.getOwnPropertyNames;
  11. var __getProtoOf = Object.getPrototypeOf;
  12. var __hasOwnProp = Object.prototype.hasOwnProperty;
  13. var __commonJS = (cb, mod) => function __require() {
  14. return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
  15. };
  16. var __export = (target, all) => {
  17. for (var name in all)
  18. __defProp(target, name, { get: all[name], enumerable: true });
  19. };
  20. var __copyProps = (to, from, except, desc) => {
  21. if (from && typeof from === "object" || typeof from === "function") {
  22. for (let key of __getOwnPropNames(from))
  23. if (!__hasOwnProp.call(to, key) && key !== except)
  24. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  25. }
  26. return to;
  27. };
  28. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  29. // If the importer is in node compatibility mode or this is not an ESM
  30. // file that has been converted to a CommonJS file using a Babel-
  31. // compatible transform (i.e. "__esModule" has not been set), then set
  32. // "default" to the CommonJS "module.exports" for node compatibility.
  33. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  34. mod
  35. ));
  36. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  37. // umd:@jridgewell/sourcemap-codec
  38. var require_sourcemap_codec = __commonJS({
  39. "umd:@jridgewell/sourcemap-codec"(exports, module2) {
  40. module2.exports = require_sourcemapCodec;
  41. }
  42. });
  43. // umd:@jridgewell/trace-mapping
  44. var require_trace_mapping = __commonJS({
  45. "umd:@jridgewell/trace-mapping"(exports, module2) {
  46. module2.exports = require_traceMapping;
  47. }
  48. });
  49. // src/gen-mapping.ts
  50. var gen_mapping_exports = {};
  51. __export(gen_mapping_exports, {
  52. GenMapping: () => GenMapping,
  53. addMapping: () => addMapping,
  54. addSegment: () => addSegment,
  55. allMappings: () => allMappings,
  56. fromMap: () => fromMap,
  57. maybeAddMapping: () => maybeAddMapping,
  58. maybeAddSegment: () => maybeAddSegment,
  59. setIgnore: () => setIgnore,
  60. setSourceContent: () => setSourceContent,
  61. toDecodedMap: () => toDecodedMap,
  62. toEncodedMap: () => toEncodedMap
  63. });
  64. module.exports = __toCommonJS(gen_mapping_exports);
  65. // src/set-array.ts
  66. var SetArray = class {
  67. constructor() {
  68. this._indexes = { __proto__: null };
  69. this.array = [];
  70. }
  71. };
  72. function cast(set) {
  73. return set;
  74. }
  75. function get(setarr, key) {
  76. return cast(setarr)._indexes[key];
  77. }
  78. function put(setarr, key) {
  79. const index = get(setarr, key);
  80. if (index !== void 0) return index;
  81. const { array, _indexes: indexes } = cast(setarr);
  82. const length = array.push(key);
  83. return indexes[key] = length - 1;
  84. }
  85. function remove(setarr, key) {
  86. const index = get(setarr, key);
  87. if (index === void 0) return;
  88. const { array, _indexes: indexes } = cast(setarr);
  89. for (let i = index + 1; i < array.length; i++) {
  90. const k = array[i];
  91. array[i - 1] = k;
  92. indexes[k]--;
  93. }
  94. indexes[key] = void 0;
  95. array.pop();
  96. }
  97. // src/gen-mapping.ts
  98. var import_sourcemap_codec = __toESM(require_sourcemap_codec());
  99. var import_trace_mapping = __toESM(require_trace_mapping());
  100. // src/sourcemap-segment.ts
  101. var COLUMN = 0;
  102. var SOURCES_INDEX = 1;
  103. var SOURCE_LINE = 2;
  104. var SOURCE_COLUMN = 3;
  105. var NAMES_INDEX = 4;
  106. // src/gen-mapping.ts
  107. var NO_NAME = -1;
  108. var GenMapping = class {
  109. constructor({ file, sourceRoot } = {}) {
  110. this._names = new SetArray();
  111. this._sources = new SetArray();
  112. this._sourcesContent = [];
  113. this._mappings = [];
  114. this.file = file;
  115. this.sourceRoot = sourceRoot;
  116. this._ignoreList = new SetArray();
  117. }
  118. };
  119. function cast2(map) {
  120. return map;
  121. }
  122. function addSegment(map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  123. return addSegmentInternal(
  124. false,
  125. map,
  126. genLine,
  127. genColumn,
  128. source,
  129. sourceLine,
  130. sourceColumn,
  131. name,
  132. content
  133. );
  134. }
  135. function addMapping(map, mapping) {
  136. return addMappingInternal(false, map, mapping);
  137. }
  138. var maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  139. return addSegmentInternal(
  140. true,
  141. map,
  142. genLine,
  143. genColumn,
  144. source,
  145. sourceLine,
  146. sourceColumn,
  147. name,
  148. content
  149. );
  150. };
  151. var maybeAddMapping = (map, mapping) => {
  152. return addMappingInternal(true, map, mapping);
  153. };
  154. function setSourceContent(map, source, content) {
  155. const {
  156. _sources: sources,
  157. _sourcesContent: sourcesContent
  158. // _originalScopes: originalScopes,
  159. } = cast2(map);
  160. const index = put(sources, source);
  161. sourcesContent[index] = content;
  162. }
  163. function setIgnore(map, source, ignore = true) {
  164. const {
  165. _sources: sources,
  166. _sourcesContent: sourcesContent,
  167. _ignoreList: ignoreList
  168. // _originalScopes: originalScopes,
  169. } = cast2(map);
  170. const index = put(sources, source);
  171. if (index === sourcesContent.length) sourcesContent[index] = null;
  172. if (ignore) put(ignoreList, index);
  173. else remove(ignoreList, index);
  174. }
  175. function toDecodedMap(map) {
  176. const {
  177. _mappings: mappings,
  178. _sources: sources,
  179. _sourcesContent: sourcesContent,
  180. _names: names,
  181. _ignoreList: ignoreList
  182. // _originalScopes: originalScopes,
  183. // _generatedRanges: generatedRanges,
  184. } = cast2(map);
  185. removeEmptyFinalLines(mappings);
  186. return {
  187. version: 3,
  188. file: map.file || void 0,
  189. names: names.array,
  190. sourceRoot: map.sourceRoot || void 0,
  191. sources: sources.array,
  192. sourcesContent,
  193. mappings,
  194. // originalScopes,
  195. // generatedRanges,
  196. ignoreList: ignoreList.array
  197. };
  198. }
  199. function toEncodedMap(map) {
  200. const decoded = toDecodedMap(map);
  201. return Object.assign({}, decoded, {
  202. // originalScopes: decoded.originalScopes.map((os) => encodeOriginalScopes(os)),
  203. // generatedRanges: encodeGeneratedRanges(decoded.generatedRanges as GeneratedRange[]),
  204. mappings: (0, import_sourcemap_codec.encode)(decoded.mappings)
  205. });
  206. }
  207. function fromMap(input) {
  208. const map = new import_trace_mapping.TraceMap(input);
  209. const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
  210. putAll(cast2(gen)._names, map.names);
  211. putAll(cast2(gen)._sources, map.sources);
  212. cast2(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
  213. cast2(gen)._mappings = (0, import_trace_mapping.decodedMappings)(map);
  214. if (map.ignoreList) putAll(cast2(gen)._ignoreList, map.ignoreList);
  215. return gen;
  216. }
  217. function allMappings(map) {
  218. const out = [];
  219. const { _mappings: mappings, _sources: sources, _names: names } = cast2(map);
  220. for (let i = 0; i < mappings.length; i++) {
  221. const line = mappings[i];
  222. for (let j = 0; j < line.length; j++) {
  223. const seg = line[j];
  224. const generated = { line: i + 1, column: seg[COLUMN] };
  225. let source = void 0;
  226. let original = void 0;
  227. let name = void 0;
  228. if (seg.length !== 1) {
  229. source = sources.array[seg[SOURCES_INDEX]];
  230. original = { line: seg[SOURCE_LINE] + 1, column: seg[SOURCE_COLUMN] };
  231. if (seg.length === 5) name = names.array[seg[NAMES_INDEX]];
  232. }
  233. out.push({ generated, source, original, name });
  234. }
  235. }
  236. return out;
  237. }
  238. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  239. const {
  240. _mappings: mappings,
  241. _sources: sources,
  242. _sourcesContent: sourcesContent,
  243. _names: names
  244. // _originalScopes: originalScopes,
  245. } = cast2(map);
  246. const line = getIndex(mappings, genLine);
  247. const index = getColumnIndex(line, genColumn);
  248. if (!source) {
  249. if (skipable && skipSourceless(line, index)) return;
  250. return insert(line, index, [genColumn]);
  251. }
  252. assert(sourceLine);
  253. assert(sourceColumn);
  254. const sourcesIndex = put(sources, source);
  255. const namesIndex = name ? put(names, name) : NO_NAME;
  256. if (sourcesIndex === sourcesContent.length) sourcesContent[sourcesIndex] = content != null ? content : null;
  257. if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  258. return;
  259. }
  260. return insert(
  261. line,
  262. index,
  263. name ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex] : [genColumn, sourcesIndex, sourceLine, sourceColumn]
  264. );
  265. }
  266. function assert(_val) {
  267. }
  268. function getIndex(arr, index) {
  269. for (let i = arr.length; i <= index; i++) {
  270. arr[i] = [];
  271. }
  272. return arr[index];
  273. }
  274. function getColumnIndex(line, genColumn) {
  275. let index = line.length;
  276. for (let i = index - 1; i >= 0; index = i--) {
  277. const current = line[i];
  278. if (genColumn >= current[COLUMN]) break;
  279. }
  280. return index;
  281. }
  282. function insert(array, index, value) {
  283. for (let i = array.length; i > index; i--) {
  284. array[i] = array[i - 1];
  285. }
  286. array[index] = value;
  287. }
  288. function removeEmptyFinalLines(mappings) {
  289. const { length } = mappings;
  290. let len = length;
  291. for (let i = len - 1; i >= 0; len = i, i--) {
  292. if (mappings[i].length > 0) break;
  293. }
  294. if (len < length) mappings.length = len;
  295. }
  296. function putAll(setarr, array) {
  297. for (let i = 0; i < array.length; i++) put(setarr, array[i]);
  298. }
  299. function skipSourceless(line, index) {
  300. if (index === 0) return true;
  301. const prev = line[index - 1];
  302. return prev.length === 1;
  303. }
  304. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  305. if (index === 0) return false;
  306. const prev = line[index - 1];
  307. if (prev.length === 1) return false;
  308. return sourcesIndex === prev[SOURCES_INDEX] && sourceLine === prev[SOURCE_LINE] && sourceColumn === prev[SOURCE_COLUMN] && namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME);
  309. }
  310. function addMappingInternal(skipable, map, mapping) {
  311. const { generated, source, original, name, content } = mapping;
  312. if (!source) {
  313. return addSegmentInternal(
  314. skipable,
  315. map,
  316. generated.line - 1,
  317. generated.column,
  318. null,
  319. null,
  320. null,
  321. null,
  322. null
  323. );
  324. }
  325. assert(original);
  326. return addSegmentInternal(
  327. skipable,
  328. map,
  329. generated.line - 1,
  330. generated.column,
  331. source,
  332. original.line - 1,
  333. original.column,
  334. name,
  335. content
  336. );
  337. }
  338. }));
  339. //# sourceMappingURL=gen-mapping.umd.js.map