jQuery compiled with Google Closure Compiler

2019-01-06 15:11发布

Has anyone compiled jQuery against Google's newly-released Closure compiler?

There has been reported huge savings in code size. I'm curious what the benefit is if jQuery was compiled with it.

10条回答
戒情不戒烟
2楼-- · 2019-01-06 15:31

I tried with jQuery jQuery 1.3.2

jQuery-1.3.2.min.js     57254 Bytes
jQuery closure compiler 49730 Bytes
-----------------------------------
Reduced by               7524 Bytes
Saved 13.31% off the original size
Saved 10.87% off the gzipped size

Gain of ~7KB

But it also reports 15 Warnings and I didn't test if it still works

查看更多
姐就是有狂的资本
3楼-- · 2019-01-06 15:31

I used Closure (with ADVANCED_OPTIMIZATIONS) to compress the javascript code for a single-page site that I host and saw a significant size savings over the YUI Compressor. So I started looking into shrinking jQuery for that site as well, since it certainly doesn't use the entier jQuery library.

I get warnings every time I run jQuery through the compiler, mostly pointing out portions of the code that are not used. Even still, the compiled code doesn't work. The primary issue I see is that things aren't being exported properly by the compiler. I was able to easily export functions from my own code by attaching them to the window object, but I have not yet been able to do this yet with jQuery.

The good news is that John Resig is already experimenting with Closure. I suspect that we'll see new releases of both that are compatible in the near future.

查看更多
beautiful°
4楼-- · 2019-01-06 15:33

John Resig reported a bug on recursive functions when he attempted to compile a nightly of jQuery 1.4, so there are a few kinks to be worked out. I wouldn't use a jQuery compiled with Closure Compiler without making sure it passes the jQuery testbed.

http://code.google.com/p/closure-compiler/issues/detail?id=1&can=1#c2

查看更多
迷人小祖宗
5楼-- · 2019-01-06 15:35

As of jQuery 1.4, Google's Closure Compiler is used as the default minification technique for jQuery releases. However, jQuery only uses the SIMPLE_OPTIMIZATIONS settings. The jQuery team has no plans to support ADVANCED_OPTIMIZATIONS.

查看更多
唯我独甜
6楼-- · 2019-01-06 15:36

jQuery is not compatible (yet) with the Closure Compiler in advanced mode. I agree it would be a very good thing to make it compatible, because its method-chaining syntax lends very readily to prototype virtualization for much improved execution speed.

In fact, among the popular JavaScript libraries (other than Closure Library, that is), only the Dojo Toolkit is compatible with Closure Advanced mode.

http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

查看更多
放荡不羁爱自由
7楼-- · 2019-01-06 15:37

With ADVANCED_OPTIMIZATIONS turned off:

  • jquery-1.3.2.min.js: 57254 bytes
  • jquery-1.3.2.closure.js 55346 bytes (-3.4%)
  • jquery-1.3.2.min.js.gz: 19680 bytes
  • jquery-1.3.2.closure.js.gz: 18666 bytes (-5.2%)

With ADVANCED_OPTIMIZATIONS turned on: it doesn't work unmodified. With ADVANCED_OPTIMIZATIONS turned on and everything I can find to export exported... it still doesn't quite work, and the code is already up to 53466 bytes again (and 18785 gzipped, which is more than the gzipped closured code without ADVANCED_OPTIMIZATIONS) so it doesn't look like a winning proposition.

查看更多
登录 后发表回答