remapping.umd.js 9.5 KB

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