Yii 1.1: minifyclientscript

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

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

Requirements

Tested on 1.1.9, should work on 1.1.*

Changelog

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

Usage

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',
    ),
    ...
  ),
  ...
);

More on this extension

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

Total 8 comments

#18053 report it
KonApaz at 2014/09/02 07:03am
Generate in each request

The extension seems to regenerate css and js in every request.

Is there way to improve it ?

#18051 report it
KonApaz at 2014/09/02 06:12am
issue homeUrl - baseUrl

Hi

In my case the extension does not work (I use Yii ver 1.1.15)

I replace Yii::app()->homeUrl with Yii::app()->baseUrl in MinifyClientScript.php and it works!

#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
Slow?

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
Optimization

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

#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)

Leave a comment

Please to leave your comment.

Create extension