-2

The title is pretty self-explanatory. I have a minified JavaScript code (100k+ lines) and would like to find all occurrences of a particular variable so I could rename it to better understand code. Is there a way to do that?

  • 5
    Does your text editor have a search function? – TZHX Mar 18 '16 at 10:32
  • 2
    Try to work on the non-minified form of the source code. – Basile Starynkevitch Mar 18 '16 at 11:41
  • 1
    Rename the variable and find where your program fails? – Pieter B Mar 18 '16 at 13:16
  • 2
    grep -i "variable_name" – GordonM Mar 18 '16 at 14:33
0

Unless this variable is global, there is no way of doing that because with javascript's closure, you can have a tons of "a" variable that doesn't match to the same things.

  • Yes, but in that case I could "divide" file into constituent parts to apply changes and "glue" it back together. You actually gave me an idea. – M. Doe Mar 18 '16 at 12:49
  • well you can cut to everywhere the debogguer pass from a localized entry point but that's a long way to go. – Walfrat Mar 18 '16 at 13:12
0

There isn't really a foolproof way to do this, as mentioned in Walfrats answer, closures and the like make this too difficult.

However, it is possible if the library created a min.map file to map the minification process when it was minified. This is called source mapping and allows the minification process to be reversed. Many big JS libraries such as jQuery have this. You can read more about this here: Introduction to JavaScript Source Maps

However, if the library has a min.map file, then it is probably open source anyway. Searching for the name of the minified JS file will probably lead you to its Github (or similar) page where you would be able to see the unminified source.

Unfortunately, if there is no min.map file, and the code isn't open source, then you are probably out of luck.

  • 1
    jQuery offers the unminified source for download anyway. – Robert Harvey Mar 18 '16 at 19:53
  • @RobertHarvey hence my third paragraph 'However, if the library has a min.map file, then it is probably open source anyway.' - I was pointing out that although there exists a way to un-minify code it is mostly pointless if you just want to study the code as the unminified version is usually available. Not sure how to edit my answer to make that point clearer? – razethestray Mar 18 '16 at 20:52
  • I'm not sure how important the min.map file is. Why does that even exist, if the unminified source is probably available in any case? – Robert Harvey Mar 18 '16 at 20:55
  • @RobertHarvey the only use I can think of is the ability to debug on production. I sometimes come across bugs on our live website that I can't initially reproduce on dev or test, and often curse the fact that all my functions and variables are called a, b and c as it can take me a while to work out what is going on, especially if it isn't code I've seen in a while... we don't have a source mapping files though so I havn't used it myself! But I believe that is the intention. – razethestray Mar 18 '16 at 20:59
  • 1
    I would say it serves the same purpose as 'Remote Debugging' and .pdb symbol maps do for compiled C# code (in release mode) – razethestray Mar 18 '16 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.