9

I'm trying to build (or find an existing one I can use) a web filter that will compress a JavaScript file at runtime. I've tried building one based on YUICompressor, but I'm getting weird errors out of it when I try and pass a String based source into it instead of an actual file.

Now I'm expecting to get bombarded with responses like 'Real time compression/minification is a bad idea' but there is a reason I'm not wanting to do it at build time.

I've got a JavaScript web application that lazy loads it's JavaScript. It will only load what it actually needs. The JavaScript files can specify dependencies and I already have a filter that will concatenate the requested files and any dependencies not already loaded into a single response. This means that there is a large number of different combinations in the JavaScript that will be sent to the user which makes trying to build all the bundles at build time impractical.

So to restate. Ideally I'm looking for an existing real time javascript filter I can just plug into my app.

If one doesn't exist I'm looking for tips on what I can use as building blocks. YUICompressor hasn't quite got me there and GoogleClosure seems to only be a web API.

Cheers, Peter

5
  • Turns out ver 2.4.2 of yui has some issues. Dropped back to 2.3.6 and it started working straight away... Commented Apr 30, 2011 at 8:24
  • Why not compress each js first, then concat those you need at run time? Commented Apr 30, 2011 at 8:36
  • So it's definitely possible to do it and YUICompressor can be used. Demian and WaiLim have correctly pointed out that minifying before I concatenate will do the job. I just wasn't in the right head space to think of it. I'll be compressing at build time. Commented Apr 30, 2011 at 9:22
  • Just for info -- Closure has a Java API as well. ACtually it is written in Java. Commented Apr 30, 2011 at 10:05
  • Try wro4j: alexo.github.com/wro4j. This can help you to choose from dozen of compressors and simplifies the way resources are managed. Commented Sep 9, 2011 at 18:21

2 Answers 2

2

Take a look at The JavaScript Minifier from Douglas Crockford. The source is here: JSMin.java. It's not a filter and only contains the code to minify. We've made it into a filter where we combine and minify JavaScript on the fly as well. It works well, especially if you have browsers and a CDN cache results.

Update: I left out that we cache them on the server too. They're only regenerated if any of the assets required to make the combined and minified output have changed. Basically instead of "compiling" them at build time, we handle each combination once at runtime.

Sign up to request clarification or add additional context in comments.

3 Comments

Demian is making some sense. I'm struggling to keep a justification for trying to do it realtime. Why did you choose to go that way rather than compressing at build time?
I updated with clarification as to how we cache on the server side too. We generate them on the fly only as needed as opposed to worrying about it being a build step. It makes sense in many cases to just do it at build time, our was just easier this way due to many other complexities. You did ask for a minification solution at runtime in Java, I'm just saying it can work :)
JSMin only normalizes whitespace it doesnt actually do more smarts like replacing long symbols with shorter one etc.
0

I already have a filter that will concatenate the requested files and any dependencies not already loaded into a single response

Sooo.. Why not just minify those prior to loading/concatenating them?

(And yes, compression on the fly is horribly expensive and definitely not worth doing for every .js file served up)

1 Comment

There's a certain logic to what you're saying. I do like the idea of being able to easily debug released code. I can achieve that by having compressed and uncompressed files side by side though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.