GWT减少编译的JavaScript大小 [英] GWT reduce compiled javascript size

查看:78
本文介绍了GWT减少编译的JavaScript大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现编译过的JavaScript的大小比我预期的要快。将几行Java代码添加到我的项目中可以将脚本大小增加几个Kbs。



目前我编译的项目重量为1Mb。除了MVP(Activities& Places),测试(JUnit)和日志记录之外,我没有使用任何外部库。

我想知道是否有任何编码实践/建议可以使编译脚本尽可能小。我不是指代码分割,而是指编码技术或模式,它们可以使编译后的JavaScript更小。



非常感谢
<解决方案GWT使用按需付费的设计理念,由于不允许使用反射,因此编译器可以静态验证(以逐个方法的方式进行验证)方法的基础),一段代码是可达,并消除那些没有。例如,如果您从未在ArrayList上使用remove()方法,那么该代码不会包含在生成的JavaScript中。



如果您看到几千字节的跳转只增加了几行,这可能意味着你已经引入了一种你尚未使用的新类型(可能还有一种依赖于其他新类型)的使用。这也可能意味着你已经做出了改变,将这种新的类型通过网络发送回服务器,在这种情况下,GWT生成器必须包含用于封送该类型的JavaScript以及可通过其进行访问的任何新类型a-a和is-a引用。

因此,如果是我,我会从那里开始:当你发现两行变化时, -kilobyte增加,首先查看类型并询问它是否是之前使用的类型,以及是否通过线路发送新类型,以及该类型是否也取决于其他类型。 / p>

最后一个想法是:在Ray Ryan 2009年的谷歌I / O发布会上,他提到了他从GWT编译团队获得的迷信,他们建议不要使用泛型(我在这里没有谈到Java泛型,而是超类型)作为RPC参数&返回值。特别是,不要让你的RPC调用接收或返回一个Map,而是让它接受或返回一个HashMap。我们相信GWT生成器可以缩小它在编译时创建的序列化代码量(因为它可以避免为TreeMap生成序列化代码)。

我希望这有助于。


I have found that the size of the compiled JavaScript grows faster than I had expected. Adding a few lines of Java code to my project can increase the script size in several Kbs.

At the moment my compiled project weights 1Mb. I'm not using any external libraries except for those for MVP (Activities & Places) , testing (JUnit) and logging.

I would like to know if there are any coding practices/recommendations to keep the compiled script as small as possible. I'm not refering to code splitting, but to coding techniques or patterns that can make the compiled JavaScript effectively smaller.

Many thanks

解决方案

GWT uses a "pay as you go" design philosophy, and since you're not allowed to use reflection the compiler can statically prove (on a method-by-method basis) that a section of code is "reachable", and eliminate those that are not. For example, if you never use the remove() method on ArrayList, then that code does not get included in the resulting JavaScript.

If you are seeing several kilobyte jumps with the addition of just a few lines, it probably means that you've introduced the use of a new type (and possibly one that depends on other new types) that you had not yet been using. It might also mean that you've made a change to send this new type "over the wire" back to the server, in which case a GWT generator had to include JavaScript for marshaling that type, and any new types that are reachable via its "has-a" and "is-a" references.

So if it were me, I would begin there: when you catch a 2-line change making a multi-kilobyte increase, start by looking at the types and asking whether it is a type that you have used before, and whether you're sending a new type over the wire, and whether that type also depends on other types under the hood.

One final thought: in Ray Ryan's 2009 presentation at Google I/O he mentioned a superstition that he had picked up from the GWT compiler team, where they recommended against using generic types (I'm not speaking of Java Generics here, but rather supertypes) as RPC arguments & return values. In particular, instead of having your RPC call take or return a Map, have it take or return a HashMap instead. The belief is that the GWT generator can then narrow the amount of serialization code that it has to create at compile time (because it could, for example, refrain from generating serialization code for a TreeMap).

I hope this helps.

这篇关于GWT减少编译的JavaScript大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆