Statistics on renaming via JavaScript minifier

168 views Asked by At

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 :)

1

There are 1 answers

1
John On BEST ANSWER

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/