Yii-MinifyClientScriptPackage
By Yan Li, (*1)
, (*2)
A PHP console application for minifying JavaScript and CSS files of a PHP Yii web application., (*3)
Usage
-
Minify JavaScript and CSS files with Ant-MinifyJsCss, (*4)
ant -Dsrc="path to Yii web application webroot" minify, (*5)
-
Download yiimin.phar from downloads, (*6)
-
Open a new command/terminal window, change current directory to the Yii web app webroot, and execute following command, (*7)
php yiimin.phar minify -v, (*8)
Example
Take the YiiWebApp in tests as an example, suppose you have 3 client script packages defined in Yii web app config file protected/config/main.php:, (*9)
'clientScript' => array(
'class' => 'CClientScript',
'packages' => array(
'jquery' => array(
'baseUrl' => '//code.jquery.com/',
'js' => array('jquery-1.11.3.min.js')
),
'layout' => array(
'baseUrl' => 'css/layout',
'css' => array('pageHeader.css', 'pageFooter.css')
),
'homePage' => array(
'baseUrl' => '',
'depends' => array('jquery', 'layout'),
'js' => array('js/homePage/homePage.js'),
'css' => array('css/homePage/homePage.css')
)
)
)
Firstly you need to minify each local JavaScript and CSS files, after doing so, you'll have the following files:
- css/layout/pageHeader.min.css
- css/layout/pageFooter.min.css
- css/homePage/homePage.min.css
- js/homePage/homePage.min.js, (*10)
Then, with this tool:, (*11)
- Open a new terminal window, change current directory to YiiWebApp
- Run
php yiimin.phar minify -v
After that, your config file will be changed to:, (*12)
'clientScript' => array(
'class' => 'CClientScript',
'packages' => array(
'jquery' => array(
'baseUrl' => '//code.jquery.com',
'js' => array('jquery-1.11.3.min.js')
),
'layout' => array(
'baseUrl' => '',
'css' => array('assets/layout_d7863...48618.min.css')
),
'homePage' => array(
'baseUrl' => '',
'depends' => array('jquery'),
'css' => array('assets/homePage_d8e21...fa8f7.min.css'),
'js' => array('assets/homePage_8cc59...57c458.min.js')
)
)
)
And the assets/homePage_d8e21...fa8f7.min.css comprises the contents of following files:
- css/layout/pageHeader.min.css
- css/layout/pageFooter.min.css
- css/homePage/homePage.min.css, (*13)
Limitations
- Better to only register one client script package on a page/view. If you registered several packages on a page, make sure the packages don't depend on a same package, otherwise the resources of the shared package will be loaded on the page multiple times;
- Don't use registerCssFile or registerScriptFile anymore; use registerPackage instead.
Run tests
- Install composer and run
composer install
- Install PHPUnit and run
phpunit
Build PHAR from source
- Download box.phar
- Open a new terminal/command prompt window, change current directory to this console application
- Run
php /path/to/box.phar build, new yiimin.phar will be created in downloads