Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80537 views
1
'use strict';
2
3
var convert = require('convert-source-map');
4
var createGenerator = require('inline-source-map');
5
var mappingsFromMap = require('./lib/mappings-from-map');
6
7
function resolveMap(source) {
8
var gen = convert.fromSource(source);
9
return gen ? gen.toObject() : null;
10
}
11
12
function hasInlinedSource(existingMap) {
13
return existingMap.sourcesContent && !!existingMap.sourcesContent[0];
14
}
15
16
function Combiner(file, sourceRoot) {
17
// since we include the original code in the map sourceRoot actually not needed
18
this.generator = createGenerator({ file: file || 'generated.js', sourceRoot: sourceRoot });
19
}
20
21
Combiner.prototype._addGeneratedMap = function (sourceFile, source, offset) {
22
this.generator.addGeneratedMappings(sourceFile, source, offset);
23
this.generator.addSourceContent(sourceFile, source);
24
return this;
25
};
26
27
Combiner.prototype._addExistingMap = function (sourceFile, source, existingMap, offset) {
28
var mappings = mappingsFromMap(existingMap);
29
30
var originalSource = existingMap.sourcesContent[0]
31
, originalSourceFile = existingMap.sources[0];
32
33
this.generator.addMappings(originalSourceFile || sourceFile, mappings, offset);
34
this.generator.addSourceContent(originalSourceFile || sourceFile, originalSource);
35
return this;
36
};
37
38
/**
39
* Adds map to underlying source map.
40
* If source contains a source map comment that has the source of the original file inlined it will offset these
41
* mappings and include them.
42
* If no source map comment is found or it has no source inlined, mappings for the file will be generated and included
43
*
44
* @name addMap
45
* @function
46
* @param opts {Object} { sourceFile: {String}, source: {String} }
47
* @param offset {Object} { line: {Number}, column: {Number} }
48
*/
49
Combiner.prototype.addFile = function (opts, offset) {
50
51
offset = offset || {};
52
if (!offset.hasOwnProperty('line')) offset.line = 0;
53
if (!offset.hasOwnProperty('column')) offset.column = 0;
54
55
var existingMap = resolveMap(opts.source);
56
57
return existingMap && hasInlinedSource(existingMap)
58
? this._addExistingMap(opts.sourceFile, opts.source, existingMap, offset)
59
: this._addGeneratedMap(opts.sourceFile, opts.source, offset);
60
};
61
62
/**
63
* @name base64
64
* @function
65
* @return {String} base64 encoded combined source map
66
*/
67
Combiner.prototype.base64 = function () {
68
return this.generator.base64Encode();
69
};
70
71
/**
72
* @name comment
73
* @function
74
* @return {String} base64 encoded sourceMappingUrl comment of the combined source map
75
*/
76
Combiner.prototype.comment = function () {
77
return this.generator.inlineMappingUrl();
78
};
79
80
/**
81
* @name create
82
* @function
83
* @param file {String} optional name of the generated file
84
* @param sourceRoot { String} optional sourceRoot of the map to be generated
85
* @return {Object} Combiner instance to which source maps can be added and later combined
86
*/
87
exports.create = function (file, sourceRoot) { return new Combiner(file, sourceRoot); };
88
89
/**
90
* @name removeComments
91
* @function
92
* @param src
93
* @return {String} src with all sourceMappingUrl comments removed
94
*/
95
exports.removeComments = function (src) {
96
if (!src.replace) return src;
97
return src.replace(convert.commentRegex, '');
98
};
99
100