0

I wanted to use Google Closure Compiler to to minimize javascript, but in minimized version it renames function names. Is this the intended behavior? How to use this in that situation ?

lib.js

function myfun() {
   $('#test').hide();
}

lib.min.js

function myfun$$module$main(){$("#test").hide()}

view

<a href="#" onclick="myfun()">no longer works</a>
marioosh
  • 27,328
  • 49
  • 143
  • 192
  • Does this help you? https://developers.google.com/closure/compiler/docs/api-tutorial3?hl=en#export – Carsten Jan 24 '13 at 08:25
  • I've read that... but i think can't use within play framework (how to set compiler options?)... maybe i'm wrong. I have not found the way how to do it... – marioosh Jan 24 '13 at 08:33

2 Answers2

1

You should assign the event handlers to the HTML elements within JavaScript so during the compilation process Google Closure can rename these functions as well. You can also use a simpler version of the optimization (WHITESPACE_ONLY) that does not rename functions or expression.

Assign an ID to the HTML element: <a href="#" id="link">does this work?</a>.

And in your JavaScript, assign the event:

var link = document.getElementById("link");
var myFun = function () {
    alert("hello world");
};
link.onclick = myFun;

Hope this helps!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • event handlers: Yes, You right, but this is not always possible or more complex than simple `onclick="myfunc()"`. How to use another Google Closure optimization within Play2 ? – marioosh Jan 24 '13 at 08:52
  • It is usually suggested to use HTML to provide the structure of the page and not the functionality. So I would avoid assigning event handlers within HTML tags as much as possible. – Zorayr Jan 24 '13 at 08:55
  • Yes you're right, but if you can do something easier, why not do it? – marioosh Jan 24 '13 at 09:06
  • Well, easier approach is not always the best. It's always a trade-off, and in this situation, you will produce better quality code and also be able to run it through Google Closure and optimize it. – Zorayr Jan 24 '13 at 09:08
1

ok, a few things you can do there, you could go:

1) window['functionName'] = functionName which will preserve it or

2) link in the closure library and use goog.exportSymbol

lennel
  • 646
  • 3
  • 10
  • `window['functionName'] = functionName` works! I did not notice this before. Thank You. – marioosh Jan 24 '13 at 10:06
  • another way is to declare an extern and pass that into the compiler, but externs should not be used in this way. effectively any string literal like this['jjjj'] will stop renaming – lennel Jan 24 '13 at 12:56