Skip to content

Instantly share code, notes, and snippets.

@davestewart
Last active April 8, 2024 11:00
Show Gist options
  • Save davestewart/92ba01044fcb16ac374d9c89c7499770 to your computer and use it in GitHub Desktop.
Save davestewart/92ba01044fcb16ac374d9c89c7499770 to your computer and use it in GitHub Desktop.
Decompile JavaScript from source maps

Decompile JavaScript from source maps

Overview

Modern JavaScript build tools compile entire folder structures of JavaScript code into single, minified files that are near-impossible to read, but can also include source maps which can be used to display the original code in tools such as the Chrome DevTools Sources panel.

These source maps can be processed to extract mainly meaningful code and file structures, by installing a package calling Shuji and running a simple bash command.

Generally, production builds shouldn't include source maps, but if you do manage to lose your source files, or for some (obviously, ethical!) reason need to view the original files, and you happen to have / find the source maps, you're good to go.

Instructions

This code will work on Mac/Unix.

  1. Locate your compiled files, which should include *.js.map files
  2. Create a new folder <project>
  3. Place files in <project>/input
  4. Run the script below from <project>
  5. View the files

Problems

Inline source maps

This solution doesn't handle inline source maps.

The source map I looked at was base-64 decoded (not sure if they all are) so you can get this into an external file by:

  1. Copying the encoded part of the source map (everything after the comma from base64,)
  2. In the browser, run copy(atob('<the copied data>'))
  3. Create a new file next to original .js file called <name of original file>.map.js
  4. Paste the copied JSON into this file
  5. Run the original script

I'm not sure if there is a way to automate this in the terminal; I may look at packaging this up.

The source files are not the same as the originals

Only the information in the source maps can be restored.

If these don't contain the full source code, then you're out of luck. Work with what you have!

Links

echo install decompiler
npm install shuji
echo find and extract files
find input -name '*.js.map' -exec shuji {} -o output -p \;
echo view files
mv output/input output
open output
@estebangarviso
Copy link

for MacOS: find . -iregex '.*\.\js\.map' | xargs -n 1 shuji -o output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment