source-map.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _genMapping = require("@jridgewell/gen-mapping");
  7. var _traceMapping = require("@jridgewell/trace-mapping");
  8. class SourceMap {
  9. constructor(opts, code) {
  10. var _opts$sourceFileName;
  11. this._map = void 0;
  12. this._rawMappings = void 0;
  13. this._sourceFileName = void 0;
  14. this._lastGenLine = 0;
  15. this._lastSourceLine = 0;
  16. this._lastSourceColumn = 0;
  17. this._inputMap = void 0;
  18. const map = this._map = new _genMapping.GenMapping({
  19. sourceRoot: opts.sourceRoot
  20. });
  21. this._sourceFileName = (_opts$sourceFileName = opts.sourceFileName) == null ? void 0 : _opts$sourceFileName.replace(/\\/g, "/");
  22. this._rawMappings = undefined;
  23. if (opts.inputSourceMap) {
  24. this._inputMap = new _traceMapping.TraceMap(opts.inputSourceMap);
  25. const resolvedSources = this._inputMap.resolvedSources;
  26. if (resolvedSources.length) {
  27. for (let i = 0; i < resolvedSources.length; i++) {
  28. var _this$_inputMap$sourc;
  29. (0, _genMapping.setSourceContent)(map, resolvedSources[i], (_this$_inputMap$sourc = this._inputMap.sourcesContent) == null ? void 0 : _this$_inputMap$sourc[i]);
  30. }
  31. }
  32. }
  33. if (typeof code === "string" && !opts.inputSourceMap) {
  34. (0, _genMapping.setSourceContent)(map, this._sourceFileName, code);
  35. } else if (typeof code === "object") {
  36. for (const sourceFileName of Object.keys(code)) {
  37. (0, _genMapping.setSourceContent)(map, sourceFileName.replace(/\\/g, "/"), code[sourceFileName]);
  38. }
  39. }
  40. }
  41. get() {
  42. return (0, _genMapping.toEncodedMap)(this._map);
  43. }
  44. getDecoded() {
  45. return (0, _genMapping.toDecodedMap)(this._map);
  46. }
  47. getRawMappings() {
  48. return this._rawMappings || (this._rawMappings = (0, _genMapping.allMappings)(this._map));
  49. }
  50. mark(generated, line, column, identifierName, identifierNamePos, filename) {
  51. var _originalMapping;
  52. this._rawMappings = undefined;
  53. let originalMapping;
  54. if (line != null) {
  55. if (this._inputMap) {
  56. originalMapping = (0, _traceMapping.originalPositionFor)(this._inputMap, {
  57. line,
  58. column
  59. });
  60. if (!originalMapping.name && identifierNamePos) {
  61. const originalIdentifierMapping = (0, _traceMapping.originalPositionFor)(this._inputMap, identifierNamePos);
  62. if (originalIdentifierMapping.name) {
  63. identifierName = originalIdentifierMapping.name;
  64. }
  65. }
  66. } else {
  67. originalMapping = {
  68. source: (filename == null ? void 0 : filename.replace(/\\/g, "/")) || this._sourceFileName,
  69. line: line,
  70. column: column
  71. };
  72. }
  73. }
  74. (0, _genMapping.maybeAddMapping)(this._map, {
  75. name: identifierName,
  76. generated,
  77. source: (_originalMapping = originalMapping) == null ? void 0 : _originalMapping.source,
  78. original: originalMapping
  79. });
  80. }
  81. }
  82. exports.default = SourceMap;
  83. //# sourceMappingURL=source-map.js.map