source-map.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _sourceMap() {
  7. const data = _interopRequireDefault(require("source-map"));
  8. _sourceMap = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. class SourceMap {
  15. constructor(opts, code) {
  16. this._cachedMap = null;
  17. this._code = code;
  18. this._opts = opts;
  19. this._rawMappings = [];
  20. }
  21. get() {
  22. if (!this._cachedMap) {
  23. const map = this._cachedMap = new (_sourceMap().default.SourceMapGenerator)({
  24. sourceRoot: this._opts.sourceRoot
  25. });
  26. const code = this._code;
  27. if (typeof code === "string") {
  28. map.setSourceContent(this._opts.sourceFileName, code);
  29. } else if (typeof code === "object") {
  30. Object.keys(code).forEach(sourceFileName => {
  31. map.setSourceContent(sourceFileName, code[sourceFileName]);
  32. });
  33. }
  34. this._rawMappings.forEach(map.addMapping, map);
  35. }
  36. return this._cachedMap.toJSON();
  37. }
  38. getRawMappings() {
  39. return this._rawMappings.slice();
  40. }
  41. mark(generatedLine, generatedColumn, line, column, identifierName, filename, force) {
  42. if (this._lastGenLine !== generatedLine && line === null) return;
  43. if (!force && this._lastGenLine === generatedLine && this._lastSourceLine === line && this._lastSourceColumn === column) {
  44. return;
  45. }
  46. this._cachedMap = null;
  47. this._lastGenLine = generatedLine;
  48. this._lastSourceLine = line;
  49. this._lastSourceColumn = column;
  50. this._rawMappings.push({
  51. name: identifierName || undefined,
  52. generated: {
  53. line: generatedLine,
  54. column: generatedColumn
  55. },
  56. source: line == null ? undefined : filename || this._opts.sourceFileName,
  57. original: line == null ? undefined : {
  58. line: line,
  59. column: column
  60. }
  61. });
  62. }
  63. }
  64. exports.default = SourceMap;