source-map.mjs 2.6 KB

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