2

When I test the following code in closure compiler at http://closure-compiler.appspot.com:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @formatting pretty_print
// ==/ClosureCompiler==

// These get renamed
window.foo = {};
window.bar = {};

// These don't
window.uid = {};
window.test = {};

The output is:

window.a = {};
window.b = {};
window.uid = {};
window.test = {};

Why does it rename :

window.foo = {};
window.bar = {};

But not:

window.uid = {};
window.test = {};

It seems to be an issue with certain words?

Sean Bannister
  • 3,105
  • 4
  • 31
  • 43

1 Answers1

6

Update

As of the 20150315 release of Closure-compiler, the type based optimizations are enabled by default.


Closure Compiler will not rename properties that have the same name as any property defined on an object in the externs unless the --use_types_for_optimization flag is enabled. See the project FAQ for more details.

nd97
  • 95
  • 1
  • 8
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • "test" is a property on RegExp objects, and for "uid" this: ScriptProfile.prototype.uid; – John Oct 01 '12 at 15:10
  • I tried ```--use_types_for_optimization``` but still seeing the same result. The default externs mentioned don't reference .uid – Sean Bannister Oct 01 '12 at 15:12
  • Thanks John, cross posted, I didn't see that uid reference but I'll take another look. – Sean Bannister Oct 01 '12 at 15:13
  • Found what was wrong the parameter is actually called ```--use_only_custom_externs```. Works great thanks :) – Sean Bannister Oct 01 '12 at 15:15
  • 2
    `--use_only_custom_externs` is different. It excludes the default externs which is giving you the renaming effect you are after. However, it may not be exactly what you intend as the default DOM object definitions now won't exist unless you manually add them back. – Chad Killingsworth Oct 01 '12 at 15:29
  • I won't be using ```--use_only_custom_externs``` in production its just good to be able to switch it on and check that my object with the name ```uid``` is still has the right name in case they ever remove ```ScriptProfile.prototype.uid``` for whatever reason. Just handy for testing similar use cases. – Sean Bannister Oct 01 '12 at 16:50