forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseMap.js
64 lines (52 loc) · 1.96 KB
/
parseMap.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
#!/usr/bin/env node
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
const vlq = require('vlq');
const fs = require('fs');
const path = require('path');
module.exports = function getMappings(bundlePath) {
const sourceMap = JSON.parse(getFile(`${bundlePath}.map`));
const sourcesContent = sourceMap.sourcesContent.map(file => file.split('\n'));
const bundleLines = getFile(bundlePath).split('\n');
let sourceLines = sourcesContent[0];
let sourceFileIndex = 0;
let sourceLineIndex = 0;
let sourceColIndex = 0;
return decodeLines(sourceMap).reduce((matchData, mapLine, genLineIndex) => {
mapLine.forEach((segment, index) => {
if (segment.length) {
const [genColDiff, sourceFileDiff, sourceLineDiff, sourceColDiff] = segment;
// if source file changes, grab new file from sourcesContent
if (sourceFileDiff !== 0) {
sourceFileIndex += sourceFileDiff;
sourceLines = sourcesContent[sourceFileIndex];
}
const genText = bundleLines[genLineIndex].trim();
const sourceText = sourceLines[sourceLineIndex + sourceLineDiff].trim();
// only record mappings that are long enough to be meaningful
if (index === 0 && genText.length > 15 && sourceText.length > 15) {
matchData.push({
genLineIndex,
sourceLineIndex,
sourceFile: sourceMap.sources[sourceFileIndex], genText, sourceText
});
}
sourceLineIndex += sourceLineDiff;
sourceColIndex += sourceColDiff;
}
});
return matchData;
}, []);
};
function getFile(filePath) {
return fs.readFileSync(path.resolve(process.cwd(), filePath), 'UTF-8');
}
function decodeLines(sourceMap) {
return sourceMap.mappings.split(';').map(
line => { return line.split(',').map(seg => vlq.decode(seg)); });
}