javascriptgwtgwt-compiler

Does GWT optimize embedded javascript?


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.


Solution

  • 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.