clancats / packtacular
Packtacular 是一个简单快速的资产打包和压缩工具。
2.0.0
2014-03-21 15:20 UTC
This package is auto-updated.
Last update: 2024-09-24 01:36:00 UTC
README
Packtacular 是一个快速且易于使用的资产打包、压缩和管理工具。
我们都忙于各种事务,因此我们不应该浪费时间处理资产打包。我也不想过多地讨论灵感的来源,让我们直接继续介绍如何使用 Packtacular。
不要忘记你需要授予 php 写权限。
使用方法
首先,packtacular 需要知道你的存储目录(public 目录)在哪里。如果你没有设置存储目录,packtacular 将会使用相对路径。
Packtacular::storage( $_SERVER['DOCUMENT_ROOT'].'/' );
这里的思路是“保持简单”,因此要创建你的包,你只需要这样做
Packtacular::<type>( '<source>', '<target>' );
示例
// When you pass relative paths packtacular is going to prefix your path // with the storage directory. `<storage>/assets/css/` Packtacular::css( 'assets/css/', 'cache/stylesheet_:time.css' )
此示例将返回到包的绝对公共路径,假设目标是公共根路径。 /cache/stylesheet_1395407386.css
更多细节
Packtacular::<filetype>( <dir>|<files_array>, <target_file> );
类型: 这基本上意味着文件扩展名。 Packtacular::js
将匹配给定目录中的所有 .js
文件。
源: 希望这不会太令人困惑。如果你设置一个文件夹作为源,Packtacular 将会在该目录中搜索所有匹配给定类型的文件。或者你也可以传递一个文件数组。
目标: 定义缓存文件应该创建的位置。你可以简单地传递一个文件路径。为了避免浏览器自己缓存文件的问题,你可以使用 :time 参数在路径中添加时间戳。
固定数组示例
Packtacular::js( array( 'js/jquery.js', 'js/myscript.js', ), 'cache/:time/core.js' );
过滤器
过滤器可以在文件被打包之前捕获文件内容。这为你提供了压缩或编译你的资产的可能性。
Packtacular::filter( '<type>', function( $data ) { // do something with $data return $data; });
有两种类型的过滤器:包过滤器和单文件过滤器。
单文件过滤器将单独应用于每个文件。
// This little example will add a comment above each file // containing the file path of the current file. Packtacular::filter( '*', function( $data, $file_name ) { return "/* ".$file_name." */\n".$buffer; }, true );
普通过滤器只能修改整个包。
// This little example will only be executed on the entire package. Packtacular::filter( 'css', function( $data ) { return str_replace( ' ', '', $data ); });
预设过滤器
一些过滤器已经与 packtacular 一起打包,你可以通过简单地执行以下操作来添加它们
Packtacular_Filter::add( 'comments' );
以下过滤器可用
注释