If you mean "compress" as in "maintain the code in exactly the same format but push it down the pipe compressed", then enable gzip compression as per the instructions for your server, which you haven't specified in the tags/question.
If you mean "compress" as in "minify the code", then there are options that you can try, like including a minification module/process in the pipeline. I've, very recently, created a solution in ASP.net that uses the Microsoft Ajax Minifier 4.0 to minify javascript files requested, on the fly basically by adding script tags to the page with a src tag that is similar to minifier.ashx?source=my/javascript/file/and/path/here.js and using the Minifier class in AjaxMin.dll to minify the code on demand. This has several advantages over pre-minification:
- You can pass a flag into your live site, via a cookie, a querystring value, a given username, to disable minification which makes debugging that little bit easier if you need to resolve the issue "on live"
- You can make emergency changes to your live code without having to change your unminified code, minify it, upload it (obviously not something you should do regularly, but it's nice that the ability is there, if needs be).
- There's one less set of build artifacts (the minified js files) to worry about keeping track of, source-controlling, etc,..
Dis-advantages:
- The minification adds some overhead to script requests. This can be mitigated by either caching the minification result in memory or on disk
- I'm not sure if the license for AjaxMin.dll permits this kind of use
- If you're using Visual Studio / another tool that provides intellisense for script, it may not now be able to give you that from your script tags