62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// Gracias a:
|
|
// - Código original: https://github.com/akx/source-from-sourcemaps
|
|
// - Aguante: https://gist.github.com/banyudu/b17a9cb3f05296b76a9f3051f66c3dcd
|
|
// - Algoritmo: https://github.com/rarecoil/unwebpack-sourcemap
|
|
|
|
const fs = require("fs")
|
|
const util = require("util")
|
|
const writeFileP = util.promisify(fs.writeFile)
|
|
const readFileP = util.promisify(fs.readFile)
|
|
const mkdirP = util.promisify(fs.mkdir)
|
|
const path = require("path")
|
|
const { SourceMapConsumer } = require("source-map")
|
|
|
|
const outputDir = 'sources-gen'
|
|
|
|
function sanitizePath (path) {
|
|
let newPath = path
|
|
.replace(/^webpack:\/\/\/?/, "")
|
|
.replace(/~\//, "node_modules/")
|
|
.replace(/\?.+$/, "")
|
|
.replace(/\.\.\//g, 'parent_dir/')
|
|
.replace(/\.\//g, '')
|
|
return newPath
|
|
}
|
|
|
|
async function processFile(pth) {
|
|
const data = await readFileP(pth, "UTF-8");
|
|
const consumer = await new SourceMapConsumer(data);
|
|
|
|
const processors = consumer.sources.map(async (src) => {
|
|
const fsSrc = path.join(
|
|
outputDir,
|
|
sanitizePath(src)
|
|
)
|
|
if (fsSrc.includes('node_modules')) {
|
|
console.warn(`Not saving ${fsSrc} because it is node_modules`)
|
|
return
|
|
}
|
|
|
|
const source = consumer.sourceContentFor(src, true)
|
|
if (!source) {
|
|
console.warn("Unable to source", src)
|
|
return
|
|
}
|
|
await mkdirP(path.dirname(fsSrc), { recursive: true })
|
|
await writeFileP(fsSrc, source, "UTF-8")
|
|
console.log(`Wrote ${fsSrc}`)
|
|
})
|
|
await Promise.all(processors)
|
|
|
|
consumer.destroy();
|
|
}
|
|
|
|
function main() {
|
|
return processFile(process.argv[2])
|
|
}
|
|
|
|
if (!module.parent) {
|
|
main().then(() => process.exit(0));
|
|
}
|