Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Minify JavaScript and CSS with the help of Jetty?

Hi Alex,


> Am 17.07.2016 um 20:18 schrieb Alexander Farber <alexander.farber@xxxxxxxxx>:
> 
> Good evening,
> 
> is it possible to minify JS and CSS files served by org.eclipse.jetty.servlet.DefaultServlet?

You mean on the fly compression of JS and CSS? I think this is out of scope for the DefaultServlet. This is not a task for Jetty.

In Java there is only one „native“ Solution for JS / CSS compression: YUICompressor. But it is a little bit out of date, and the CSS compressor sometimes breaks some CSS rules in subtile ways. (i.e. border: none gets a border:0 and so on…)

For state of the art JS compression you need uglifyjs2 - you can run it on Rhino and I think even on Nashorn. But it is *way* to slow for on the fly compression. Uglifyjs2 is already slow when running on node.js. But its even slower in the JVM…

I do JS on the fly compression in a servlet and still use the YUICompressor JS Compressor, as it works well enough and is really fast. Uglifyjs2 can save some more kB but is to slow for „interactive“ JS compression. Some JS scripts, which rarely change, are precompressed using uglifyjs2 - mostly JQuery and such 3rdparty stuff.

In our project we migrated away from pure CSS to SASS. You can run the original (ruby) sass in jruby without problems. But it is also very slow… But there is sassc to rescue here. I´ve build a static sassc binary for Linux and Mac OS X, which I embedded in my java project and just extract to a temporary folder to convert sass->css on the fly. This is very fast and even gives a good compressed/minified CSS.

But this all only makes sense if your templates / javascript are maintained / changed separately from your java project. I.e. if you build the server and another team builds the frontend templates, which are updated independently from the server.

Otherwise you should just integrate the JS/CSS compression/minifying into your (maven/ant/gradle/ivy/…) build script.

cu,
  Emmy

> 
> I've searched on Google and Github, couldn't find any examples.
> 
> Regards
> Alex
> 
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/jetty-users

Mit freundlichen Grüßen aus Augsburg

Emmeran Seehuber
Dipl. Inf. (FH)
Schrannenstraße 8
86150 Augsburg
USt-IdNr.: DE266070804



Back to the top