source_map_utils.js
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"use strict";
var source_map_1 = require("source-map");
/**
* Return a new RegExp object every time we want one because the
* RegExp object has internal state that we don't want to persist
* between different logical uses.
*/
function getInlineSourceMapRegex() {
return new RegExp('^//# sourceMappingURL=data:application/json;base64,(.*)$', 'mg');
}
function containsInlineSourceMap(source) {
return getInlineSourceMapCount(source) > 0;
}
exports.containsInlineSourceMap = containsInlineSourceMap;
function getInlineSourceMapCount(source) {
var match = source.match(getInlineSourceMapRegex());
return match ? match.length : 0;
}
exports.getInlineSourceMapCount = getInlineSourceMapCount;
function extractInlineSourceMap(source) {
var inlineSourceMapRegex = getInlineSourceMapRegex();
var previousResult = null;
var result = null;
// We want to extract the last source map in the source file
// since that's probably the most recent one added. We keep
// matching against the source until we don't get a result,
// then we use the previous result.
do {
previousResult = result;
result = inlineSourceMapRegex.exec(source);
} while (result !== null);
var base64EncodedMap = previousResult[1];
return Buffer.from(base64EncodedMap, 'base64').toString('utf8');
}
exports.extractInlineSourceMap = extractInlineSourceMap;
function removeInlineSourceMap(source) {
return source.replace(getInlineSourceMapRegex(), '');
}
exports.removeInlineSourceMap = removeInlineSourceMap;
/**
* Sets the source map inline in the file. If there's an existing inline source
* map, it clobbers it.
*/
function setInlineSourceMap(source, sourceMap) {
var encodedSourceMap = Buffer.from(sourceMap, 'utf8').toString('base64');
if (containsInlineSourceMap(source)) {
return source.replace(getInlineSourceMapRegex(), "//# sourceMappingURL=data:application/json;base64," + encodedSourceMap);
}
else {
return source + "\n//# sourceMappingURL=data:application/json;base64," + encodedSourceMap;
}
}
exports.setInlineSourceMap = setInlineSourceMap;
function sourceMapConsumerToGenerator(sourceMapConsumer) {
return source_map_1.SourceMapGenerator.fromSourceMap(sourceMapConsumer);
}
exports.sourceMapConsumerToGenerator = sourceMapConsumerToGenerator;
/**
* Tsc identifies source files by their relative path to the output file. Since
* there's no easy way to identify these relative paths when tsickle generates its
* own source maps, we patch them with the file name from the tsc source maps
* before composing them.
*/
function sourceMapGeneratorToConsumer(sourceMapGenerator, fileName, sourceName) {
var rawSourceMap = sourceMapGenerator.toJSON();
if (sourceName) {
rawSourceMap.sources = [sourceName];
}
if (fileName) {
rawSourceMap.file = fileName;
}
return new source_map_1.SourceMapConsumer(rawSourceMap);
}
exports.sourceMapGeneratorToConsumer = sourceMapGeneratorToConsumer;
function sourceMapTextToConsumer(sourceMapText) {
var sourceMapJson = sourceMapText;
return new source_map_1.SourceMapConsumer(sourceMapJson);
}
exports.sourceMapTextToConsumer = sourceMapTextToConsumer;
function sourceMapTextToGenerator(sourceMapText) {
var sourceMapJson = sourceMapText;
return source_map_1.SourceMapGenerator.fromSourceMap(sourceMapTextToConsumer(sourceMapJson));
}
exports.sourceMapTextToGenerator = sourceMapTextToGenerator;
//# sourceMappingURL=source_map_utils.js.map