Requirements ¶
Yii 1.1.x or above
Description: ¶
EClientScript (an extension for Yii's CClientScript)
This is an optimizing client script manager for Yii framework, that can minify and combine CSS/JS files.
It will automatically detects changes in file content (based on modification time) and generates a unique file name accordingly.
This will reduce the HTTP calls for resources files by merging several resources filesinto a single (or more) files.
Since version of 1.5, also support conditional loading js/css file in IE browser, see example codes please.
The source code is hosted under github:
https://github.com/muayyad-alsadi/yii-EClientScript
Css Files: ¶
CSS files are merged based on there media attribute, background images with a relative path in file can also be displayed correctly.
Script files: ¶
Script files are merged based on their position, If you use the 'CClientScript::POS_HEAD' you will end up with a single file for all the script files you've used on that page.
If you use 'CClientScript::POS_HEAD' and 'CClientScript::POS_END' for example then you'll end up with two files for each page on that request, becuase those resources are located in different positions.
File optmization (EXPERIMENTAL, @since: 1.1) ¶
CssMin used to optimize merged css file. You can set property 'optmizeCssFiles' of the component to enable this feature. JSMinPlus used to optimize merged script file. You can set property 'optmizeScriptFiles' of the component to enable this feature.
Usage: ¶
Using this extension is as simple as adding the following code to the application configuration under the components array:
'clientScript' => array(
'class' => 'application.vendors.yii-EClientScript.EClientScript',
'combineScriptFiles' => !YII_DEBUG, // By default this is set to true, set this to true if you'd like to combine the script files
'combineCssFiles' => !YII_DEBUG, // By default this is set to true, set this to true if you'd like to combine the css files
'optimizeScriptFiles' => !YII_DEBUG, // @since: 1.1
'optimizeCssFiles' => !YII_DEBUG, // @since: 1.1
'optimizeInlineScript' => false, // @since: 1.6, This may case response slower
'optimizeInlineCss' => false, // @since: 1.6, This may case response slower
),
~~~
Then you can use the regular 'registerScriptFile' & 'registerCssFile' methods as normal
and the files will be combined or optimized automatically.
2. Using to conditional loading js/css file for IE browser,
you just need to specify the media property.
[php] $cs = Yii::app()->clientScript;
// result to: $cs->registerScriptFile('/js/html5.js', CClientScript::POS_HEAD, array('media' => 'lt IE 9'));
// result to: $cs->registerCssFile('/css/ie.css', 'lte IE 6'); ~~~
NOTE: ¶
If you registered some external resource files that not in the web application root directory, they will be kept and not combined. Compression or optmization is a EXPERIMENTAL feature, please use it carefully(@since: 1.1)
ChangesLog: ¶
Aug 13, 2013
- New version number 1.6
- Fixed bug for merging minified scripts, they may be missing a semicolon at the end
- Add support to optimize inline css/js codes
Aug 2, 2013
- Fixed load order of non-combined css files.
Mar 29, 2013
- New version number 1.5
- Compatiable with the 3rd parameter of
registerScript
andregisterScriptFile
- Add support for conditional loading js/css file in IE.
- Prepend the base url of current request when register a script/css file with relative path
Mar 27, 2013 (by Muayyad Alsadi)
- New version number 1.4
- update JSMinPlus, CssMin
- use stronger hash for file names
- consider modification time for calculating hash
- enable all features by default
Dec 06, 2010
- Fixed problem for css files that begin with
@charset "xxx"
, it should be in the first line of file and not repeatly. - Add support for theme resource files.
Nov 23, 2010
- Skip the minimization of files whose names include
.pack.
- Add the last modification time as the QUERY_STRING to merged file, to avoid not properly flush the browser cache when the file updated.
Nov 6, 2010
- New version number 1.3
- Not repeat the minimization of Javascript codes those who have been minimized, whose names include
.min.
- Fixed
getRelativeUrl()
platform compatibility issue. (thanks to Troto)
Known Issues: ¶
When some resource files can not be merged and they are strictly dependent on loading order, then may have some problems.
Reporting Issue: ¶
Reporting Issues and comments are welcome, please report issues to https://github.com/muayyad-alsadi/yii-EClientScript/issues
all platforms compatible
line 314 (from v1.2) should be:
else if ($from === dirname($from))
Awesome!
Great extension!
It would be nice to add some caching and recombine files only when last modification date of files changes.
to nayjest
Error when merging CSS
Fatal error: Maximum execution time of 300 seconds exceeded in Z:\home\taft.loc\www\protected\extensions\minify\EClientScript.php on line 316
=>nayjest
Have you resoloved this problem, I guess there may be bug for cssMin or jsMin library, try to disable 'optimizeCssFiles' ?
svn?
svn address is ... ?
Thank you
Works great on Yii-1.0.12
bug report
Can you keep the original order of the files that combined or not into one single file? In addition, if you choose some combination, some do not mix, it is difficult to do all in order to keep the original, so that it will generate an error, for example, CSS class application.
problem with js libraries depending on js location
There are number of libraries which depends on js file location to fetch other resources relatively. Common example is CKEditor which loads css, images, and other files relatively to 'ckeditor.js' location.
When you merge this js to one file with others - CKEditor won't work.
My solution (I did such modyfication to make it work and use combined js feature):
provide 'public $whiteList = array()' configuration attribute in your component, and change piece of code that decides wheter to merge css/js entry or not. I have changed getLocalFile function to getLocalFileNotWhitelisted with body like this:
private function getLocalPathNotWhitelisted($url) { $basePath = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR; $baseUrl = Yii::app()->request->baseUrl . '/'; if (!strncmp($url, $baseUrl, strlen($baseUrl))) { $fname = basename( $url ); if( in_array( $fname, $this->whiteList ) ) { return false; } $url = $basePath . substr($url, strlen($baseUrl)); return $url; } return false; }
the core change to the original function is:
$fname = basename( $url ); if( in_array( $fname, $this->whiteList ) ) { return false; }
This solutions makes it easy to specify which files cannot be merged with others.
updated and enhanced version
I've made a fork of your code on github
and released v1.4
https://github.com/muayyad-alsadi/yii-EClientScript
the new version got updated minification and use mtime to generate file name
=> muayyad alsadi
Thanks you, I manually integrate your updating, and release new version 1.5
moving to github
=>hightman
can we use github https://github.com/muayyad-alsadi/yii-EClientScript
it's better to keep track of each other's changes
I have more enhancements to do (we can pull from each other)
=> muayyad alsadi
OK, thanks, I will change the extension README file later.
creates new files each page load?
Hi I have been using your extension for a little while now and all seemed fine until I ran out of web space yesterday. I only use the "'combineCssFiles' => true" and all others false as it was breaking some things.
however it turns out that the script is creating a brand new version of the compbined css on every single page load/refresh, it isnt caching at all, after 3 months I had used up all my web space (30 gig) and thats because there was 29 gig of combined css files in the assets folder.
I have the extension in the protected/extension folder and I have the following added to my main.php config file
'clientScript' => array( 'class' => 'ext.minify.EClientScript', 'combineScriptFiles' => false, // By default this is set to true, set this to true if you'd like to combine the script files 'combineCssFiles' => true, // By default this is set to true, set this to true if you'd like to combine the css files 'optimizeScriptFiles' => false, // @since: 1.1 'optimizeCssFiles' => false, // @since: 1.1 'optimizeInlineScript' => false, // @since: 1.6, This may case response slower 'optimizeInlineCss' => false, // @since: 1.6, This may case response slower 'coreScriptPosition' => CClientScript::POS_END, 'defaultScriptPosition' => CClientScript::POS_END, 'defaultScriptFilePosition' => CClientScript::POS_END, ),
can you suggest why it is creating a new combined css every time?
regards
wayne
Extension is Great! There is an Important issue!!!
In the version 1.6 EClientScript.php registerCssFile and registerScript do not have return value !!!
If You use an EClientScript in the following way:
$cs=Yii::app()->clientScript; $cs->registerCssFile($path . '/content.css') ->registerScriptFile($path . '/jquery.cookie.js')
It causes a strong headache (like to me)!!!
Please change in source in line 115 (and similar in 132):
parent::registerCssFile($url, $media);
to
return parent::registerCssFile($url, $media);
bug fixed
@Szincsák András: this bug is fixed here
https://github.com/muayyad-alsadi/yii-EClientScript/commit/a097d8c
Error in CssMin.php
My PHP Storm reports an error:
// CssMin.php // class CssAtImportParserPlugin extends aCssParserPlugin { // public function parse($index, $char, $previousChar, $state) { $this->parser->buffer // Member has private access
asset folder is huge
why does the script is creating a brand new version of the compbined css on every single page load/refresh? my asset folder is huge?!
answer Shahir Reza Ali
@Shahir Reza Ali
it uses mtime (modification time) of source css/js as hash. You should not change mtime/touch your files if you don't change them. You can delete old unused/unneeded combined assets.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.