forked from systemjs/builder
/
sourcemaps.js
105 lines (86 loc) · 2.82 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
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 = /\/\/[@#] ?(sourceURL|sourceMappingURL)=([^\n'"]+)/;
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++) {
var source = (map.sourceRoot || '') + map.sources[i];
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) {
if (isFileURL(source))
source = fromFileURL(source);
return path.relative(outPath, path.resolve(basePath, source)).replace(/\\/g, '/');
});
return JSON.stringify(normalized);
};