Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80552 views
1
'use strict';
2
var SourceMapGenerator = require('source-map').SourceMapGenerator;
3
4
function offsetMapping(mapping, offset) {
5
return { line: offset.line + mapping.line, column: offset.column + mapping.column };
6
}
7
8
function newlinesIn(src) {
9
if (!src) return 0;
10
var newlines = src.match(/\n/g);
11
12
return newlines ? newlines.length : 0;
13
}
14
15
function Generator(opts) {
16
opts = opts || {};
17
this.generator = new SourceMapGenerator({ file: opts.file || '', sourceRoot: opts.sourceRoot || '' });
18
this.sourcesContent = undefined;
19
}
20
21
/**
22
* Adds the given mappings to the generator and offsets them if offset is given
23
*
24
* @name addMappings
25
* @function
26
* @param sourceFile {String} name of the source file
27
* @param mappings {Array{{Object}} each object has the form { original: { line: _, column: _ }, generated: { line: _, column: _ } }
28
* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
29
* @return {Object} the generator to allow chaining
30
*/
31
Generator.prototype.addMappings = function (sourceFile, mappings, offset) {
32
var generator = this.generator;
33
34
offset = offset || {};
35
offset.line = offset.hasOwnProperty('line') ? offset.line : 0;
36
offset.column = offset.hasOwnProperty('column') ? offset.column : 0;
37
38
mappings.forEach(function (m) {
39
// only set source if we have original position to handle edgecase (see inline-source-map tests)
40
generator.addMapping({
41
source : m.original ? sourceFile : undefined
42
, original : m.original
43
, generated : offsetMapping(m.generated, offset)
44
});
45
});
46
return this;
47
};
48
49
/**
50
* Generates mappings for the given source, assuming that no translation from original to generated is necessary.
51
*
52
* @name addGeneratedMappings
53
* @function
54
* @param sourceFile {String} name of the source file
55
* @param source {String} source of the file
56
* @param offset {Object} offset to apply to each mapping. Has the form { line: _, column: _ }
57
* @return {Object} the generator to allow chaining
58
*/
59
Generator.prototype.addGeneratedMappings = function (sourceFile, source, offset) {
60
var mappings = []
61
, linesToGenerate = newlinesIn(source) + 1;
62
63
for (var line = 1; line <= linesToGenerate; line++) {
64
var location = { line: line, column: 0 };
65
mappings.push({ original: location, generated: location });
66
}
67
68
return this.addMappings(sourceFile, mappings, offset);
69
};
70
71
/**
72
* Adds source content for the given source file.
73
*
74
* @name addSourceContent
75
* @function
76
* @param sourceFile {String} The source file for which a mapping is included
77
* @param sourcesContent {String} The content of the source file
78
* @return {Object} The generator to allow chaining
79
*/
80
Generator.prototype.addSourceContent = function (sourceFile, sourcesContent) {
81
this.sourcesContent = this.sourcesContent || {};
82
this.sourcesContent[sourceFile] = sourcesContent;
83
return this;
84
};
85
86
/**
87
* @name base64Encode
88
* @function
89
* @return {String} bas64 encoded representation of the added mappings
90
*/
91
Generator.prototype.base64Encode = function () {
92
var map = this.toString();
93
return new Buffer(map).toString('base64');
94
};
95
96
/**
97
* @name inlineMappingUrl
98
* @function
99
* @return {String} comment with base64 encoded representation of the added mappings. Can be inlined at the end of the generated file.
100
*/
101
Generator.prototype.inlineMappingUrl = function () {
102
return '//# sourceMappingURL=data:application/json;base64,' + this.base64Encode();
103
};
104
105
Generator.prototype.toJSON = function () {
106
var map = this.generator.toJSON();
107
if (!this.sourcesContent) return map;
108
109
var toSourcesContent = (function (s) { return this.sourcesContent[s] || null; }).bind(this);
110
map.sourcesContent = map.sources.map(toSourcesContent);
111
return map;
112
};
113
114
Generator.prototype.toString = function () {
115
return JSON.stringify(this);
116
};
117
118
Generator.prototype._mappings = function () {
119
return this.generator._mappings._array;
120
};
121
122
Generator.prototype.gen = function () {
123
return this.generator;
124
};
125
126
module.exports = function (opts) { return new Generator(opts); };
127
module.exports.Generator = Generator;
128
129