13

YUI Compressor does not accept wildcard parameters, so I cannot run it like this:

C:>java -jar yuicompressor.jar *.js

But I have over 500 files and would rather not have to create a batch file like this:

C:>java -jar yuicompressor.jar file1.js -o deploy\file1.js
C:>java -jar yuicompressor.jar file2.js -o deploy\file2.js
...
C:>java -jar yuicompressor.jar file500.js -o deploy\file500.js

And of course my file names are not in such uniform way.

Is there way to automate this without writing any code? :)

z-boss
  • 17,111
  • 12
  • 49
  • 81
  • 1
    YUICompressor supports wildcards starting from version 2.4.4. See below for download links. – hazerd May 03 '12 at 09:11

6 Answers6

12

I might go for a makefile (I think it would probably be more maintainable long term), but if you want a quick-n-dirty Windows batch command something like the following should work:

for %%a in (*.js) do @java -jar yuicompressor.jar "%%a" -o "deploy\%%a"
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • It works good if there are no spaces in the file name. Thanks! – z-boss Oct 22 '08 at 20:26
  • You should be able to throw quotes in there - I'll edit that in. – Michael Burr Oct 22 '08 at 20:33
  • Yep, that did it! The last thing I need to overcome is to be able to run this batch file from folder A, have all .js files in folder B, and put output files into folder C. – z-boss Oct 22 '08 at 21:08
  • I feared that this might wander down the "Advanced Batch Scripting Hell Hole"... You can have positional parameters to batch files using %1 %2 %3, etc. However, this is when you start running into **HUGE** problems with trying to combine and quote entities that might have spaces. Insanity follows. – Michael Burr Oct 22 '08 at 22:06
  • Thanks anyway. I can always run my batch from the folder where all the .js files are. So, it's not critical. – z-boss Oct 23 '08 at 01:03
  • Running this twice would cause all files that are already minified be minified again, with filenames resulting in .min.min.min.js and so on. Can you maybe avoid that by only minifying files that are not already minified? – Diego Mar 29 '11 at 19:54
5

If you are geared towards Java, you can also use Ant for conversion. I've found a blog entry about an Ant Taks for the YUI Compressor. Disclaimer: Never tried it - sorry

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Another ant task can be found at https://bitbucket.org/m6_russell_francis/yui-compressor-ant-task I use it in several projects to automate the compression of js/css files. As the author I may be slightly biased. – Russ Dec 13 '12 at 17:11
5

YUI compressor now supports wildcards, starting from version 2.4.4. You can get the latest version here or from YUI Git Hub.

Community
  • 1
  • 1
hazerd
  • 592
  • 4
  • 6
4

I should mention that using GNU Make, I have the following rule:

%-min.js: %.js
    ${java} -jar ${compressor} $< -o ${<:.js=-min.js}
foxxtrot
  • 11,214
  • 4
  • 27
  • 27
2

And for unix or cygwin you can use xargs or something like:

ls -1 *.js | awk '{printf("java -jar yuicompressor.jar %s -o deploy/%s",$1,$1)}'

Pipe that to /bin/sh when you're happy with the command line to execute it.

Niniki
  • 812
  • 4
  • 6
1

You'll need to use some sort of a script to get a list of all the .js files, and then runs the YUI Compressor on all of them. On the windows command prompt, something like this should work:

FOR %f IN (*.js) DO java -jar yuicompressor.jar %f -o deploy\%f
foxxtrot
  • 11,214
  • 4
  • 27
  • 27