source-map.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. exports.__esModule = true;
  3. var _keys = require("babel-runtime/core-js/object/keys");
  4. var _keys2 = _interopRequireDefault(_keys);
  5. var _typeof2 = require("babel-runtime/helpers/typeof");
  6. var _typeof3 = _interopRequireDefault(_typeof2);
  7. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  8. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  9. var _sourceMap = require("source-map");
  10. var _sourceMap2 = _interopRequireDefault(_sourceMap);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var SourceMap = function () {
  13. function SourceMap(opts, code) {
  14. (0, _classCallCheck3.default)(this, SourceMap);
  15. this._cachedMap = null;
  16. this._code = code;
  17. this._opts = opts;
  18. this._rawMappings = [];
  19. }
  20. SourceMap.prototype.get = function get() {
  21. if (!this._cachedMap) {
  22. var map = this._cachedMap = new _sourceMap2.default.SourceMapGenerator({
  23. file: this._opts.sourceMapTarget,
  24. sourceRoot: this._opts.sourceRoot
  25. });
  26. var code = this._code;
  27. if (typeof code === "string") {
  28. map.setSourceContent(this._opts.sourceFileName, code);
  29. } else if ((typeof code === "undefined" ? "undefined" : (0, _typeof3.default)(code)) === "object") {
  30. (0, _keys2.default)(code).forEach(function (sourceFileName) {
  31. map.setSourceContent(sourceFileName, code[sourceFileName]);
  32. });
  33. }
  34. this._rawMappings.forEach(map.addMapping, map);
  35. }
  36. return this._cachedMap.toJSON();
  37. };
  38. SourceMap.prototype.getRawMappings = function getRawMappings() {
  39. return this._rawMappings.slice();
  40. };
  41. SourceMap.prototype.mark = function mark(generatedLine, generatedColumn, line, column, identifierName, filename) {
  42. if (this._lastGenLine !== generatedLine && line === null) return;
  43. if (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. return SourceMap;
  64. }();
  65. exports.default = SourceMap;
  66. module.exports = exports["default"];