Files
react-native-devtools-frontend/node_modules/karma-sourcemap-loader/index.js
T
Tim van der Lippe 455069ae90 Add karma-sourcemap-loader to node_modules
This will be used to load sourcemap files for TypeScript unittests.

R=aerotwist@chromium.org,jacktfranklin@chromium.org
DISABLE_THIRD_PARTY_CHECK=Add node_module

Bug: 1061125
Change-Id: I13cfdbe078402b02563e7843595016a14bf93617
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2105436
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Paul Lewis <aerotwist@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
2020-03-16 16:40:16 +00:00

69 lines
2.0 KiB
JavaScript

var fs = require('fs');
var path = require('path');
var createSourceMapLocatorPreprocessor = function(args, logger, helper) {
var log = logger.create('preprocessor.sourcemap');
log.debug('loaded');
return function(content, file, done) {
function sourceMapData(data){
file.sourceMap = JSON.parse(data);
done(content);
}
function inlineMap(inlineData){
var data;
var b64Match = inlineData.match(/^data:.+\/(.+);base64,(.*)$/);
if (b64Match !== null && b64Match.length == 3) {
// base64-encoded JSON string
log.debug('base64-encoded source map for', file.originalPath);
var buffer = new Buffer(b64Match[2], 'base64');
sourceMapData(buffer.toString());
} else {
// straight-up URL-encoded JSON string
log.debug('raw inline source map for', file.originalPath);
sourceMapData(decodeURIComponent(inlineData.slice('data:application/json'.length)));
}
}
function fileMap(mapPath){
fs.exists(mapPath, function(exists) {
if (!exists) {
log.debug('sourcemap file does not exist');
done(content);
return;
}
log.debug('test');
fs.readFile(mapPath, function(err, data) {
if (err){ throw err; }
log.debug('external source map exists for', file.originalPath);
sourceMapData(data);
});
});
}
var match = content.match(/#\s*sourceMappingURL=(.+)$/m);
log.debug(match);
if (match !== null && match.length == 2) {
var mapUrl = match[1];
if( /^data:application\/json/.test(mapUrl) ){
inlineMap(mapUrl);
} else {
fileMap(path.resolve(path.dirname(file.path), mapUrl));
}
} else {
fileMap(file.path + ".map");
}
};
};
createSourceMapLocatorPreprocessor.$inject = ['args', 'logger', 'helper'];
// PUBLISH DI MODULE
module.exports = {
'preprocessor:sourcemap': ['factory', createSourceMapLocatorPreprocessor]
};