Search code examples
javascriptnode.jssource-mapsuglifyjs2

Extract individual javascript file code from minified file


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?


Solution

  • 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