How to automate JavaScript files compression with YUI Compressor?

风格不统一 提交于 2019-11-27 11:58:11

问题


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? :)


回答1:


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"



回答2:


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




回答3:


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




回答4:


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

%-min.js: %.js
    ${java} -jar ${compressor} $< -o ${<:.js=-min.js}



回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/227288/how-to-automate-javascript-files-compression-with-yui-compressor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!