2

It is well known that GWT compiles the Java source to an optimized Javascript. It will for example eliminate all unused functions. This is useful if you use a third party library such as gwtQuery.

I wonder if there is a way to let GWT also optimize the Javascript you embed in the HTML page with the <script> tag. For example instead of letting the user download the full JQuery Javascript library, only download the few functions that are used.

Vjeetje
  • 5,314
  • 5
  • 35
  • 57
  • I'm not sure, but from what I know about GWT, I'm gonna say no, it's not possible. But you could always test to see if jQuery (or any other functions you have) is needed before loading it, as described here: http://stackoverflow.com/questions/1014203/best-way-to-use-googles-hosted-jquery-but-fall-back-to-my-hosted-library-on-go – Churro Aug 28 '13 at 23:13
  • gwtquery is NOT a 3party javascript library, it is a full GWT library (in fact a rewrite of jquery api in java) so it does need any kind of optimization since gwt compiler will get rid of unused methods and will optimize all the code. Maybe you meant jquery (you can edit your question). If you import gwtquery you dont need jquery to use the api. Obiously you need jquery if you use jquery plugins because they are writen in js, since gwtquery plugins are writen in java. Anyway, the only way to optimize a bit jquery is copying the code into a jsni block, but it wont work. – Manolo Carrasco Moñino Aug 29 '13 at 09:42

1 Answers1

1

If you are talking about extra <script> tags you add to the page, the answer is no - how could it? You are adding those tags to the html page, and GWT has no way of knowing what they are going to be, or changing where they come from, at least not without rewriting the html file itself (and that isn't how GWT does its work).

If you are talking about putting JS into a JSNI method in your application, then the answer is 'sort of' or 'it depends'. If using GWT 2.4 or earlier, then only very limited optimizations are performed, such as interning strings and inlining very simple methods. If you are using GWT 2.5 or later and have the Closure Compiler option enabled, then Closure will modify all source in the compiled output, including JSNI methods. Note that this optimization may break some JS source if it isn't designed with Closure in mind.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • I doesn't seem too odd to add the js file somewhere so that GWT adds it to a compilation step. You could add all external js files to the compiled javascript code (from the java source files) and then let closure do it's thing. So in my perspective it's possible. It's just a matter of how to add that js. Maybe there is a way to add the external JS code via JSNI. But you are right it doesn't make sense to let GWT rewrite HTML pages. DO you agree it's possible, but not yet implemented? And can you use JSNI to add a complete external JS file and expect GWT to remove unused methods? – Vjeetje Aug 29 '13 at 08:56
  • It doesn't seem odd if you remember that GWT is a Java to JavaScript compiler. GWT should never rewrite the HTML page, and GWT should almost certainly never rewrite the JS in the module file (What if I load from another server and expect it to change? What if I want to use non-GWT capable JS like the `with` statement? And what if my code uses `eval` to perform reflection to access otherwise unreachable code?). Finally, as my answer said, JSNI could contain JS, but it will only be optimized (extra methods removed) *if* you enable closure. – Colin Alworth Aug 29 '13 at 13:21