compressor

Assets compressor for ClientScript
7 followers

CompressorClientScript

Assets minify and compressor for JS/CSS files using YUI compressor

https://github.com/gusnips/compressor

Why another?

This supports files from external sources. In my case, I use a CDN to serve the files and none of the existing extension were good for my case.

How to use

Extract the files to protected/extension/compressor (or whatever path you want)

Then in your config define the class of clientScript component to use CompressorClientScript

return array(
//...
'components'=>array(
    'class'=>'ext.compressor.CompressorClientScript',//use whatever location you put the extension
    //the following options are optional and the values defined in this example are the default ones
    'compress'=>true,//wheter to minify and compress the files. Defaults to true
    //see YUICompressor class for more details
    'compressorOptions'=>array(
        //Insert a line breaks after '}' characters for css files or 
        //Insert a line break after the specified column number for js files
        'linebreak'=>false,
        //Display informational messages and warnings. (useful for cleaning up your JS)
        'verbose'=>false,
        //Minify only, no symbol obfuscation. Valid for js files
        'nomunge'=>false,
        //Preserve unnecessary semicolons (such as right before a '}'). Valid for js files
        'semi'=>false,
        //Disable all the built-in micro optimizations. Valid for js files
        'nooptimize'=>false,
        //path to the java binary
        //you can use, for example, 'C:\Program Files (x86)\Java\jre7\bin\java.exe' if you are windows
        'javaBin'=>'java',
    ),
),
);

How to clear the cache?

Just clean up protected/runtime/compressor folder and it will recreate the files

How does it work?

It uses file_get_contents to get the files, so it can be either on your machine or load from external sources

It creates a compressed file in protected/runtime/compress for each page and then publishes it using assetsManager component

For css files, it rewrites the css url's

Uses a modified version of https://github.com/gpbmike/PHP-YUI-Compressor for compression adapted to Yii

Uses a modified version of https://github.com/mrclay/minify/ to rewrite css urls adapted to Yii

Total 8 comments

#15374 report it
Gustavo at 2013/11/02 08:20pm
@compugator

Firstly, thanks for trying it out!

I can't reproduce the error here but googling it a bit I find out that you are most probably running an older version of java

http://stackoverflow.com/questions/9014992/jar-file-not-running-in-linux-terminal

Check your java version and try to update to a newer one.

Cheers, Gustavo

#15373 report it
compugator at 2013/11/02 08:12pm
Cannot compress files

I am getting the following error:

Exception

Failed to generate compressed file. Error: "Exception in thread "main" java.lang.NullPointerException at java.lang.VMClassLoader.defineClass(libgcj.so.10) at java.lang.ClassLoader.defineClass(libgcj.so.10) at java.security.SecureClassLoader.defineClass(libgcj.so.10) at java.net.URLClassLoader.findClass(libgcj.so.10) at java.lang.ClassLoader.loadClass(libgcj.so.10) at java.lang.ClassLoader.loadClass(libgcj.so.10) at gnu.java.lang.MainThread.run(libgcj.so.10)"

I have the 'javaBin'=>'java' set and I am running on a linux server so I am not sure what I am doing wrong.

#14684 report it
Gustavo at 2013/09/03 09:31am
Thanks

I misstyped

It's fixed now in download file.

Thanks

#14683 report it
onikandish at 2013/09/03 09:24am
@Gustavo

hi it works with a little edit. in CompressorClientScript.php file edit $fille to $file. thanks

#14681 report it
Gustavo at 2013/09/03 06:04am
@onikandish

Sorry about that.

I fixed only for css files. Forgot to add to js files. It should work now.

Thanks for using it!

Cheers, Gustavo

#14677 report it
onikandish at 2013/09/03 02:47am
@Gustavo

i get the same error again :(

#14666 report it
Gustavo at 2013/09/02 09:50am
@onikandish

There was a problem when using relative urls

Fixed now. Please let me know if you encounter any other errors

#14647 report it
onikandish at 2013/09/01 01:12am
hi

hi @Gustavo thanks for this. you should edit config file and add clientScript component name. ('clientScript'=>array(...),) after that i get this error: file_get_contents(/testcompressor/assets/17ae64b6/jquery.js) [function.file-get-contents]: failed to open stream: No such file or directory but jquery file is there.what is the problem?

Leave a comment

Please to leave your comment.

Create extension