thomasperi/yacssmin

另一个CSS压缩工具

v0.7.1 2020-02-08 00:19 UTC

This package is auto-updated.

Last update: 2024-09-08 10:10:21 UTC


README

另一个CSS压缩工具

为什么?

为什么还要写另一个CSS压缩工具?请阅读PHILOSOPHY.md

安装

Composer
在你的项目目录下

composer require thomasperi/yacssmin

手动
下载Minifier.php并在你的PHP应用中require

require '/path/to/Minifier.php';

使用方法

use \ThomasPeri\YaCSSMin\Minifier;
$minified_css = Minifier::minify($css);

返回值

minify方法在成功压缩样式表时返回一个压缩后的字符串。如果它返回false,则表示CSS代码中包含未平衡的花括号、括号、括号、注释或字符串;或者字符串中的未转义换行符。为了将其与空字符串区分开来,请使用严格的比较

$minified_css = Minifier::minify($css);
if (false === $minified_css) {
    // Error
} else {
    // Success
}

保留注释

minify方法还接受一个可选的第二个参数,它应该是一个选项数组。

您可以通过编写一个用于过滤每个注释的回调函数并作为comments选项传递来选择性地保留注释。此回调函数应接受一个完整的注释(以/*开始并以*/结束)并返回一个完整的注释。为了保留注释不变,您可以只需返回传递进来的字符串

// Preserve any comments that contain the string `@license`:
$minified_css = Minifier::minify($css, [
    'comments' => function ($comment) {
        if (false !== strpos($comment, '@license')) {
            return $comment;
        }
    }
]);

如果它返回null或任何非有效的CSS注释,则注释将从输出中删除。

内容过滤

另一个可用的选项是将自定义过滤器传递进去,以改变CSS,而不仅仅是YaCSSMin本身所做的。

YaCSSMin仅移除空白和注释。它不会改变CSS中的任何值,例如缩短颜色代码或重写URL。 (有关原因,请参阅PHILOSOPHY.md。)因此,要执行此类操作,您需要使用filter选项

// Preserve any comments that contain the string `@license`:
$minified_css = Minifier::minify($css, [
    'filter' => function ($css, &$strings, &$comments) {
        // ... do stuff that modifies $css, then return the modified copy ...
        return $css;
    }
]);

函数应该接受的参数如下

如果您从$css中删除字符串或注释,请确保也将其从&$strings&$comments中删除(使用unsetarray_splice),以保持同步。

还可能有注释(/**/),它们只是被清空而不是删除,因为位置奇怪。这些在&$comments数组中没有对应项。

示例

以下是一个重写URL的示例,不包括实际的重写部分。

$minified_css = Minifier::minify($css, [
    'filter' => function ($css, &$strings) {
        $pattern = '#"_"|url\((.+?)\)#i';
        $i = 0;
        $rewrite = function ($url) {
            // ... make your changes to $url here ...
            return $url;
        };
        return preg_replace_callback(
            $pattern,
            function ($matches) use (&$i, &$strings, $rewrite) {
                $match = $matches[0];
                switch (strtolower($match)) {
                    // Skip over strings that aren't URLs, but count them.
                    case '"_"':
                        $i++;
                        break;
                
                    // Found a string that contains a URL. Rewrite the string
                    // in the array, but leave the placeholder unchanged.
                    case 'url("_")':
                        // Separate the URL from the quotes, rewrite the URL,
                        // and put the quotes back on.
                        $string = $strings[$i];
                        $quote = substr($string, 0, 1);
                        $url = substr($string, 1, strlen($string) - 2);
                        $strings[$i] = $quote . $rewrite($url) . $quote;
                        $i++;
                        break;
                    
                    // Unquoted URLs aren't stashed as strings, so do the
                    // rewrite on what was found inside url(...).
                    default:
                        $match = 'url(' . $rewrite($matches[1]) . ')';
                }
                return $match;
            },
            $css
        );
    }
]);

发音

/ YAX min /