Yii 1.1: extendedclientscript

Faster page loading by combining and compressing JS / CSS files
29 followers

With this extension you can transparently reduce page load times by combining and compressing the used Javascript and CSS files.

All parameters can be set in your main.php config. No need for altering your code, just set and forget.

Needs Yii 1.0.4/1.1.x or greater, and for compression JSMin and CSSTidy.

Resources

Documentation

Requirements

  • Yii 1.0.4/1.1.x or above

Installation

Since version 0.9 * Extract the zip file under 'proctected/extensions' * Add the following to your 'config/main.php':

// application components
    'components'=>array(
                 ....
        'clientScript'=>array(
            'class'=>'ext.ExtendedClientScript.ExtendedClientScript',
            'combineCss'=>true,
            'compressCss'=>true,
            'combineJs'=>true,
            'compressJs'=>true,
        ),

And you're set!

For more options see the comments in the file. To mention a few:

  • excludeFiles: array with files not to be combined/compressed. Handy for some rarely used JS/CSS files.
  • autoRefresh: If false, no check on filemtime() to speed up a bit more. Useful in production.

Change Log

August 30, 2011: 0.9.0

  • New version, using CssMin and JsMin (included). Several improvements of Sam included.

August 23, 2011: 0.4.1

  • Automatic exclusion of remote files (JS as well as CSS), so you can use your CDN of choice. Thanks Kir!
  • By popular demand: removal of DIRECTORY_SEPARATOR, changed to '/'

March 12, 2009

  • Additional test to prevent E_ALL errors.

March 11, 2009

  • Initial release.

Total 20 comments

#13117 report it
iEmpire at 2013/05/06 06:27am
pull request

@Rodrigo Coelho

I've published it on the github with your fix: https://github.com/kirs/extendedclientscript

#13095 report it
Rodrigo Coelho at 2013/05/04 04:55am
@Nathan - Re: Array to string conversion

In ExtendedClientScript.php, line 209, replace:

serialize($this->cssMinFilters . $this->cssMinPlugins)

With:

serialize($this->cssMinFilters) . serialize($this->cssMinPlugins)
#10709 report it
Nathan at 2012/11/18 04:15am
Array to string conversion

Just started, installed and set the config.

I got: Array to string conversion in [...]/ExtendedClientScript.php on line 209

#8316 report it
yugene at 2012/05/25 02:26am
include(JSMin.php) error

Hi, Thank you for the extension. Do you have an idea what can cause this error:

include(JSMin.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

?

It appears only if

'autoRefresh' => true,

For a quick fix I added

'import'=>array('ext.ExtendedClientScript.jsmin.JSMin' )

to config file.

Is it expected behavior?

#6199 report it
andser at 2011/12/19 08:07am
Enhancements

I propose you to enhance some functionality of excluding css and js files:

public function renderHead(&$output)
{
..........
  foreach ($this->cssFiles as $url => $media) {
     $file_arr = pathinfo($url);
     if (!empty($this->excludeCssFiles) && in_array($file_arr['basename'], $this->excludeCssFiles) || $this->isRemoteFile($url)) {
         // excluding files
     } else {
         $cssFiles[$media][$url] = $url;
     }
  }
.........
}

And renderJs function:

private function renderJs($output, $pos)
{
.......
    foreach ($jsFiles as &$fileName) {
    $file_arr = pathinfo($fileName);
    (!empty($this->excludeJsFiles) && in_array($file_arr['basename'], $this->excludeJsFiles) || $this->isRemoteFile($fileName)) AND $fileName = false;
.......
}

After this enhances I can do this in my config:

'clientScript' => array(
            'class' => 'application.extensions.ExtendedClientScript.ExtendedClientScript',
            'combineCss' => true,
            'compressCss' => true,
            'combineJs' => true,
            'compressJs' => true,
            'excludeJsFiles' => array('jquery.min.js', 'jquery-ui.min.js', 'jquery-ui.css'),
            'excludeCssFiles' => array('jquery-ui.css'),
        ),
#5511 report it
Stamm at 2011/10/17 04:21pm
Another server host

Could I set another server url for generated files like this: http://www.yiiframework.com/doc/api/1.1/CAssetManager#basePath-detail ?

#5376 report it
Wenceslaus D&#39;silva at 2011/10/08 02:54am
Thank u

Thank you Nafania it worked like a charm....great work...cheers

#5371 report it
Wenceslaus D&#39;silva at 2011/10/07 08:40am
thank you Nafania

Thank u Nafania I shall check this out Tom and let u know how it goes. Cheers

#5367 report it
Nafania at 2011/10/07 05:34am
Re: Facing a problem

deadmantfa: i posted patch for it at google code, here it is http://code.google.com/p/extendedclientscript/issues/detail?id=1

#5060 report it
Wenceslaus D&#39;silva at 2011/09/12 12:46am
Facing a problem

Hi first of all thank you for this extension i have been using it for quite sometime now....just one problem...now whenever i use a widget from yii the images do not load when the compression is on...if i take the compression off it works fine...i checked the css which is calling the images and they are fine but once the compression happens the css file gets moved and so the path in the css does not work... how do i make it work..kindly help

#3449 report it
sAe at 2011/04/13 05:51pm
DIRECTORY_SEPARATOR not needed

DIRECTORY_SEPARATOR is not needed. As long as you use forward slashes (/) everywhere everything will work as intended.

All *nix systems use forward slashes (/) while windows use back slashes (). The good news is that Windows converts forward slashes (/) into back slashes () hence why you should only use forward slashes (/). It's been like this for ages and that's how i do it.

#2864 report it
rickywu at 2011/02/18 03:23am
DIRECTORY_SEPARATOR BUG AND RECOMMENDED

In the 221 line, you can not use DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR used in the URL is not correct, I agree this is a bug. In addition, can you turn the associated documents to be re-released, such as the pictures to use relative paths inside css file? I think this is necessary, css files are mapped to another file, the directory path has changed, the picture of the paths must be properly re-mapping.This is also can be done.

#682 report it
p_m at 2010/03/19 12:14pm
CSS file handling need to be improved

The option "excludeFiles" does only work for JS files. CSS files getting combined/compressed even if listed in the option "excludeFiles".

Another improvement would be to exclude all files where the path points to a CDN.

#1001 report it
mintao at 2010/01/12 09:12am
csstidy too old

seems that csstidy is too old for php 5.3 :( "Non-static method csstidy::is_important() should not be called statically, assuming $this from incompatible context" .. don't want to start debugging as it ends up with 6 hours and no result :) Trying to find a good alternative...

#1002 report it
mintao at 2010/01/12 08:47am
DIRECTORY_SEPARATOR

DIRECTORY_SEPARATOR is - as the name says a directory separator, not a url separator. So it works well with linux and mac because the folder separator in urls matches the directory separator.

As windows uses the backslash \ creating a url with the constant DIRECTORY_SEPARATOR can't work anymore on windows virtual hosts.

So there definitely has to be a "/" instead of the constant DIRECTORY_SEPARATOR.

#1523 report it
samdark at 2009/07/06 03:14pm
Update
#1525 report it
samdark at 2009/07/06 09:44am
DIRECTORY_SEPARATOR

Same problem with DIRECTORY_SEPARATOR.

#1555 report it
Ismael at 2009/06/24 11:23pm
Great component

I have the same issue Boris did.

For me in line 221 does not make sense use DIRECTORY_SEPARATOR because this a URL separator and not a file separator.

You said: "It is also used in several other places in this extension".

This is just coincidence. In my Windows XP DIRECTORY_SEPARATOR is '\' and in your tests maybe is LINUX. And linux maybe is '/' separator, just like URL separator which is '/'.

#1839 report it
Maxximus at 2009/03/12 07:06pm
DIRECTORY_SEPARATOR

Thanks! DIRECTORY_SEPARATOR is a predefined PHP constant, so that can't (shoudn't) be the real issue. It is also used in several other places in this extension. BTW: Might be better to use the forum thread for this: [url=http://www.yiiframework.com/forum/index.php/topic,1091.0.html]forum discussion[/url]

#1840 report it
Boris at 2009/03/12 03:23pm
Great job, is this a bug?

Hi,

it works fine for me. Great idea, no need to reinvent the wheel now ;-)

But can you please check the line 221 in the current revision of ExtendedClientScript

I think DIRECTORY_SEPARATOR is wrong, i did not work for me, it worked as i changed it to "/". How come? Idea?

Otherwise i got errors that resource could not be found...

Thanks, Boris

Leave a comment

Please to leave your comment.

Create extension