views:

191

answers:

4

I'm working on a one-page site that is incorporating about a dozen .js files and and maybe a half dozen .css files.

Most of the .js files are jQuery plugins. Aside from a base css file, the CSS files are for the corresponding jQuery plugins.

YUI Compressor seems to be a favorite for compressing CSS and JS files. However, it only compresses individual files.

I'd also like to combine my files and (ideally) end up with one .js and one .css file (both compressed).

Are there any preferred tools out there that allow you to automate the combining of the .js and .css files into one file so it can then be run through YUI compressor?

+1  A: 

You might want to check out sprockets (http://www.getsprockets.com/).

jonnii
looks good! Alas, it's Ruby, so may not fit into our workflow, but good info.
DA
+1  A: 

I was asking this question only the other day. After tiring of scouring the web, I came up with this hackish (windows batch file) solution.

@echo off
set TUNA_ROOT=C:\path\to\webroot
set YUI_COMPRESSOR_PATH=C:\path\to\yuicompressor-2.4.2\build
set TEMP_JS_FILE=%TUNA_ROOT%\scripts\all_scripts_temp.js
set OUTPUT_JS_FILE=%TUNA_ROOT%\scripts\tuna_min.js
if exist "%TEMP_JS_FILE%" del "%TEMP_JS_FILE%"
if exist "%OUTPUT_JS_FILE%" del "%OUTPUT_JS_FILE%"
type "%TUNA_ROOT%\Scripts\MicrosoftAjax.js" >> "%TEMP_JS_FILE%"
echo. >> "%TEMP_JS_FILE%"
echo. >> "%TEMP_JS_FILE%"
type "%TUNA_ROOT%\Scripts\MicrosoftMvcAjax.js" >> "%TEMP_JS_FILE%"
echo. >> "%TEMP_JS_FILE%"
echo. >> "%TEMP_JS_FILE%"
rem ...and so on...
java -jar "%YUI_COMPRESSOR_PATH%\yuicompressor-2.4.2.jar" -v --charset utf-8 -o "%OUTPUT_JS_FILE%" "%TEMP_JS_FILE%"
if exist "%TEMP_JS_FILE%" del "%TEMP_JS_FILE%"

but I'd really love it if there were a more automated way of doing things.

spender
Thanks for sharing that! I'll play with that.
DA
+2  A: 

If you know a little, php, I have found this to be the best way: http://www.thedanglybits.com/2007/06/21/minify-your-external-javascript-and-css-with-php/

Ramblingwood
good option. I will explore! I'm really looking for a desktop solution, though, given that I work in place that's using all sorts of different servers. It'd best if we can process it on our desktop prior to shipping off to the servers.
DA
If you run a local Apache + PHP server it would work.I may be able to compile it into an exe. You use Windows, right?
Ramblingwood
Ok. Download this and extract it: http://ramblingwood.com/sandbox/js-minifier/js-minifier.zipJust drag and drop .js files onto it (you can do multiple) and it will make a file called total.js in the same directory as the orginal files that has them all compressed in that file.It will append to an already existing total.js file if it exists.
Ramblingwood
Rambling...that's GREAT! Alas, the ZIP appears to be corrupt. But it's a great idea. Are you going to share it publicly (I mean, beyond here?) I think there's definitely a demand for a simple app like that.
DA
I compressed the zip using 7z at Ultra compression. I forgot that Windows default extractor doesn't like that. I fixed that and reuploaded it.I do have plans on improving the compression and supporting CSS and ordering a few other features.
Ramblingwood
A: 

I ended up stumbling upon this option:

http://johannburkard.de/blog/programming/javascript/automate-javascript-compression-with-yui-compressor-and-packer.html

It's a batch file that combines local versions of concatenation, YUI Compressor and Dean Edward's Packer.

In the end, though, I couldn't get Packer to work locally. It kept corrupting my .js.

So, I skipped that part and then ran my YUI Compressed code through the online Packer and only saw a 1% further increase in compression, so just omitted the Packer stage.

In the end, my solution used the above linked instructions with slightly modified batch file:

type ..\js-in* > jb.js java -jar ..\yui\build\yuicompressor-2.4.2.jar jb.js -o jb-yui.js

Thanks for all the other (valid) solutions as well. Lots of good info!

DA