Files
react-native-devtools-frontend/node_modules/karma-sourcemap-loader/index.js
T
Tim van der Lippe 8fa65441f9 Add filesystem TypeScript types
We use these types for all of our interactions with the local filesystem
API's and they don't normally ship with TypeScript library definitions.

R=jacktfranklin@chromium.org
DISABLE_THIRD_PARTY_CHECK=Add node_module

Bug: 1011811
Change-Id: I41ae8faf8cce289c86a6795ff76d8a10b5b3609d
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2108760
Auto-Submit: Tim van der Lippe <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
2020-03-18 17:58:39 +00:00

64 lines
1.8 KiB
JavaScript

var fs = require('fs');
var path = require('path');
var createSourceMapLocatorPreprocessor = function(args, logger, helper) {
var log = logger.create('preprocessor.sourcemap');
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) {
done(content);
return;
}
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);
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]
};