This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
sourcemaps.js
121 lines (97 loc) · 3.44 KB
/
sourcemaps.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var sourceMap = require('source-map');
var path = require('path');
var fs = require('fs');
var toFileURL = require('./utils').toFileURL;
var fromFileURL = require('./utils').fromFileURL;
var wrapSourceMap = function(map) {
return new sourceMap.SourceMapConsumer(map);
};
var sourceMapRegEx = /^\s*\/\/[@#] ?(sourceURL|sourceMappingURL)=([^\n'"]+)/m;
exports.removeSourceMaps = function(source) {
return source.replace(sourceMapRegEx, '');
};
function getMapObject(map) {
if (typeof map != 'string')
return map;
try {
return JSON.parse(map);
}
catch(error) {
throw new Error('Invalid JSON: ' + map);
}
}
function isFileURL(url) {
return url.substr(0, 8) == 'file:///';
}
exports.concatenateSourceMaps = function(outFile, mapsWithOffsets, basePath, sourceMapContents) {
var generated = new sourceMap.SourceMapGenerator({
file: path.basename(outFile)
});
var outPath = path.dirname(outFile);
var contentsBySource = sourceMapContents ? {} : null;
mapsWithOffsets.forEach(function(pair) {
var offset = pair[0];
var map = getMapObject(pair[1]);
if (sourceMapContents && map.sourcesContent) {
for (var i=0; i<map.sources.length; i++) {
// If the source is an absolute path or file URL, use it as-is, otherwise prepend the sourceRoot.
var isAbsoluteOrFileUrl = path.isAbsolute(map.sources[i]) || isFileURL(map.sources[i]);
var source = isAbsoluteOrFileUrl ? map.sources[i] : (map.sourceRoot || '') + map.sources[i];
// If the source is still not a file URL, normalize the path.
if (!isFileURL(source))
{
source = path.normalize(source).replace(/\\/g, '/');
}
if (!source.match(/\/@traceur/)) {
if (!contentsBySource[source]) {
contentsBySource[source] = map.sourcesContent[i];
} else {
if (contentsBySource[source] != map.sourcesContent[i]) {
throw new Error("Mismatched sourcesContent for: " + source);
}
}
}
}
}
wrapSourceMap(map).eachMapping(function(mapping) {
if (mapping.originalLine == null || mapping.originalColumn == null || !mapping.source || mapping.source.match(/(\/|^)@traceur/))
return;
generated.addMapping({
generated: {
line: offset + mapping.generatedLine,
column: mapping.generatedColumn
},
original: {
line: mapping.originalLine,
column: mapping.originalColumn
},
source: mapping.source,
name: mapping.name
});
});
});
// normalize source paths and inject sourcesContent if necessary
var normalized = JSON.parse(JSON.stringify(generated));
if (sourceMapContents) {
normalized.sourcesContent = normalized.sources.map(function(source) {
if (contentsBySource[source])
return contentsBySource[source];
try {
return fs.readFileSync(path.resolve(basePath, source)).toString();
}
catch (e) {
return "";
}
});
}
normalized.sources = normalized.sources.map(function(source) {
var isRootRelative = /^[\\/]/.test(source);
if (isFileURL(source))
source = fromFileURL(source);
// If the source is not already a root-relative path, make it relative to the outPath.
if (!isRootRelative)
source = path.relative(outPath, path.resolve(basePath, source));
return source.replace(/\\/g, '/');
});
return JSON.stringify(normalized);
};