bpedroza / asset-compiler
这是一个用于php的资产编译器。可以将多个JavaScript或CSS文件编译成一个单独的文件。
v1.1.0
2017-10-28 02:08 UTC
Requires
- php: >=5.6.0
- matthiasmullie/minify: 1.3.*
Requires (Dev)
- phpunit/phpunit: ~5.7
This package is auto-updated.
Last update: 2024-09-11 03:19:50 UTC
README
此软件包允许您将多个资产编译成一个单独的文件,并对单个或编译后的资产进行压缩。目前支持的类型是CSS和JavaScript。
此软件包的额外好处是它增加了缓存破坏器。
文件使用最新文件的最后修改时间进行编译、压缩和缓存破坏。新文件不会在每次页面加载时编译,只有当编译中的文件发生变化时才会编译。
安装
将"bpedroza/asset-compiler": "1.1.*"添加到您的composer.json require数组中
配置
首先创建一个AssetCcompiler的新实例,并给它提供构建文件所需的路径。构造函数接受资产绝对路径和HTTP路径作为参数。HTTP路径可以是相对的,也可以包含完整的Web地址。
$AssetCompiler = new \Bpedroza\AssetCompiler\AssetCompiler('/path/to/my/assets/', '/assets/');
或者
$AssetCompiler = new \Bpedroza\AssetCompiler\AssetCompiler('/path/to/my/assets/', 'http://www.example.com');
接下来,您可以配置特定资产的路径、调试和异常行为。
默认情况下,假设CSS文件夹为/css,JavaScript文件夹为/js。
所有配置选项都是getter和setter,并且在设置时可以链式调用。当参数缺失时,它将返回当前的配置值。
// As Setters $AssetCompiler->config() ->cssPath('mycssfolder') ->jsPath('myjsfolder'); // As getter $compiledFolderName = $AssetCompiler->config()->compiledFolder();
可用的配置选项
使用方法
生成单个、缓存破坏的文件。
// Single css file $AssetCompiler->getStyle('test1.css', ['attr1' => 'some value', 'type' => 'text/css']); // Single js file $AssetCompiler->getScript('test1.js', ['attr1' => 'some value', 'type' => 'text/javascript']); // Compiled css file $AssetCompiler->getStylesMulti(['test1.css', 'test2.css'], 'mycompiledcssfile.css', $attributes = []); // Compiled javascript file $AssetCompiler->getScriptsMulti(['test1.js', 'test2.js'], 'mycompiledjsfile.js', $attributes = []);
输出
<link href="/css/test1.css?v=123456789" attr1="some value" type="text/css" rel="stylesheet" /> <script src="/js/test1.js?v=123456789" attr1="some value" type="text/javascript" ></script> <link href="/css/compiled/mycompiledcssfile.css?v=123456789" rel="stylesheet" /> <script src="/js/compiled/mycompiledjsfile.js?v=123456789" ></script>
压缩后的输出
<link href="/css/compiled/test1.min.css?v=123456789" attr1="some value" type="text/css" rel="stylesheet" /> <script src="/js/compiled/test1.min.js?v=123456789" attr1="some value" type="text/javascript" ></script> <link href="/css/compiled/mycompiledcssfile.css?v=123456789" rel="stylesheet" /> <script src="/js/compiled/mycompiledjsfile.js?v=123456789" ></script>