You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

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