2017-09-07 13:16:03 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-09-22 13:51:03 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-09-22 13:51:03 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-09-07 13:16:03 -04:00
|
|
|
const vlq = require('vlq');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2017-09-22 13:51:03 -04:00
|
|
|
module.exports = function getMappings(bundlePath) {
|
2017-09-07 13:16:03 -04:00
|
|
|
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,
|
2020-04-13 19:40:21 -04:00
|
|
|
sourceFile: sourceMap.sources[sourceFileIndex],
|
|
|
|
genText,
|
|
|
|
sourceText
|
2017-09-07 13:16:03 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceLineIndex += sourceLineDiff;
|
|
|
|
sourceColIndex += sourceColDiff;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return matchData;
|
|
|
|
}, []);
|
2017-09-22 13:51:03 -04:00
|
|
|
};
|
2017-09-07 13:16:03 -04:00
|
|
|
|
|
|
|
function getFile(filePath) {
|
|
|
|
return fs.readFileSync(path.resolve(process.cwd(), filePath), 'UTF-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeLines(sourceMap) {
|
2020-04-13 19:40:21 -04:00
|
|
|
return sourceMap.mappings.split(';').map(line => {
|
|
|
|
return line.split(',').map(seg => vlq.decode(seg));
|
|
|
|
});
|
2017-09-07 13:16:03 -04:00
|
|
|
}
|