tetrode / minify
CSS & JavaScript 压缩器,用 PHP 实现。移除空白字符,删除注释,合并文件(包括 @import 语句和 CSS 文件中的小资产),并优化/缩短一些常见的编程模式。
Requires
- php: >=5.3.0
- ext-pcre: *
- matthiasmullie/path-converter: ~1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- matthiasmullie/scrapbook: ~1.0
- phpunit/phpunit: ~4.8
Suggests
- psr/cache-implementation: Cache implementation to use with Minify::cache
- dev-master
- 1.3.55
- 1.3.54
- 1.3.53
- 1.3.52
- 1.3.51
- 1.3.50
- 1.3.49
- 1.3.48
- 1.3.47
- 1.3.46
- 1.3.45
- 1.3.44
- 1.3.43
- 1.3.42
- 1.3.41
- 1.3.40
- 1.3.39
- 1.3.38
- 1.3.37
- 1.3.36
- 1.3.35
- 1.3.34
- 1.3.33
- 1.3.32
- 1.3.31
- 1.3.30
- 1.3.29
- 1.3.28
- 1.3.27
- 1.3.26
- 1.3.25
- 1.3.24
- 1.3.23
- 1.3.22
- 1.3.21
- 1.3.20
- 1.3.19
- 1.3.18
- 1.3.17
- 1.3.16
- 1.3.15
- 1.3.14
- 1.3.13
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-patch-1
This package is not auto-updated.
Last update: 2024-09-29 04:43:03 UTC
README
移除空白字符,删除注释,合并文件(包括 @import
语句和 CSS 文件中的小资产),并优化/缩短一些常见的编程模式,例如
JavaScript
object['property']
->object.property
true
,false
->!0
,!1
while(true)
->for(;;)
CSS
@import url("http://path")
->@import "http://path"
#ff0000
,#ff00ff
->red
,#f0f
-0px
,50.00px
->0
,50px
bold
->700
p {}
-> removed
并且它附带了一个庞大的测试套件。
用法
CSS
use MatthiasMullie\Minify; $sourcePath = '/path/to/source/css/file.css'; $minifier = new Minify\CSS($sourcePath); // we can even add another file, they'll then be // joined in 1 output file $sourcePath2 = '/path/to/second/source/css/file.css'; $minifier->add($sourcePath2); // or we can just add plain CSS $css = 'body { color: #000000; }'; $minifier->add($css); // save minified file to disk $minifiedPath = '/path/to/minified/css/file.css'; $minifier->minify($minifiedPath); // or just output the content echo $minifier->minify();
JS
// just look at the CSS example; it's exactly the same, but with the JS class & JS files :)
方法
可用的方法,对于 CSS 和 JS 压缩器,都是
__construct(/* 路径重载 */)
对象构造函数接受 0、1 或多个文件路径,甚至完整的 CSS/JS 内容,这些内容应该被压缩。所有传递的 CSS/JS 将合并成 1 个压缩文件。
use MatthiasMullie\Minify; $minifier = new Minify\JS($path1, $path2);
add($path, /* 路径重载 */)
这大致相当于构造函数。
$minifier->add($path3); $minifier->add($js);
minify($path)
这将压缩文件内容,将结果保存到 $path,并返回结果内容。如果省略 $path 参数,则结果不会写入任何地方。
注意:如果您有带有相对路径的 CSS(到导入、图片等),您应始终指定目标路径!然后那些相对路径将根据新路径进行调整。
$minifier->minify('/target/path.js');
gzip($path, $level)
压缩并可选地保存到文件,就像 minify()
一样,但它还使用 gzencode()
对压缩内容进行压缩。
$minifier->gzip('/target/path.js');
setMaxImportSize($size) (仅限 CSS)
CSS 压缩器将自动将引用的文件(如图片、字体等)嵌入到压缩的 CSS 中,这样就不需要通过多个连接进行获取。
然而,对于非常大的文件,单独加载它们可能更好(因为如果包含在内,它会增加 CSS 的加载时间。)
此方法允许将导入到压缩 CSS 中的文件的最大大小设置为(以 KB 为单位)。默认大小为 5。
$minifier->setMaxImportSize(10);
setImportExtensions($extensions) (仅限 CSS)
CSS 压缩器将自动将引用的文件(如图片、字体等)嵌入到压缩的 CSS 中,这样就不需要通过多个连接进行获取。
此方法允许指定文件的类型,以及它们的数据:mime 类型。
默认嵌入的文件类型是 gif、png、jpg、jpeg、svg 和 woff。
$extensions = array( 'gif' => 'data:image/gif', 'png' => 'data:image/png', ); $minifier->setImportExtensions($extensions);
安装
如果您使用 Composer 管理项目的依赖项,只需在 composer.json 文件中添加对 matthiasmullie/minify 的依赖即可。
composer require matthiasmullie/minify
虽然建议使用 Composer,但实际上您可以根据需要包含这些文件。
尝试一下
只需在https://www.minifier.org上在线尝试。
许可协议
Minify 使用 MIT 许可协议。
挑战
如果您对了解我在构建此产品时遇到的一些更困难的技术挑战感兴趣,您可能想看看我在博客上所写的关于它的内容。