6

I have a list of .js files and I have a single minified version of these files with the source map. The minified file has been created using UglifyJS

Is it possible to extract code of the individual js files from the minified file? maybe by reading the source map?

eg:

original files:
1)head.js
2)tail.js
3)stomach.js
4)liver.js

minified file (containing code of all of above files):
-> body.min.js

source map:
-> body.min.js.map

Is it possible to extract the minified code (string) of stomach.js from body.min.js?

Imraan Khan
  • 315
  • 1
  • 2
  • 9
  • This is an interesting question. Can you provide a little context? Are you afraid someone else will reverse engineer your code? Are you trying to reverse engineer someone else's code? Have you lost the original files and are attempting to restore them from the minified versions? – JDB still remembers Monica Mar 16 '16 at 14:08
  • Basically I want to generate some code on runtime. the final code might not include code from all files. And I also want to avoid running a minification of the final code – Imraan Khan Mar 16 '16 at 14:18
  • Then why combine the files in the first place? I think extracting code is going to be far more expensive then minifying it. If you minify each file individually, wouldn't that give you the best of both worlds? You can combine as needed without having to minify at runtime. – JDB still remembers Monica Mar 16 '16 at 14:21

1 Answers1

11

This script will expand minified source (via give sourcemap), and recreate all the files; essentially mirroring the original source repo. Is this what you needed? Quick and dirty, but might do the trick. (only tested against sourcemaps derived from webpack)

https://gist.github.com/hooptie45/6f9a7e6251a120c8d2d04e75f9d73c0e#file-sourcemap-extract-rb

$ ./sourcemap-extract.rb site.com/assets/app.min.js.map ./tmp

user407275
  • 126
  • 2
  • 3
  • This is a beautiful thing, thank you! Helping rescue a failed project where source wasn't turned over from the original team, and this got us 95% of the way to restoring the original code. – Joshua Jan 18 '19 at 21:59