remapping.umd.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@jridgewell/trace-mapping'), require('@jridgewell/gen-mapping')) :
  3. typeof define === 'function' && define.amd ? define(['@jridgewell/trace-mapping', '@jridgewell/gen-mapping'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.remapping = factory(global.traceMapping, global.genMapping));
  5. })(this, (function (traceMapping, genMapping) { 'use strict';
  6. const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null);
  7. const EMPTY_SOURCES = [];
  8. function SegmentObject(source, line, column, name, content) {
  9. return { source, line, column, name, content };
  10. }
  11. function Source(map, sources, source, content) {
  12. return {
  13. map,
  14. sources,
  15. source,
  16. content,
  17. };
  18. }
  19. /**
  20. * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
  21. * (which may themselves be SourceMapTrees).
  22. */
  23. function MapSource(map, sources) {
  24. return Source(map, sources, '', null);
  25. }
  26. /**
  27. * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
  28. * segment tracing ends at the `OriginalSource`.
  29. */
  30. function OriginalSource(source, content) {
  31. return Source(null, EMPTY_SOURCES, source, content);
  32. }
  33. /**
  34. * traceMappings is only called on the root level SourceMapTree, and begins the process of
  35. * resolving each mapping in terms of the original source files.
  36. */
  37. function traceMappings(tree) {
  38. // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
  39. // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
  40. const gen = new genMapping.GenMapping({ file: tree.map.file });
  41. const { sources: rootSources, map } = tree;
  42. const rootNames = map.names;
  43. const rootMappings = traceMapping.decodedMappings(map);
  44. for (let i = 0; i < rootMappings.length; i++) {
  45. const segments = rootMappings[i];
  46. for (let j = 0; j < segments.length; j++) {
  47. const segment = segments[j];
  48. const genCol = segment[0];
  49. let traced = SOURCELESS_MAPPING;
  50. // 1-length segments only move the current generated column, there's no source information
  51. // to gather from it.
  52. if (segment.length !== 1) {
  53. const source = rootSources[segment[1]];
  54. traced = originalPositionFor(source, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
  55. // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
  56. // respective segment into an original source.
  57. if (traced == null)
  58. continue;
  59. }
  60. const { column, line, name, content, source } = traced;
  61. genMapping.maybeAddSegment(gen, i, genCol, source, line, column, name);
  62. if (source && content != null)
  63. genMapping.setSourceContent(gen, source, content);
  64. }
  65. }
  66. return gen;
  67. }
  68. /**
  69. * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
  70. * child SourceMapTrees, until we find the original source map.
  71. */
  72. function originalPositionFor(source, line, column, name) {
  73. if (!source.map) {
  74. return SegmentObject(source.source, line, column, name, source.content);
  75. }
  76. const segment = traceMapping.traceSegment(source.map, line, column);
  77. // If we couldn't find a segment, then this doesn't exist in the sourcemap.
  78. if (segment == null)
  79. return null;
  80. // 1-length segments only move the current generated column, there's no source information
  81. // to gather from it.
  82. if (segment.length === 1)
  83. return SOURCELESS_MAPPING;
  84. return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  85. }
  86. function asArray(value) {
  87. if (Array.isArray(value))
  88. return value;
  89. return [value];
  90. }
  91. /**
  92. * Recursively builds a tree structure out of sourcemap files, with each node
  93. * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
  94. * `OriginalSource`s and `SourceMapTree`s.
  95. *
  96. * Every sourcemap is composed of a collection of source files and mappings
  97. * into locations of those source files. When we generate a `SourceMapTree` for
  98. * the sourcemap, we attempt to load each source file's own sourcemap. If it
  99. * does not have an associated sourcemap, it is considered an original,
  100. * unmodified source file.
  101. */
  102. function buildSourceMapTree(input, loader) {
  103. const maps = asArray(input).map((m) => new traceMapping.TraceMap(m, ''));
  104. const map = maps.pop();
  105. for (let i = 0; i < maps.length; i++) {
  106. if (maps[i].sources.length > 1) {
  107. throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
  108. 'Did you specify these with the most recent transformation maps first?');
  109. }
  110. }
  111. let tree = build(map, loader, '', 0);
  112. for (let i = maps.length - 1; i >= 0; i--) {
  113. tree = MapSource(maps[i], [tree]);
  114. }
  115. return tree;
  116. }
  117. function build(map, loader, importer, importerDepth) {
  118. const { resolvedSources, sourcesContent } = map;
  119. const depth = importerDepth + 1;
  120. const children = resolvedSources.map((sourceFile, i) => {
  121. // The loading context gives the loader more information about why this file is being loaded
  122. // (eg, from which importer). It also allows the loader to override the location of the loaded
  123. // sourcemap/original source, or to override the content in the sourcesContent field if it's
  124. // an unmodified source file.
  125. const ctx = {
  126. importer,
  127. depth,
  128. source: sourceFile || '',
  129. content: undefined,
  130. };
  131. // Use the provided loader callback to retrieve the file's sourcemap.
  132. // TODO: We should eventually support async loading of sourcemap files.
  133. const sourceMap = loader(ctx.source, ctx);
  134. const { source, content } = ctx;
  135. // If there is a sourcemap, then we need to recurse into it to load its source files.
  136. if (sourceMap)
  137. return build(new traceMapping.TraceMap(sourceMap, source), loader, source, depth);
  138. // Else, it's an an unmodified source file.
  139. // The contents of this unmodified source file can be overridden via the loader context,
  140. // allowing it to be explicitly null or a string. If it remains undefined, we fall back to
  141. // the importing sourcemap's `sourcesContent` field.
  142. const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
  143. return OriginalSource(source, sourceContent);
  144. });
  145. return MapSource(map, children);
  146. }
  147. /**
  148. * A SourceMap v3 compatible sourcemap, which only includes fields that were
  149. * provided to it.
  150. */
  151. class SourceMap {
  152. constructor(map, options) {
  153. const out = options.decodedMappings ? genMapping.toDecodedMap(map) : genMapping.toEncodedMap(map);
  154. this.version = out.version; // SourceMap spec says this should be first.
  155. this.file = out.file;
  156. this.mappings = out.mappings;
  157. this.names = out.names;
  158. this.sourceRoot = out.sourceRoot;
  159. this.sources = out.sources;
  160. if (!options.excludeContent) {
  161. this.sourcesContent = out.sourcesContent;
  162. }
  163. }
  164. toString() {
  165. return JSON.stringify(this);
  166. }
  167. }
  168. /**
  169. * Traces through all the mappings in the root sourcemap, through the sources
  170. * (and their sourcemaps), all the way back to the original source location.
  171. *
  172. * `loader` will be called every time we encounter a source file. If it returns
  173. * a sourcemap, we will recurse into that sourcemap to continue the trace. If
  174. * it returns a falsey value, that source file is treated as an original,
  175. * unmodified source file.
  176. *
  177. * Pass `excludeContent` to exclude any self-containing source file content
  178. * from the output sourcemap.
  179. *
  180. * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
  181. * VLQ encoded) mappings.
  182. */
  183. function remapping(input, loader, options) {
  184. const opts = typeof options === 'object' ? options : { excludeContent: !!options, decodedMappings: false };
  185. const tree = buildSourceMapTree(input, loader);
  186. return new SourceMap(traceMappings(tree), opts);
  187. }
  188. return remapping;
  189. }));
  190. //# sourceMappingURL=remapping.umd.js.map