remapping.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // src/build-source-map-tree.ts
  2. import { TraceMap } from "@jridgewell/trace-mapping";
  3. // src/source-map-tree.ts
  4. import { GenMapping, maybeAddSegment, setIgnore, setSourceContent } from "@jridgewell/gen-mapping";
  5. import { traceSegment, decodedMappings } from "@jridgewell/trace-mapping";
  6. var SOURCELESS_MAPPING = /* @__PURE__ */ SegmentObject("", -1, -1, "", null, false);
  7. var EMPTY_SOURCES = [];
  8. function SegmentObject(source, line, column, name, content, ignore) {
  9. return { source, line, column, name, content, ignore };
  10. }
  11. function Source(map, sources, source, content, ignore) {
  12. return {
  13. map,
  14. sources,
  15. source,
  16. content,
  17. ignore
  18. };
  19. }
  20. function MapSource(map, sources) {
  21. return Source(map, sources, "", null, false);
  22. }
  23. function OriginalSource(source, content, ignore) {
  24. return Source(null, EMPTY_SOURCES, source, content, ignore);
  25. }
  26. function traceMappings(tree) {
  27. const gen = new GenMapping({ file: tree.map.file });
  28. const { sources: rootSources, map } = tree;
  29. const rootNames = map.names;
  30. const rootMappings = decodedMappings(map);
  31. for (let i = 0; i < rootMappings.length; i++) {
  32. const segments = rootMappings[i];
  33. for (let j = 0; j < segments.length; j++) {
  34. const segment = segments[j];
  35. const genCol = segment[0];
  36. let traced = SOURCELESS_MAPPING;
  37. if (segment.length !== 1) {
  38. const source2 = rootSources[segment[1]];
  39. traced = originalPositionFor(
  40. source2,
  41. segment[2],
  42. segment[3],
  43. segment.length === 5 ? rootNames[segment[4]] : ""
  44. );
  45. if (traced == null) continue;
  46. }
  47. const { column, line, name, content, source, ignore } = traced;
  48. maybeAddSegment(gen, i, genCol, source, line, column, name);
  49. if (source && content != null) setSourceContent(gen, source, content);
  50. if (ignore) setIgnore(gen, source, true);
  51. }
  52. }
  53. return gen;
  54. }
  55. function originalPositionFor(source, line, column, name) {
  56. if (!source.map) {
  57. return SegmentObject(source.source, line, column, name, source.content, source.ignore);
  58. }
  59. const segment = traceSegment(source.map, line, column);
  60. if (segment == null) return null;
  61. if (segment.length === 1) return SOURCELESS_MAPPING;
  62. return originalPositionFor(
  63. source.sources[segment[1]],
  64. segment[2],
  65. segment[3],
  66. segment.length === 5 ? source.map.names[segment[4]] : name
  67. );
  68. }
  69. // src/build-source-map-tree.ts
  70. function asArray(value) {
  71. if (Array.isArray(value)) return value;
  72. return [value];
  73. }
  74. function buildSourceMapTree(input, loader) {
  75. const maps = asArray(input).map((m) => new TraceMap(m, ""));
  76. const map = maps.pop();
  77. for (let i = 0; i < maps.length; i++) {
  78. if (maps[i].sources.length > 1) {
  79. throw new Error(
  80. `Transformation map ${i} must have exactly one source file.
  81. Did you specify these with the most recent transformation maps first?`
  82. );
  83. }
  84. }
  85. let tree = build(map, loader, "", 0);
  86. for (let i = maps.length - 1; i >= 0; i--) {
  87. tree = MapSource(maps[i], [tree]);
  88. }
  89. return tree;
  90. }
  91. function build(map, loader, importer, importerDepth) {
  92. const { resolvedSources, sourcesContent, ignoreList } = map;
  93. const depth = importerDepth + 1;
  94. const children = resolvedSources.map((sourceFile, i) => {
  95. const ctx = {
  96. importer,
  97. depth,
  98. source: sourceFile || "",
  99. content: void 0,
  100. ignore: void 0
  101. };
  102. const sourceMap = loader(ctx.source, ctx);
  103. const { source, content, ignore } = ctx;
  104. if (sourceMap) return build(new TraceMap(sourceMap, source), loader, source, depth);
  105. const sourceContent = content !== void 0 ? content : sourcesContent ? sourcesContent[i] : null;
  106. const ignored = ignore !== void 0 ? ignore : ignoreList ? ignoreList.includes(i) : false;
  107. return OriginalSource(source, sourceContent, ignored);
  108. });
  109. return MapSource(map, children);
  110. }
  111. // src/source-map.ts
  112. import { toDecodedMap, toEncodedMap } from "@jridgewell/gen-mapping";
  113. var SourceMap = class {
  114. constructor(map, options) {
  115. const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
  116. this.version = out.version;
  117. this.file = out.file;
  118. this.mappings = out.mappings;
  119. this.names = out.names;
  120. this.ignoreList = out.ignoreList;
  121. this.sourceRoot = out.sourceRoot;
  122. this.sources = out.sources;
  123. if (!options.excludeContent) {
  124. this.sourcesContent = out.sourcesContent;
  125. }
  126. }
  127. toString() {
  128. return JSON.stringify(this);
  129. }
  130. };
  131. // src/remapping.ts
  132. function remapping(input, loader, options) {
  133. const opts = typeof options === "object" ? options : { excludeContent: !!options, decodedMappings: false };
  134. const tree = buildSourceMapTree(input, loader);
  135. return new SourceMap(traceMappings(tree), opts);
  136. }
  137. export {
  138. remapping as default
  139. };
  140. //# sourceMappingURL=remapping.mjs.map