Created
January 30, 2020 23:12
-
-
Save m1k1o/7c97d64bd1e06f0303ae4a713fca0fa3 to your computer and use it in GitHub Desktop.
Create file structure from source-maps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs'); | |
function ensureDirectoryExistence(filePath) { | |
let dirname = path.dirname(filePath); | |
if (fs.existsSync(dirname)) { | |
return true; | |
} | |
ensureDirectoryExistence(dirname); | |
fs.mkdirSync(dirname); | |
} | |
function restoreSourceMap(map) { | |
for ( i=0; i < map.sources.length; i++ ) { | |
let file = map.sources[i]; | |
// webpack:// -> _WEBPACK_ | |
file = file.replace(/webpack:\/\//, "_WEBPACK_"); | |
// config sync /^/.//.*//config/.json$ -> config sync/__REGEX__.json | |
file = file.replace(/[\s*\/]*\^(.*?)\/(\.[a-z]+)\$/, "/__REGEX__$2") | |
// a/./b -> a/b | |
file = file.replace(/\/\.\//g, "/"); | |
// a.svg?1234 -> a.svg | |
file = file.replace(/\?.*$/g, ""); | |
console.log(file); | |
ensureDirectoryExistence(file); | |
fs.writeFileSync(file, map.sourcesContent[i]) | |
} | |
} | |
fs.readdirSync('./').forEach(file => { | |
if(/\.map$/.test(file)) { | |
let data_RAW = fs.readFileSync(file); | |
let data_JSON = JSON.parse(data_RAW); | |
restoreSourceMap(data_JSON); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment