2017 © Pedro Peláez
 

library yii-asset-compress

Command to merge and minify assets for Yii

image

cornernote/yii-asset-compress

Command to merge and minify assets for Yii

  • Friday, April 29, 2016
  • by cornernote
  • Repository
  • 2 Watchers
  • 1 Stars
  • 4,244 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 7 % Grown

The README.md

Yii Asset Compress

Command to merge and minify assets for Yii., (*1)

Features

  • Merges and minifies lists of CSS or JS files into a single CSS or JS files.
  • Replaces releative url() in CSS files
  • Publishes required assets so that relative assets are available

Installation

Please download using ONE of the following methods:, (*2)

Composer Installation

All requirements are automatically downloaded into the correct location when using composer. There is no need to download additional files or set paths to third party files., (*3)

Get composer:, (*4)

curl http://getcomposer.org/installer | php

Install latest release OR development version:, (*5)

php composer.phar require cornernote/yii-asset-compress:*            // latest release
php composer.phar require cornernote/yii-asset-compress:dev-master    // development version

Add the vendor folder to the aliases in your yii configuration:, (*6)

return array(
    'aliases' => array(
        'vendor' => '/path/to/vendor',
    ),
);

Manual Installation

Download the latest release or development version and move the commands/AssetCompressCommand.php file into your protected/commands folder., (*7)

In addition the following are required: - tedious/JShrink - mrclay/minify, (*8)

Configuration

Add to your yii console config:, (*9)

return array(
    'commandMap' => array(
        'assetCompress' => array(
            'class' => 'vendor.cornernote.yii-asset-compress.commands.AssetCompressCommand',
            'assetsPath' => 'application.assets',
            'css' => array(
                'combine' => array(
                    'css/combined.css' => array(                                     // output to application.assets|css/desktop.css
                        // format is: asset.path.alias|path/to/asset.css
                        'vendor.twbs.bootstrap.dist|css/bootstrap.css',             // -{ (alias!=application) = this asset path will be
                        'bootstrap.assets|css/yiistrap.css',                        // -{ published, and any url() in the CSS will be 
                        'vendor.fortawesome.font-awesome|css/font-awesome.min.css', // -{ replaced with the correct relative path.
                        'application.assets|css/app.css',                           // -{
                        'application|css/app.css',                                  // - (alias=application) = Uses webroot, assets not published.
                    ),
                ),
                'minify' => true
            ),
            'js' => array(
                'combine' => array(
                    'js/combined.js' => array(                            // output to application.assets|js/desktop.js
                        // format is: asset.path.alias|path/to/asset.js
                        'system.web.js.source|jquery.min.js',            // -{ (alias!=application) = this asset path will be
                        'system.web.js.source|jquery.yiiactiveform.js',  // -{ published, and any url() in the CSS will be 
                        'vendor.twbs.bootstrap.dist|js/bootstrap.js',    // -{ replaced with the correct relative path.
                        'application.assets|js/app.js',                  // -{ 
                        'application|js/app.js',                         // - (alias=application) = Uses webroot, assets not published.
                    ),
                ),
                'minify' => true
            )
        ),
    ),
);

Compressing Assets

Run using your yiic command:, (*10)

php yiic assetCompress

command, (*11)

Using Assets

To display your combined assets on your page you can use the following in your layout file:, (*12)

$baseUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.assets'));
Yii::app()->clientScript->registerCssFile($baseUrl . '/css/combined.css');
Yii::app()->clientScript->registerScriptFile($baseUrl . '/js/combined.js');

Supressing Merged Assets

Now that you have jQuery and Bootstrap (and others) merged, you don't want them to output. One method is to overwrite CClientScript:, (*13)

<?php
class ClientScript extends CClientScript
{
    public $ignoreCoreScript = array();
    public $ignoreScriptFile = array();
    public $ignoreCssFile = array();

    public function registerCoreScript($name, $options = array())
    {
        if (in_array($name, $this->ignoreCoreScript))
            return $this;
        return parent::registerCoreScript($name);
    }
    public function registerScriptFile($url, $position = null, array $htmlOptions = array())
    {
        foreach ($this->ignoreScriptFile as $ignore)
            if ($this->endsWith($url, $ignore))
                return $this;
        return parent::registerScriptFile($url, $position, $htmlOptions);
    }
    public function registerCssFile($url, $media = '')
    {
        foreach ($this->ignoreCssFile as $ignore)
            if ($this->endsWith($url, $ignore))
                return $this;
        return parent::registerCssFile($url, $media);
    }

    private function endsWith($haystack, $needle)
    {
        $length = strlen($needle);
        if ($length == 0)
            return true;
        return (substr($haystack, -$length) === $needle);
    }
}

Set this up in your config as follows:, (*14)

return array(
    'components' => array(
        'clientScript' => array(
            'class' => 'application.components.ClientScript',
            'ignoreCssFile' => array(
                'bootstrap.css',
                'yiistrap.css',
                'font-awesome.min.css',
            ),
            'ignoreScriptFile'=>array(
                'bootstrap.js',
            ),
            'ignoreCoreScript' => array(
                'jquery',
                'yiiactiveform',
            ),
        ),
    ),
);

Resources

Support

License

BSD-3-Clause, Copyright 2017 Mr PHP, (*15)

Mr PHP, (*16)

Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License, (*17)

The Versions

29/04 2016

dev-master

9999999-dev

Command to merge and minify assets for Yii

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii mrphp

29/04 2016

1.0.0

1.0.0.0

Command to merge and minify assets for Yii

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

yii mrphp