Yii 1.1: minifyclientscript

Minify and merge your CSS and JavaScript seamlessly via this ClientScript extension

This extension allows you to seamlessly minify and merge your CSS and JS files.


Tested on 1.1.9, should work on 1.1.*


20120507.1438 UTC+1

  • fixed bug where the wrong resources were checked, causing the js to rebuild every request comment#8052 crasx
  • fixed bug where POS_* was ignored; thanks Renka

20120330.1039 UTC+1

  • added LOCK_EX to file_put_contents() to prevent certain access issues

20120327.1112 UTC+1

  • fixed bug on Windows(?) hosts; comment#7505 Jesse
  • added return values to registerScript and registerCss; comment#7492 thyseus
  • fixed a bug where CSS wasn't loaded correctly


Extract the files to the corresponding directories, components/* into compontents/ and extensions/* into extensions/ then configure the config file as described below, now MinifyClientScript will do the rest. :) Please comment if you have comments/suggestions/questions.

Example main.php:

return array(

More on this extension

I have written a blog about minifying and merging CSS/JS, read more about it HERE.

Total 20 comments

#15310 report it
cubestyle at 2013/10/27 11:08am
Parameter Question

I have simply followed that setup description here and the minification works but the most of the JS code is missing so my app doesn't run well anymore. Beside the embedded coded that has been minified and rendered I have several files in header like

$cs ->registerScriptFile($baseUrl.'/js/json2.js')

Those are all missing and may have been replaced by 2 new generated JS file like this:

But definately is is just a little piece of my JS code inside...

Are there any parameters I could play around and try to fix it?

Maybe you have an idea what goes wrong..?

Btw, the minification could be improved by shortening also variables inside functions e.g. function(response){if(response!='') minified: function(e){if(e!='')

#12897 report it
shiv at 2013/04/19 04:10am

I think its slowing down my site. Without this < 1 sec after this 8 sec.

Thanks, Shiv

#12242 report it
Mohammadreza at 2013/03/09 06:49am
check for file isn't empty

on lines 146,198 and 202 also need ckeck on file size if equal zero byte,ignore publish those files

if(filesize($this->minifyPath . "/$filenames[$media]") > 0){
  $this->registerLinkTag("stylesheet", "text/css", Yii::app()->assetManager->getPublishedUrl($this->minifyPath) . "/$filenames[$media]", $media == "default" ? null : $media);
if ($position===self::POS_HEAD){
if(filesize($this->minifyPath . DIRECTORY_SEPARATOR . "$filenameCore.js") > 0)
  $this->registerScriptFile($publishedMinifyPath . "/$filenameCore.js", $position);
if(filesize($this->minifyPath . DIRECTORY_SEPARATOR . "$filename.js") > 0) {
  $this->registerScriptFile($publishedMinifyPath . "/$filename.js", $position);
#9854 report it
allain at 2012/09/15 02:11pm

I noticed that whenever the content of the compiled JavaScript is empty, there is still a script tag generated, causing an unnecessary http request.

#9112 report it
glyph at 2012/07/20 05:28pm
Problem minifying d3.js - unterminated regular expression

This portion of d3.js:

// Compute the angular scale factor: from value to radians.
    var k = ((typeof endAngle === "function"
        ? endAngle.apply(this, arguments)
        : endAngle) - startAngle)
        / d3.sum(values);

produces the following error:


Unterminated RegExp: '/ d3.sum(values); '


163 $this->output .= $this->a . $this->b; 164 $pattern = '/'; // in case needed for exception 165 while (true) { 166 $this->a = $this->get(); 167 $pattern .= $this->a; 168 if ($this->a === '/') { // end pattern 169 break; // while (true) 170 } elseif ($this->a === '\') { 171 $this->output .= $this->a; 172 $this->a = $this->get(); 173 $pattern .= $this->a; 174 } elseif (ord($this->a) <= self::ORD_LF) { 175 throw new JSMin_UnterminatedRegExpException( 176 'Unterminated RegExp: '. var_export($pattern, true)); 177 } 178 $this->output .= $this->a; 179 } 180 $this->b = $this->next(); 181 } 182 // end case ACTION_DELETE_A_B 183 } 184 } 185
186 protected function isRegexpLiteral() 187 {

#8533 report it
ksangers at 2012/06/11 03:26am
RE: Very slow

Koduc, did you make sure to run this test in production mode? and did you also build the cache beforehand?

#8449 report it
Koduc at 2012/06/05 02:46am
Very slow

This extension is VERY slow with twitter bootstrap. Page loaded without extension:

ab -c10 -n500 http://localhost/index.php

Requests per second: 15.56 [#/sec] (mean)

Page loaded with extension:

Requests per second: 0.76 [#/sec] (mean)

#8054 report it
ksangers at 2012/05/07 08:39am
Did another update

POS_* aren't ignored anymore, and the files should no longer rebuild at every request.

#8052 report it
crasx at 2012/05/06 07:26pm
Overhead problem

huge, huge problem in the component part of this:

Lines 156-157:

$compileCoreJs = YII_DEBUG || !file_exists($publishedMinifyPath . DIRECTORY_SEPARATOR . "/$filenameCore.js");
$compileJs = YII_DEBUG || !file_exists($publishedMinifyPath . DIRECTORY_SEPARATOR . "$filename.js");

Should be:

$compileCoreJs = YII_DEBUG || !file_exists($this->minifyPath. DIRECTORY_SEPARATOR . "$filenameCore.js");
$compileJs = YII_DEBUG || !file_exists($this->minifyPath. DIRECTORY_SEPARATOR . "$filename.js");

This was causing a 2 second time to first byte on my server because it generated the cached files every request.

#7976 report it
amiramir at 2012/04/30 01:10pm
i get PHP notice:Undefined index: default

its says the problem in components\MinifyClientScript.php line: 132

#7538 report it
ksangers at 2012/03/28 02:02pm
RE: jQuery is not defined

I can't seem to reproduce your error, but you could check whether the runtime/MinifyClientScript directory has been published properly, just look for the core-BIGHASHHERE.js file in the assets directory.

Extending CAssetManager could be a nice way to minify the code, but it would require you to recursively search the directory to find .css- and .js-files and it would not merge the files though (which MinifyClientScript also does). You could try and use JSMin and CssCompressor to extend CAssetManager.

#7519 report it
Jesse at 2012/03/27 06:45pm
jQuery is not defined


Thanks for your quick reply, the 404 errors are not coming through now; however, the error "jQuery is not defined" is still an issue.

I cleaned the www/assets and www/protected/runtime/MinifyClientScript directories. Also to confirm components/MinifyClientScript.php line 132 is your updated version.

Current error is:

jQuery is not defined

After running the application again: jquery exists in the runtime/MinifyClientScript under a hash file name. Anyone know a solution, something I'm missing?

What if you extended CAssetManager.php and replaced function publish(...) to pipe any .css or .js through minify ?

#7513 report it
ksangers at 2012/03/27 09:27am
RE: 404 and empty core files

Could you check components/MinifyClientScript.php line 132 (at the end of minifyCss())? It should be:

file_put_contents("$this->minifyPath/$filenames[$media]", CssCompressor::deflate($cssArray[$media]));

and not:

file_put_contents("$this->minifyPath/$filenames[$media]", CssCompressor::deflate($css));

Otherwise clearing the runtime and www/assets directories might help.

#7512 report it
fleuryc at 2012/03/27 08:23am
404 and empty core files


Same as Jesse :

"NetworkError: 404 CHttpException - http://myapp.local/assets/7b768164/core-a23c688da8235c331a4a2d9edc5422a6b411e98caf8e301725933f7f5c4767b2.js"

Plus, when I take a look in the file (wich actually exists), it is empty...


#7511 report it
ksangers at 2012/03/27 05:18am
Update 20120327.1112UTC+1

Thanks for the comments guys! I've updated the code and put it online. Let me know what you think of this version, especially what you don't like so I can fix it! :)

#7505 report it
Jesse at 2012/03/26 09:26pm
Uncaught ReferenceError: jQuery is not defined

Hi, great extension you've made here!

I keep receiving this error:

"Uncaught ReferenceError: jQuery is not defined | jquery.yiilistview.js:114"
"NetworkError: 404 Not Found - http://localhost/WWW/assets/e94214b1%5Cdefault-d2a448605e081d92dd6f50542f3648bcdde0a938ddef420baf0df4efbe329b52.css"
"NetworkError: 404 Not Found - http://localhost/WWW/assets/e94214b1%5C45982dae651db9bcf314be0ba414a5d0d9c7abf84c4c8d5367c16c370006b112.js"


Obviously the assets are not being found. Perhaps the "%5C" is the problem??

I'm wondering if anyone else had a similar issue and knows the solution?

#7492 report it
thyseus at 2012/03/26 04:21am
Nice, but a small addition

Just a small addition to this wonderful clientscript minimizer:

In order to make CClientScript chainable, please return the object:

public function registerScript() [ return parent::

public function registerCss() [ return parent::

(line 223/234/236)

#7125 report it
ksangers at 2012/02/25 06:11am
RE: Error with CSS

I have updated the file to do the callback differently, could you test this? If it still does not work I would like to know what PHP version you are using so I can fix this better.

Hope this works :)

#7123 report it
3logy at 2012/02/24 10:49pm
Error with CSS

Your extensions give me this error :

"preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'self::cssReplaceCallback', to be a valid callback"

Where is the problem here?

#7030 report it
fleuryc at 2012/02/20 04:47am
RE: RE: RE: RE: RE: RE: 404 and empty files

Ok, thx for the update...

Leave a comment

Please to leave your comment.

Create extension