UNPKG

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