source-map.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { AnyMap, encodedMappings, originalPositionFor, generatedPositionFor, allGeneratedPositionsFor, sourceContentFor, eachMapping } from '@jridgewell/trace-mapping';
  2. import { GenMapping, fromMap, maybeAddMapping, setSourceContent, toEncodedMap, toDecodedMap } from '@jridgewell/gen-mapping';
  3. class SourceMapConsumer {
  4. constructor(map, mapUrl) {
  5. const trace = (this._map = new AnyMap(map, mapUrl));
  6. this.file = trace.file;
  7. this.names = trace.names;
  8. this.sourceRoot = trace.sourceRoot;
  9. this.sources = trace.resolvedSources;
  10. this.sourcesContent = trace.sourcesContent;
  11. this.version = trace.version;
  12. }
  13. static fromSourceMap(map, mapUrl) {
  14. // This is more performant if we receive
  15. // a @jridgewell/source-map SourceMapGenerator
  16. if (map.toDecodedMap) {
  17. return new SourceMapConsumer(map.toDecodedMap(), mapUrl);
  18. }
  19. // This is a fallback for `source-map` and `source-map-js`
  20. return new SourceMapConsumer(map.toJSON(), mapUrl);
  21. }
  22. get mappings() {
  23. return encodedMappings(this._map);
  24. }
  25. originalPositionFor(needle) {
  26. return originalPositionFor(this._map, needle);
  27. }
  28. generatedPositionFor(originalPosition) {
  29. return generatedPositionFor(this._map, originalPosition);
  30. }
  31. allGeneratedPositionsFor(originalPosition) {
  32. return allGeneratedPositionsFor(this._map, originalPosition);
  33. }
  34. hasContentsOfAllSources() {
  35. if (!this.sourcesContent || this.sourcesContent.length !== this.sources.length) {
  36. return false;
  37. }
  38. for (const content of this.sourcesContent) {
  39. if (content == null) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. sourceContentFor(source, nullOnMissing) {
  46. const sourceContent = sourceContentFor(this._map, source);
  47. if (sourceContent != null) {
  48. return sourceContent;
  49. }
  50. if (nullOnMissing) {
  51. return null;
  52. }
  53. throw new Error(`"${source}" is not in the SourceMap.`);
  54. }
  55. eachMapping(callback, context /*, order?: number*/) {
  56. // order is ignored as @jridgewell/trace-map doesn't implement it
  57. eachMapping(this._map, context ? callback.bind(context) : callback);
  58. }
  59. destroy() {
  60. // noop.
  61. }
  62. }
  63. class SourceMapGenerator {
  64. constructor(opts) {
  65. // TODO :: should this be duck-typed ?
  66. this._map = opts instanceof GenMapping ? opts : new GenMapping(opts);
  67. }
  68. static fromSourceMap(consumer) {
  69. return new SourceMapGenerator(fromMap(consumer));
  70. }
  71. addMapping(mapping) {
  72. maybeAddMapping(this._map, mapping);
  73. }
  74. setSourceContent(source, content) {
  75. setSourceContent(this._map, source, content);
  76. }
  77. toJSON() {
  78. return toEncodedMap(this._map);
  79. }
  80. toString() {
  81. return JSON.stringify(this.toJSON());
  82. }
  83. toDecodedMap() {
  84. return toDecodedMap(this._map);
  85. }
  86. }
  87. export { SourceMapConsumer, SourceMapGenerator };
  88. //# sourceMappingURL=source-map.mjs.map