Created
April 2, 2021 18:11
-
-
Save michaelkitson/a845b9089208039f3e6e988cc06377d9 to your computer and use it in GitHub Desktop.
A quick ruby script to extract sources from a JS sourcemap
This file contains hidden or 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
require 'json' | |
require 'rubygems/package' | |
unless ARGV.length == 1 | |
puts "Usage: ruby sourcemap_to_tar.rb <sourcemap_file>" | |
exit 1 | |
end | |
sourcemap_filename = ARGV.shift | |
sourcemap = JSON.parse(File.read(sourcemap_filename)) | |
source_filenames = sourcemap['sources'].map { |x| x.sub(%r{^webpack:///}, '').sub(%r{^\./}, '') } | |
source_contents = sourcemap['sourcesContent'] | |
File.open("#{sourcemap_filename}.tar", "wb") do |file| | |
Gem::Package::TarWriter.new(file) do |tar| | |
source_filenames.zip(source_contents).each do |filename, content| | |
tar.add_file(filename, 0644) do |io| | |
io.write(content) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment