0

I am new to the minification of JavaScript. This is the setting of the problem:

Assuming we have an original JavaScript code A and its minified code A′ (which is generated by a minifier like UglifyJS or Closure Compiler), how can I:

  1. Count the number of variables which have been renamed, and

  2. Map every variable's original name to its minified name

Any detailed instructions are welcomed, with the tool UglifyJS or Closure Compiler would be better :)

Aliu
  • 99
  • 9
  • 1
    You probably want a minifier that will generate a [sourceMap](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/). – jfriend00 Jun 20 '15 at 00:55
  • Can you clarify what your actual goal is? – Jethro Larson Jun 20 '15 at 01:56
  • @jfriend00 I did generate the sourceMap file. But it is hard to understand the mappings in there. Can your give some details? Thx! – Aliu Jun 20 '15 at 02:11
  • @JethroLarson My goal is to calculate how many variables are affected in the minification, since some variables may not be renamed. – Aliu Jun 20 '15 at 02:12
  • Since the point of a sourceMap is to map minimized code back to the original source, including variable names, it seems likely that all the info you want is in there. I don't personally know the file format. You will have to do some research to see what's been written about it. Here's [some documatation](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#) Google wrote about it. – jfriend00 Jun 20 '15 at 03:03

1 Answers1

0

There are two approaches:

1) The Closure Compiler can produce a "renaming map" for both properties and variables. The map does not include unrenamed variables so that there would still be some work for you to do. See the --variable_renaming_report command-line option https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CommandLineRunner.java#L215.

2) The Closure Compiler and other tools produce source maps. The source map can be used to map every byte back to the original character in the original.

For the second the Closure Compiler project includes a Java Library for reading source maps: https://github.com/google/closure-compiler/blob/master/src/com/google/debugging/sourcemap/SourceMapConsumerV3.java#L225

There also exist javascript source map utilities: https://github.com/mozilla/source-map/

John
  • 5,443
  • 15
  • 21
  • Thanks! Using source-map library is convenient to map minified codes back to original source :) – Aliu Jun 26 '15 at 00:59