UNPKG

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