source-map.cjs 3.2 KB

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