最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to minify different javascript files at runtime using Java - Stack Overflow

programmeradmin3浏览0评论

I'm trying to build (or find an existing one I can use) a web filter that will press 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 pression/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 binations 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

I'm trying to build (or find an existing one I can use) a web filter that will press 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 pression/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 binations 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

Share Improve this question edited Mar 29, 2012 at 20:17 hitautodestruct 20.9k14 gold badges75 silver badges95 bronze badges asked Apr 30, 2011 at 7:38 Programming GuyProgramming Guy 7,47111 gold badges52 silver badges61 bronze badges 5
  • Turns out ver 2.4.2 of yui has some issues. Dropped back to 2.3.6 and it started working straight away... – Programming Guy Commented Apr 30, 2011 at 8:24
  • Why not press each js first, then concat those you need at run time? – WaiLam 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 pressing at build time. – Programming Guy Commented Apr 30, 2011 at 9:22
  • Just for info -- Closure has a Java API as well. ACtually it is written in Java. – Stephen Chung Commented Apr 30, 2011 at 10:05
  • Try wro4j: alexo.github./wro4j. This can help you to choose from dozen of pressors and simplifies the way resources are managed. – Alex Objelean Commented Sep 9, 2011 at 18:21
Add a ment  | 

2 Answers 2

Reset to default 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 bine 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 bined and minified output have changed. Basically instead of "piling" them at build time, we handle each bination once at runtime.

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, pression on the fly is horribly expensive and definitely not worth doing for every .js file served up)

发布评论

评论列表(0)

  1. 暂无评论