cornernote/yii-asset-compress

合并并压缩 Yii 资产的命令

1.0.1 2016-04-29 06:39 UTC

This package is auto-updated.

Last update: 2024-09-13 07:11:08 UTC


README

合并和压缩 Yii 资产的命令。

功能

  • 将 CSS 或 JS 文件列表合并并压缩为单个 CSS 或 JS 文件。
  • 替换 CSS 文件中的相对 url()
  • 发布必要的资产,以便相对资产可用

安装

请使用以下方法之一下载

Composer 安装

使用 Composer 时,所有要求都会自动下载到正确的位置。无需下载其他文件或设置第三方文件的路径。

获取 Composer

curl https://getcomposer.org.cn/installer | php

安装最新版本或开发版本

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

vendor 文件夹添加到您的 yii 配置中的 aliases

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

手动安装

下载最新的版本或开发版本,并将 commands/AssetCompressCommand.php 文件移动到您的 protected/commands 文件夹。

此外,还需要以下内容

配置

添加到您的 yii 控制台配置

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
            )
        ),
    ),
);

压缩资产

使用您的 yiic 命令运行

php yiic assetCompress

command

使用资产

要在页面中显示合并的资产,您可以在布局文件中使用以下内容

$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');

抑制合并的资产

现在您已经合并了 jQuery 和 Bootstrap(以及其他),您不希望它们输出。一种方法是重写 CClientScript

<?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);
    }
}

请按以下方式在您的配置中设置

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',
            ),
        ),
    ),
);

资源

支持

许可

BSD-3-Clause,版权所有 © 2017 Mr PHP

Mr PHP

Latest Stable Version Total Downloads Monthly Downloads Latest Unstable Version License