Skip to content

Instantly share code, notes, and snippets.

@michaelkitson
Created April 2, 2021 18:11
Show Gist options
  • Save michaelkitson/a845b9089208039f3e6e988cc06377d9 to your computer and use it in GitHub Desktop.
Save michaelkitson/a845b9089208039f3e6e988cc06377d9 to your computer and use it in GitHub Desktop.
A quick ruby script to extract sources from a JS sourcemap
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