Extends ClientScript to minify and pack registered assets. It uses the PHP port of Douglas Crockford's jsmin by rgrove (https://github.com/rgrove/jsmin-php).
I tested this extension with Yii 1.1.7 < but it should work from 1.1 onwards.
Copy the folder into the extensions folder of your app. Than in the configuration file add the following section under components:
'clientScript' => array( 'class' => 'ext.ClientScriptPacker.ClientScriptPacker', ),
If this is done you can register your scriptfiles like you did before with Yii::app()->clientScript->registerScriptFile() and it will create the packed and minified javascript files in the assets folder. Your original JS files will stay untouched. If you want to re-pack the files just delete the files from your assets folder. I extended the registerScriptFile and it accepts an array as well with multiple js files.
IMPORTANT! You need to pass the path relatively to your $_SERVER['DOCUMENT_ROOT'] to the registerScriptFile method.
If you experience any problem please give me a shout. I removed the caching because it didn't work well with the assets. The minify option is off by default because it can really slow down things. I need to rework this extension just need to find to do it.
Total 2 comments
Hi vario,
You are doing exactly what you should but there is a bug in the extension. I can't push the fix to github until this evening. I will make a comment here when it is up.
Update: I just realized I can host files here so I uploaded the fixed extension.
Thanks,
Greg
Hi,
I think I'm in need of an example of how to use this script (or rather, using the Yii clientScript).
At the top of my MasterView, I register jQuery like so:
<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>And then I include the custom javascript I want to minify:
<?php Yii::app()->clientScript->registerScriptFile(array( '/scripts/jwplayer/jwplayer.js', '/scripts/plugins.js', '/scripts/desktop.js'), CClientScript::POS_END); ?>What else do I need to do? Because in doing this, it just added 3 references to a minified head.js file, which appeared to be the first jQuery file.
What am I missing?
Thanks in advance.
Leave a comment
Please login to leave your comment.