Created
October 25, 2019 20:30
-
-
Save thomaswilburn/45e4d41696a3637eece49a9e8546cf26 to your computer and use it in GitHub Desktop.
source-un-map
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
var http = require("http"); | |
var https = require("https"); | |
var { SourceMapConsumer } = require("source-map"); | |
var fs = require("fs").promises; | |
var path = require("path"); | |
var fetch = function(address) { | |
return new Promise(function(ok, fail) { | |
var parsed = new URL(address); | |
var remote = parsed.protocol == "http:" ? http : https; | |
var { host, pathname, search } = parsed; | |
var p = remote.get({ | |
host, | |
path: pathname + search, | |
headers: { | |
"User-Agent": "Radio" | |
} | |
}, function(proxied) { | |
if (proxied.statusCode > 300 && proxied.headers.location) { | |
return fetch(proxied.headers.location, output); | |
} | |
var body = ""; | |
proxied.on("data", chunk => body += chunk.toString("utf-8")); | |
proxied.on("end", () => ok(body)); | |
}); | |
p.on("error", fail); | |
}); | |
} | |
var main = async function(input, output = "./output") { | |
var js = await fetch(input); | |
var smFile = js.match(/^\/\/# sourceMappingURL\s*=\s*(.+)$/m)[1]; | |
var smURL = new URL(smFile, input); | |
var map = await fetch(smURL.toString()); | |
var consumer = await new SourceMapConsumer(map); | |
output = path.resolve(output); | |
for (var source of consumer.sources) { | |
if (source.match(/node_modules/)) continue; | |
var file = path.join(output, source); | |
var contents = consumer.sourceContentFor(source); | |
await fs.mkdir(path.dirname(file), { recursive: true }); | |
await fs.writeFile(file, contents); | |
} | |
consumer.destroy(); | |
} | |
var args = process.argv.slice(2); | |
var [input, output] = args; | |
main(input, output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment