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
20120330.1039 UTC+1
20120327.1112 UTC+1
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( ... 'components'=>array( ... 'clientScript'=>array( 'class'=>'application.components.MinifyClientScript', ), ... ), ... );
I have written a blog about minifying and merging CSS/JS, read more about it HERE.
Total 20 comments
I think its slowing down my site. Without this < 1 sec after this 8 sec.
Thanks, Shiv
on lines 146,198 and 202 also need ckeck on file size if equal zero byte,ignore publish those files
I noticed that whenever the content of the compiled JavaScript is empty, there is still a script tag generated, causing an unnecessary http request.
This portion of d3.js:
produces the following error:
JSMin_UnterminatedRegExpException
Unterminated RegExp: '/ d3.sum(values); '
/var/www/html/mysite/pub/protected/extensions/jsmin/JSMin.php(175)
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 {
Koduc, did you make sure to run this test in production mode? and did you also build the cache beforehand?
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)
POS_* aren't ignored anymore, and the files should no longer rebuild at every request.
huge, huge problem in the component part of this:
Lines 156-157:
Should be:
This was causing a 2 second time to first byte on my server because it generated the cached files every request.
its says the problem in components\MinifyClientScript.php line: 132
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.
ksangers,
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:
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 ?
Could you check components/MinifyClientScript.php line 132 (at the end of minifyCss())? It should be:
and not:
Otherwise clearing the runtime and www/assets directories might help.
Hi!
Same as Jesse :
Plus, when I take a look in the file (wich actually exists), it is empty...
Cheers!
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! :)
Hi, great extension you've made here!
I keep receiving this error:
etc
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?
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)
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 :)
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?
Ok, thx for the update...
Yeah I noticed this, and fixed this. You got the crappy version really quick! (<5mins of me posting it)
I had the strpos(haystack, needle) wrong, I've already updated it. Sorry for the inconvenience.
Leave a comment
Please login to leave your comment.