Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80538 views
1
'use strict';
2
3
var path = require('path');
4
var convert = require('convert-source-map');
5
var memoize = require('lodash.memoize');
6
var createGenerator = require('inline-source-map');
7
var pathIsAbsolute = require('./lib/path-is-absolute');
8
var mappingsFromMap = require('./lib/mappings-from-map');
9
10
var protocolRx = /^[a-z]+:\/\//;
11
12
/**
13
* Rebases a relative path in 'sourceFile' to be relative
14
* to the path where 'sourceFile' is located.
15
*
16
* This is necessary before adding relative paths to the
17
* new combined map to ensure all paths are relative to their
18
* original source.
19
*
20
* The 'sourceRoot' from the original source map is joined
21
* as well to ensure the complete path.
22
*
23
* Resulting paths that are absolute are passed along directly.
24
*
25
* @param sourceFile {String} path to the original source file that references a map
26
* @param relativeRoot {String} sourceRoot in sourceFile's map to combine with relativePath
27
* @param relativePath {String} source path from sourceFile's map
28
*/
29
var rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath) {
30
if (!relativePath) {
31
return relativePath;
32
}
33
34
// join relative path to root (e.g. 'src/' + 'file.js')
35
var relativeRootedPath = relativeRoot ? path.join(relativeRoot, relativePath) : relativePath;
36
37
if (sourceFile === relativeRootedPath || // same path,
38
pathIsAbsolute(relativeRootedPath) || // absolute path, nor
39
protocolRx.test(relativeRootedPath)) { // absolute protocol need rebasing
40
return relativeRootedPath;
41
}
42
43
// make relative to source file
44
return path.join(path.dirname(sourceFile), relativeRootedPath);
45
}, function(a, b, c) {
46
return a + '::' + b + '::' + c;
47
});
48
49
function resolveMap(source) {
50
var gen = convert.fromSource(source);
51
return gen ? gen.toObject() : null;
52
}
53
54
function hasInlinedSource(existingMap) {
55
return existingMap.sourcesContent && !!existingMap.sourcesContent[0];
56
}
57
58
function Combiner(file, sourceRoot) {
59
// since we include the original code in the map sourceRoot actually not needed
60
this.generator = createGenerator({ file: file || 'generated.js', sourceRoot: sourceRoot });
61
}
62
63
Combiner.prototype._addGeneratedMap = function (sourceFile, source, offset) {
64
this.generator.addGeneratedMappings(sourceFile, source, offset);
65
this.generator.addSourceContent(sourceFile, source);
66
return this;
67
};
68
69
Combiner.prototype._addExistingMap = function (sourceFile, source, existingMap, offset) {
70
var mappings = mappingsFromMap(existingMap);
71
72
// add all of the sources from the map
73
for (var i = 0, len = existingMap.sources.length; i < len; i++) {
74
if (!existingMap.sourcesContent) continue;
75
76
this.generator.addSourceContent(
77
rebaseRelativePath(sourceFile, existingMap.sourceRoot, existingMap.sources[i]),
78
existingMap.sourcesContent[i]);
79
}
80
81
// add the mappings, preserving the original mapping 'source'
82
mappings.forEach(function(mapping) {
83
// Add the mappings one at a time because 'inline-source-map' doesn't handle
84
// mapping source filenames. The mapping.source already takes sourceRoot into account
85
// per the SMConsumer.eachMapping function, so pass null for the root here.
86
this.generator.addMappings(
87
rebaseRelativePath(sourceFile, null, mapping.source), [mapping], offset);
88
}, this);
89
90
return this;
91
};
92
93
/**
94
* Adds map to underlying source map.
95
* If source contains a source map comment that has the source of the original file inlined it will offset these
96
* mappings and include them.
97
* If no source map comment is found or it has no source inlined, mappings for the file will be generated and included
98
*
99
* @name addMap
100
* @function
101
* @param opts {Object} { sourceFile: {String}, source: {String} }
102
* @param offset {Object} { line: {Number}, column: {Number} }
103
*/
104
Combiner.prototype.addFile = function (opts, offset) {
105
106
offset = offset || {};
107
if (!offset.hasOwnProperty('line')) offset.line = 0;
108
if (!offset.hasOwnProperty('column')) offset.column = 0;
109
110
var existingMap = resolveMap(opts.source);
111
112
return existingMap && hasInlinedSource(existingMap)
113
? this._addExistingMap(opts.sourceFile, opts.source, existingMap, offset)
114
: this._addGeneratedMap(opts.sourceFile, opts.source, offset);
115
};
116
117
/**
118
* @name base64
119
* @function
120
* @return {String} base64 encoded combined source map
121
*/
122
Combiner.prototype.base64 = function () {
123
return this.generator.base64Encode();
124
};
125
126
/**
127
* @name comment
128
* @function
129
* @return {String} base64 encoded sourceMappingUrl comment of the combined source map
130
*/
131
Combiner.prototype.comment = function () {
132
return this.generator.inlineMappingUrl();
133
};
134
135
/**
136
* @name create
137
* @function
138
* @param file {String} optional name of the generated file
139
* @param sourceRoot {String} optional sourceRoot of the map to be generated
140
* @return {Object} Combiner instance to which source maps can be added and later combined
141
*/
142
exports.create = function (file, sourceRoot) { return new Combiner(file, sourceRoot); };
143
144
/**
145
* @name removeComments
146
* @function
147
* @param src
148
* @return {String} src with all sourceMappingUrl comments removed
149
*/
150
exports.removeComments = function (src) {
151
if (!src.replace) return src;
152
return src.replace(convert.commentRegex, '').replace(convert.mapFileCommentRegex, '');
153
};
154
155