gen-mapping.umd.js 11 KB

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