4

I have a complex dojo app that works correctly uncompiled, but after compiling with Google's Closure Compiler, I get subtle differences in some behaviours.

As it is, it's exceedingly difficult to debug, and I've been unable to find any information about possible functional differences between compiled and uncompiled Javascript with Google Closure.

Can anyone point me in the direction of known differences, or share any similar experiences and some ideas of where to start looking?

4
  • No clue. But you may want to give UglifyJS a shot. I'm a big fan.
    – Alex Wayne
    Commented Jan 3, 2013 at 21:26
  • @AlexWayne Definitely want to keep Closure - the dojo build system works particularly well with it.
    – Hamish
    Commented Jan 3, 2013 at 21:28
  • 1
    What optimization level are you using? You can read about some limitations of the closure compiler here: developers.google.com/closure/compiler/docs/limitations
    – Bart
    Commented Jan 3, 2013 at 21:30
  • @Bart SIMPLE_OPTIMIZATIONS - there are no with statements in the code base, and a single eval that is trivial and definitely unrelated to the bug I'm seeing.
    – Hamish
    Commented Jan 3, 2013 at 21:41

3 Answers 3

10

General Closure Compiler Debugging Tips

  • Use the VERBOSE warning level. This turns on all of the checks.
  • Use the debug flag. It makes renamed symbols ridiculously long, but they are named in such a way you can find the original. If code works with the debug flag but not without it, it is almost certainly a renaming issue.
  • Definitely use formatting=PRETTY_PRINT. Debugging compacted code is painful without it.
  • Use source maps
  • Disable the type based optimizations with --use_types_for_optimization false. Incorrect type annotations can cause the compiler to make invalid assumptions.

UPDATE: As of the 20150315 compiler release, the type based optimizations are enabled by default.

3
  • Thanks Chad, found the issue with this help, see my additional answer :D
    – Hamish
    Commented Jan 3, 2013 at 23:55
  • 1
    source maps in chrome are fun too
    – John
    Commented Jan 4, 2013 at 2:00
  • I always forget about source maps - added to the answer. Commented Jan 4, 2013 at 13:52
1

With the help of Chad's answer, I found a bug where my working code like so:

a = [b, b = a][0]; // swap variable values

Was compiled to:

a = b;

It might be fixed in later versions, because tests with the online Closure compiler app don't demonstrate the same bug. Fixed it by not trying to be clever, and using a 3rd variable to hold the old value while swapping values.

2
  • I can't find the exact issue that would have fixed this, but I'll bring it up and make sure. If not, we'll definitely take a look at this. Commented Jan 4, 2013 at 13:50
  • Oh wow, thanks! I'll see if I can put together a replicating case for you, with version.
    – Hamish
    Commented Jan 5, 2013 at 8:43
0

A couple problems that I've seen with dojo 1.3 (pre-closure):

  • If you have a class property named class it needs to be quoted. So {class: "css"} needs to be written as {"class": "css"} this includes any widget fields.
  • Make sure you remove any debugger statements.

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