Dynamic Res ¶
Compress CSS or JS files in only one file to increase performance and decrease request of many files. This extension (version > 1.3) use CssMin and JavaScriptPacker to compress.
Requirements ¶
assetManager (native)
Usage ¶
- Dowload the extension and put correctly in the extension folder
- Add the following lines in your configuration file:
main.php
'components'=>array(
...
'dynamicRes'=>array(
'class' => 'application.extensions.DynamicRes.DynamicRes',
'urlConfig' => array( // Its fix Css, and convert Url to RealName
'baseUrl' => '/', // Url of your Site (ending with /), modify it if you use subdomain
'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This)
)
),
...
In your layouts puts ~~~ ...
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/style.css'); ?>
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/reset.css'); ?>
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/common.css'); ?>
...
Warning
Warning: This is a beta version
Bugs
Hi. You never change value of $this->saved, so I can call saveScheme() function multiple times without warning. I suppose you should add this:
$this->saved = true;
anywhere between lines 110 and 117. Besides that this script doesn't work on Windows, probably you should change urlToPath() function.
Bug Fixed
Thanks, the bug was fixed
Another bug
Hello again. :) It seems that you have one more bug (or more, we will see :) ) - in parserCss() function, line 37. Everything is ok if you use relative paths. Problem shows when you use absolute paths, for example:
[css] background: url(/images/default/top-menu.png) repeat-x;
Line 37 changes it to something like that (/css is my original folder for css files):
[css] background: url(/css//images/default/top-menu.png) repeat-x;
Also you should consider replacing compressContent() with something better. CssMin is great class. JS compressing would be nice too - Vince developed nice extension, google-complier-extension. Think about this. ;)
Bug Fixed
Thanks, bug in parserCss is fixed now
I will study this CssMin to replace compress
CssMin
I will test it tomorrow and see if there are any other bugs. :)
About CssMin - it is really simple.
function minify($code) { require_once('CssMin.php'); return CssMin::minify($code, array('compress-unit-values' => true)); }
As you see you only need to include CssMin file and call CssMin::minify($code, $options) function. You may want to check out $options, but that's all.
Bug Fixed (Version 1.3)
Fixed bug in "registerScriptFile"
compress of css is using "CssMin"
compress of js is using "JavaScriptPacker"
One more thing...
I found bug in registerScriptFile(), but it's already fixed. Nice. :) But there is something wrong with CssMin - maybe because my example was for CssMin 2.0.1 and you are using CssMin 3.0 (didn't know it is out), so entire script breakes. You have to change line 106 to this:
return CssMin::minify($content, array(), array('CompressUnitValues' => true));
BTW have you ever tested JavaScriptPacker for large files? I've tried a lot of libraries and some of them (JsMin+) caused memory leak while processing big files. Don't have time to try this now, maybe someone else will test.
Not working to me
Hi,
i have installed this, and follow all setups but dont work for me
in view page source, its blank of scripts or css includes
Thanks
not working?
use "Yii::app()->dynamicRes->saveScheme();" after yours "Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/style.css')"
Version 1.4 > CssMin Bug Fix 4 and Fix Bug in double "saveScheme"
Great job
Everything works fine. Will let you know how it works with big css/js files after some tests.
Not working to me
hi,
i use it in controller like it say on setup
protected function afterRender($view, &$output) { Yii::app()->dynamicRes->saveScheme(); }
so i have to useit in view instead controller?
Wrong Config
change config to
... 'dynamicRes'=>array( 'class' => 'application.extensions.DynamicRes.DynamicRes', 'urlConfig' => array( // Its fix Css, and convert Url to RealName 'baseUrl' => '/', // Url of your Site (ending with /), modify it if you use subdomain 'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This) ) ), ...
note: basePath and baseUrl end with "/"
Still not working
i dont know why, but it still not working ....
all placed with savescheme but i got no results :(
my code:
<?php //$cs=Yii::app()->clientScript; $cs=Yii::app()->dynamicRes; $cs->registerScriptFile(BASEURL.'/resources/js/jquery/hoverIntent.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/jquery.cycle.all.min.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/jquery.easing.1.3.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/superfish.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/supersubs.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/validation/jquery.validationEngine-pt.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/validation/jquery.validationEngine.js'); $cs->registerScriptFile(BASEURL.'/resources/js/jquery/jquery.formalize.min.js'); $cs->registerCssFile(BASEURL.'/resources/css/screen.css', 'screen, projection'); // blueprint CSS framework //$cs->registerCssFile(Yii::app()->theme->baseUrl.'/resources/css/print.css', 'print'); // blueprint CSS framework $cs->registerCssFile(Yii::app()->theme->baseUrl.'/resources/css/form.css'); $cs->registerCssFile(Yii::app()->theme->baseUrl.'/resources/css/style.css'); $cs->registerCssFile(Yii::app()->theme->baseUrl.'/resources/css/site.css'); $cs->registerCssFile(BASEURL.'/resources/css/jquery/validation/validationEngine.jquery.css'); $cs->registerCssFile(BASEURL.'/resources/css/jquery/formalize.css'); //$cs->registerCssFile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/themes/ui-lightness/jquery-ui.css'); ?>
not working?
Upgrade to "DynamicRes 1.5" and use function debug();
<?php Yii::app()->dynamicRes->debug(); ?>
use this after include yours files (in your view)
not working
thanks i now see where its the error
dynamicRes: FUNCTION DEBUG CALLED - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\hoverIntent.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\jquery.cycle.all.min.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\jquery.easing.1.3.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\superfish.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\supersubs.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\validation\jquery.validationEngine-pt.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\validation\jquery.validationEngine.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\js\jquery\jquery.formalize.min.js dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\css\screen.css dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\themes\default\resources\css\form.css dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\themes\default\resources\css\style.css dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\themes\default\resources\css\site.css dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\css\jquery\validation\validationEngine.jquery.css dont exists. - File D:\wamp\www\yii\ecorifa\yii\ecorifa\resources\css\jquery\formalize.css dont exists.
but why it repeat path? if im using yii base url
$cs->registerCssFile(Yii::app()->theme->baseUrl.'/resources/css/site.css'); $cs->registerCssFile(BASEURL.'/resources/css/jquery/validation/validationEngine.jquery.css'); $cs->registerCssFile(BASEURL.'/resources/css/jquery/formalize.css');
Wrong Config
You need to config Url Base of Your Site, your baseUrl is "/yii/escorifa/"
'urlConfig' => array( // Its fix Css, and convert Url to RealName 'baseUrl' => '/yii/escorifa/', // Url of your Site (ending with /), modify it if you use subdomain 'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This) )
another bug
css:
.sf-sub-indicator { position: absolute; display: block; right: 10px; top: 17px; /* IE6 only */ width: 10px; height: 10px; text-indent: -999em; overflow: hidden; background: url('../images/arrows-ffffff.png') no-repeat -10px -100px; /* 8-bit indexed alpha png. IE6 gets solid image only */ }
RESULT:
indicator{position:absolute;display:block;right:10px;top:17px;width:10px;height:10px;text-indent:-999em;overflow:hidden;background:url(/yii/ecorifa/themes/default/resources/css/"../images/arrows-ffffff.png") no-repeat -10px -100px}
also happen in more situations
sub-indicator{background-position:-10px 0}.sf-shadow ul{background:url(/yii/ecorifa/themes/default/resources/css/'../images/shadow.png') no-repeat bottom
Bug Fixed (Version 1.6)
Bug Fixed,
now, baseUrl config is absolete, use only:
'urlConfig' => array( // Its fix Css, and convert Url to RealName 'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This) )
Works
now it works correctly
i always had problems with other cahce extensions because path and script errors on minify because a use of massive scripts ... but that works correctly
thanks and good job :)
just a resquest: minify inline js and css processed by Yii eg. Yii->app()->clientScript->javascript(); and ->css
Compress JS files Used by YII and Extensions
Wow, this extension has sped up my site (+1). Thanks for sharing it.
Can I also use this extension to compress these JavaScript files used by Yii and extensions?
For example: jquery.min.js, jquery.yii.js, query.yiigridview.js, etc.
Thanks, Jay.
I have make some modification
here is my modification.
'clientScript'=>array( 'class'=>'application.extensions.DynamicRes.DynamicRes', 'urlConfig'=>array( // Its fix Css, and convert Url to RealName 'baseUrl'=> '/', // Url of your Site (ending with /), modify it if you use subdomain 'basePath'=>WWW_PATH.'/', // path of your site (ending with /) (No Change This) ), 'debug'=>true, ),
//extends this class from CClientScript, then no need to modify any view file. class DynamicRes extends CClientScript { //compress or not. //it's important for development. public $debug = false; public function registerCssFile($urlName, $media='') { if($this->debug) return parent::registerCssFile($urlName, $media); else return $this->registerFile($this->cssList, $urlName); } public function registerScriptFile($urlName, $position=self::POS_HEAD) { if($this->debug) return parent::registerScriptFile($urlName, $position); else return $this->registerFile($this->jsList, $urlName); } public function saveScheme() { if( ! $this->debug) { if(count($this->jsList)>0) { //do not loading the same js files. $this->jsList = array_unique($this->jsList); $fileJs = $this->saveFileScheme($this->jsList, 'js'); parent::registerScriptFile($fileJs); } if(count($this->cssList)>0) { //do not loading the same css files. $this->cssList = array_unique($this->cssList); $fileCss = $this->saveFileScheme($this->cssList, 'css'); parent::registerCssFile($fileCss); } $this->jsList = array(); $this->cssList = array(); } } }
Awesomeness!
This extension turned 5-6 script and link tags into one! However, I am still facing a few problems...
For one, when I minify socket.io.js, I get a JS error int he console about an unexpected token. Also with pick-a-color-1.2.3.min.js.
Is there a way to add configuration options to DynamicRes to:
You could look at http://www.yiiframework.com/extension/escriptboost/ for some good JS packers. This extension does not provide a register{...}File set of functions, which is why i am using DynamicRes and EScriptBoost side-by-side.
I also have another problem about CSS. I am using WingStyle ( https://github.com/IngwiePhoenix/WingStyle ) to generate my main CSS. I didn't figure out a good way to "automatically generate and update" a static CSS from it, yet. So is there an option to append a CSS file to the array verbatim, without it getting either minified or even added to the big file?
You can see my current code for this here: http://git.ingwie.me/ingwie/bird3/blob/master/protected/components/Controller.php
Would be great if you could help me out here ^_^ Kind regards, Ingwie.
not working
I tried to combine several js files but it's not working. Moreover, the html position of the generated js file is in the HEAD tag, and I would like to be just before de end of the BODY tag. Furthermore, Yii::app()->dynamicRes->registerCoreScript('jquery') is not supported
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.