Skip to content

Instantly share code, notes, and snippets.

@jrasanen
Forked from zxbodya/source-map-unpacker.js
Created January 18, 2024 20:25
Show Gist options
  • Save jrasanen/cde83d69c603ef2fcea11bdeb857f508 to your computer and use it in GitHub Desktop.
Save jrasanen/cde83d69c603ef2fcea11bdeb857f508 to your computer and use it in GitHub Desktop.
script to extract sources, from sourcesContent field in sourcemap
'use strict';
var fs = require('fs');
var maps = [
fs.readFileSync('./main.xxxx.js.map'),
fs.readFileSync('./1.chunk.xxxx.js.map'),
fs.readFileSync('./2.chunk.xxxx.js.map'),
fs.readFileSync('./3.chunk.xxxx.js.map')
];
function mapping(name){
if (/^webpack:\/\/\/\.\//.test(name)) {
return 'unpacked/' + name.replace(/^webpack:\/\/\/\.\//, 'app/')
.replace(/\.(\w+)\?[./]*/, '\.$1');
}
// add more cases here to map all mossible source paths
return false;
}
maps.forEach(function (mainJSON) {
var main = JSON.parse(mainJSON);
main.sources.forEach(function (name, i) {
var dstFile;
if (dstFile = mapping(name)) {
if (!fs.existsSync(dstFile)) {
var dirs = dstFile.split('/');
dirs.slice(0,-1).reduce(function (acc, di) {
var dir = acc+di;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir+'/';
}, './');
fs.writeFileSync(dstFile, main.sourcesContent[i]);
}
} else {
console.log('Mapping missing for filename: '+name);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment