-
Notifications
You must be signed in to change notification settings - Fork 0
/
source-map-explainer.js
executable file
·69 lines (54 loc) · 2.19 KB
/
source-map-explainer.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
#!/usr/bin/env node
var fs = require('fs');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var S = require('string');
var lineReader = require('line-reader');
function annotateFile(sourceFileName, sourceMapFileName, outputFileName){
if (outputFileName == sourceFileName){
throw new Error("outputFileName can't equal sourceFilename");
}
if (outputFileName == sourceMapFileName){
throw new Error("outputFileName can't equal sourceMapFileName");
}
var sourceMapContents = fs.readFileSync(sourceMapFileName, {encoding:'utf8'});
var sourceMap = JSON.parse(sourceMapContents);
var smc = new SourceMapConsumer(sourceMap);
var lineNumber = 1;
var outputString = "";
// Single line comments (//) appended to the right of the line are safer,
// but I don't think they look as good. If you prepend the comments,
// you need to use block comments (/* */) which can interfere with other block comments
// in the code. But you're probably not running this output code anyway
var prependComments = true;
lineReader.eachLine(sourceFileName, function(line, last) {
var sourceLocationString = " <nomap> ";
var lineLength = line.length;
var originalPosition;
for(var x=0;x<lineLength;++x){
originalPosition = smc.originalPositionFor({line:lineNumber,column:x});
// Did we find a mapping for this character?
if (originalPosition.source){
sourceLocationString = originalPosition.source + ", "+originalPosition.line +", "+ originalPosition.column+', '+x+'';
break;
}
}
if (prependComments){
outputString += "/* "+ sourceLocationString + " */" + " " + line + '\n';
} else {
outputString += S(line).padRight(80) + " //" + sourceLocationString + '\n';
}
++lineNumber;
if (last) {
// And, we're done!
fs.writeFileSync(outputFileName, outputString);
}
});
}
if (process.argv.length != 5){
console.log("\nUsage: source-map-explainer.js <js_source_file> <sourcemap_file> <output_file>")
process.exit(1);s
}
var file_to_annotate = process.argv[2];
var sourcemap_file = process.argv[3];
var output_file = process.argv[4];
annotateFile(file_to_annotate, sourcemap_file, output_file)