rudrax / magicmin
基于PHP的JavaScript压缩器
Requires
- php: >=5.3.0
README
MagicMin是一个基于PHP的JavaScript和CSS压缩与合并类。
此类旨在生成压缩、合并和自动更新的文件,以简化在生产环境和开发环境之间使用压缩文件和更新的过程。
此类有四个主要功能
- 单个文件的压缩
- $class->minify( 'sourcefile', 'outputfile', 'version' );
- 合并和压缩整个目录的内容
- $class->merge( 'outputfile', 'directory', 'type [css, js]', array( 'items to exclude' ), array( 'files to merge and minify in order' ) );
- 可以通过使用第五个和最后一个参数来指定输出和压缩的顺序,文件作为数组传递
- 这里只包含必要的文件,而不是目录中的所有文件
- 编码图像文件数据,替换CSS中的外部图像引用
- 仅适用于CSS文件
- 默认为false(保留图像引用)(见“基本用法,项目#1”)
- 使用gzip和指定的缓存控制提供生成的资产
- zlib必须存在并启用,否则不使用gzip
- 默认过期设置为30天(60 x 60 x 24 x 31)
此类使用filemtime确定何时以及何时重新创建压缩版本,并且只有在要包含在minify或merge函数中的文件比之前创建的压缩文件新时,才会创建新的压缩文件
文件名中包含".min."的文件不会压缩其内容,但仍然会将其内容返回并添加到编译文件中,就像它应该被假定已经压缩一样。
完整的用法示例包含在example.php中,此包包含在/base中的jqueryui样式以及一些用于测试的杂项JavaScript和bootstrap文件。
由于不一致,于2013年6月15日移除了正则表达式和str_replace运算符,并已用以下替换
- JsMin作为默认
- 如果jsmin.php文件未在class.magic-min.php相同目录中找到,它将从GitHub检索并在同一目录中创建
- JsMin是默认的JavaScript压缩包
- Google Closure
- 要使用Google Closure,请将'class initation'中的'closure' => true添加
##基本用法首先,包含并初始化类。类已更新为使用具有多达4个键-值对的数组,所有接受布尔值或可以完全省略
- Base64编码的图像(本地或远程)可以在生成时自动替换文件引用。这仅适用于CSS文件。
- 'encode' => true/false(默认为false)
- 以"http"或"https"开头的url()类型文件路径将通过cURL检索并编码,而不是用于本地文件的file_get_contents
- 输出生成的文件路径,或返回作为变量使用
- 'echo' => true/false(默认为true)
- 输出总执行时间
- 'timer' => true/false(默认为false)
- 作为__destruct的一部分设置,以记录到JavaScript控制台,根据需要调整
- 使用gzip和缓存控制输出压缩/合并的资产
- 'gzip' => true/false(默认为false)
- 使用Google Closure API而不是jsmin(jsmin是默认选项)
- 'closure' => true(默认为false)
require( 'class.magic-min.php' ); //Default usage will echo from function calls and leave images untouched $minified = new Minifier(); //Return data without echo $minified = new Minifier( array( 'echo' => false ) ); //Echo the resulting file path, while base64_encoding images and including as part of css $minified = new Minifier( array( 'encode' => true ) ); //Return only AND encode/include graphics as part of css (gasp) $minified = new Minifier( array( 'encode' => true, 'echo' => false ) ); //Include images as part of the css, and gzip $minified = new Minifier( array( 'encode' => true, 'gzip' => true ) ); //Use google closure API, and gzip the resulting file $minified = new Minifier( array( 'gzip' => true, 'closure' => true ) );
输出一个单独的最小化样式表
<link rel="stylesheet" href="<?php $minified->minify( 'css/style.css' ); ?>" />
输出一个单独的最小化javascript文件(将输出为js/autogrow.min.js)
<script src="<?php $minified->minify( 'js/autogrow.js' ); ?>"></script>
合并并最小化整个目录的内容
<script src="<?php $minified->merge( 'js/packed.min.js', './js', 'js' ); ?>"></script> <link rel="stylesheet" href="<?php $minified->merge( 'css/master.min.css', './css', 'css' ); ?>" />
##高级用法
以指定的文件名和?v=参数输出一个最小化样式表
<link rel="stylesheet" href="<?php $minified->minify( 'css/style.css', 'css/style-compressed.min.css', '1.1' ); ?>" /> //Will output: <link rel="stylesheet" href="css/style-compressed.min.css?v=1.1" />
以[filename].min.js名称和?v=1.8参数输出一个最小化javascript文件
<script src="<?php $minified->minify( 'js/jquery.reveal.js', '', '1.8' ); ?>"></script> //Will output: <script src="js/jquery.reveal.min.js?v=1.8"></script>
检索/js目录中所有javascript文件的内容作为master.min.js(排除上述文件)
<?php $exclude = array( 'js/autogrow.js', 'js/autogrow.min.js', 'js/jquery.reveal.js', 'js/jquery.reveal.min.js' ); ?> <script src="<?php $minified->merge( 'js/packed.min.js', 'js', 'js', $exclude ); ?>"></script>
获取目录中的所有样式表并创建一个单独的最小化样式表(排除上述使用的样式表)
<?php $exclude_styles = array( 'css/style.css', 'css/style-compressed.min.css' ); ?> <link rel="stylesheet" href="<?php $minified->merge( 'css/master.min.css', 'css', 'css', $exclude_styles ); ?>" />
###查看MagicMin执行的动作
为了提供更多透明度,已添加"logs()"函数,可以将console.log消息输出到使用MagicMin的页面,以便在页面源代码或javascript控制台中查看。
如果使用该函数,应在magicmin脚本完成所有其他输出之后请求该函数,即...
<html> <head> <?php $minified = new Minifier( array( 'gzip' => true, 'timer' => true ) ); ?> <script src="<?php $minified->merge( 'js/my-new-file.min.js', 'javascript-directory', 'js' ); ?>"></script> </head> <body> <h1>your content here</h1> <?php $minified->logs(); ?> </body> </html>
这将向控制台输出类似以下内容
Minifier Log : Timer enabled
Minifier Log : Gzip enabled
Minifier Log: timer : MagicMin processed and loaded in 0.00019097328186 seconds
###在使用"merge"时指定包含文件的顺序
由于假设glob()将确定依赖关系并不现实,因此merge()函数存在第五个参数。
为了指定顺序,您只需指定需要排序的文件即可。您不需要指定目录中的所有文件,函数将自动优先考虑通过$order参数传入的文件。
<?php //Only exclude autogrow.js $exclude = array( 'js/autogrow.js' ); //Put jquery and validate before anything else $order = array( 'js/jquery.min.js', 'js/jquery.validate.js' ); <script src="<?php $minified->merge( 'js/magically-ordered.min.js', 'js', 'js', $exclude, $order ); ?>"></script>
###仅按顺序包含指定的文件,忽略所有其他文件
自2013年6月13日起,->merge()函数的第三个参数现在可以接受一个文件数组。文件按照数组中指定的顺序包含。
当使用数组作为第三个参数(而不是"css"或"js"文件类型)时,不要包含其他参数,因为这可能会产生冲突。
<?php $include_only = array( 'css/base/jquery.ui.all.css', 'css/base/jquery.ui.base.css', 'css/base/jquery.ui.spinner.css' ); ?> <link rel="stylesheet" href="<?php $minified->merge( 'css/base/specified-files.css', 'css/base', $include_only ); ?>" />
注意:文件必须是同一类型(css或js),因为不同类型的文件在输出中不会表现良好
##在WordPress中的使用
由于许多MVC框架(如WordPress)使用完整的URI来样式表和javascript文件,因此首先创建一个包含header.php文件绝对路径的变量,并确保Minifier初始化设置为echo为false很有帮助。
例如(在header.php中)
$dir = dirname( __FILE__ ); require_once( $dir .'/includes/class.magic-min.php' ); $min = new Minifier( array( 'echo' => false ) );
然后执行str_replace操作以重新生成样式表或javascript文件的URI
<link rel="stylesheet" href="<?php echo str_replace( $dir, get_bloginfo('template_directory'), $min->merge( $dir . '/css/master.min.css', $dir . '/css', 'css' ) ); ?>" />
这将反过来输出正确的样式表URI
<link rel="stylesheet" href="http://yourwebsite.com/wp-content/themes/yourtheme/css/master.min.css" />