dotsunited/bundlefu
Composer 安装命令:
composer require dotsunited/bundlefu
包简介
BundleFu is a PHP 5.3+ library which bundles multiple css/javascript files into a big package and sends it out at once
关键字:
README 文档
README
BundleFu is a PHP 5.3+ library which bundles multiple css/javascript files into a big package and sends it out at once.
It is highly inspired by the Ruby on Rails plugin bundle-fu.
In short, it turns this:
<script type="text/javascript" src="/js/jquery.js"></script> <script type="text/javascript" src="/js/jquery.myplugin.js"></script> <script type="text/javascript" src="/js/app.js"></script> <script type="text/javascript" src="/js/app.module.js"></script> <link media="screen" type="text/css" href="/css/reset.css"> <link media="screen" type="text/css" href="/css/jquery.myplugin.css"> <link media="screen" type="text/css" href="/css/app.css"> <link media="screen" type="text/css" href="/css/app.module.css">
Into this:
<link href="/css/cache/bundle_3f84da97fc873ca8371a8203fcdd8a82.css?1234567890" rel="stylesheet" type="text/css"> <script src="/js/cache/bundle_3f84da97fc873ca8371a8203fcdd8a82.js?1234567890" type="text/javascript"></script>
Features
- Automatically detects modifications to your css and javascript files and regenerates the bundles automatically.
- Bundle contents can be modified by filters for css url rewriting to avoid broken images, code minification and compression etc. (A Google Closure Compiler filter using the Service API comes with the library).
Installation
BundleFu can be installed using the Composer tool. You can either add dotsunited/bundlefu to the dependencies in your composer.json, or if you want to install BundleFu as standalone, go to the main directory and run:
$ wget http://getcomposer.org/composer.phar $ php composer.phar install
You can then use the composer-generated autoloader to access the BundleFu classes:
<?php require 'vendor/autoload.php'; ?>
Usage
Configure a Bundle instance:
<?php $bundle = new \DotsUnited\BundleFu\Bundle(); $bundle // Set the document root ->setDocRoot('/path/to/your/document_root') // Set the css cache path (relative to the document root) ->setCssCachePath('css/cache') // Set the javascript cache path (relative to the document root) ->setJsCachePath('js/cache'); ?>
Alternatively, you can pass an options array to the constructor (or use the method setOptions later):
<?php $options = array( 'doc_root' => '/path/to/your/document_root', 'css_cache_path' => 'css/cache', 'js_cache_path' => 'js/cache', ); $bundle = new \DotsUnited\BundleFu\Bundle($options); ?>
Use the instance to bundle your files in your templates:
<?php $bundle->start(); ?> <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/javascript" src="/js/jquery.myplugin.js"></script> <script type="text/javascript" src="/js/app.js"></script> <script type="text/javascript" src="/js/app.module.js"></script> <link media="screen" type="text/css" href="/css/reset.css"> <link media="screen" type="text/css" href="/css/jquery.myplugin.css"> <link media="screen" type="text/css" href="/css/app.css"> <link media="screen" type="text/css" href="/css/app.module.css"> <?php $bundle->end(); ?>
Output the bundle <script> and <link> tags wherever you want:
<?php // Renders both <script> and <link> tags echo $bundle->render(); // Renders the <link> tag only echo $bundle->renderCss(); // Renders the <script> tag only echo $bundle->renderJs(); ?>
Using the Factory
You can also use a factory to create bundle instances. The advantage is, that the factory can hold global options (like bypass and doc_root) which are shared across all created bundles:
<?php $options = array( 'doc_root' => '/path/to/your/document_root', 'css_cache_path' => 'css/cache', 'js_cache_path' => 'js/cache', ); $factory = new \DotsUnited\BundleFu\Factory($options); // $bundle1 and $bundle2 use the same doc_root, css_cache_path and js_cache_path options $bundle1 = $factory->createBundle(); $bundle2 = $factory->createBundle(); ?>
You can pass specific options to the createBundle method (global factory options will be overwritten):
<?php $bundle1 = $factory->createBundle(array('name' => 'bundle1', 'doc_root' => '/path/to/another/document_root')); $bundle2 = $factory->createBundle(array('name' => 'bundle2')); ?>
The factory also lets you define name aliases for filters. You can then define the string alias for the css_filter and js_filter options instead of passing a filter instance:
<?php $filters = array( 'js_closure_compiler' => new \DotsUnited\BundleFu\Filter\ClosureCompilerServiceFilter() ); $factory = new \DotsUnited\BundleFu\Factory(array(), $filters); $bundle1 = $factory->createBundle(array('js_filter' => 'js_closure_compiler')); ?>
Filters
You can manipulate the loaded css/javascript files and the bundled css/javascript code with filters. Filters are classes which implement DotsUnited\BundleFu\Filter\FilterInterface.
You can add filters like this:
<?php $bundle->setCssFilter(new MyCssFilter()); $bundle->setJsFilter(my MyJsFilter()); ?>
If you need multiple filters, you can use DotsUnited\BundleFu\Filter\FilterChain like this:
<?php $filterChain = new \DotsUnited\BundleFu\Filter\FilterChain(); $filterChain->addFilter(new MyCssFilter1()); $filterChain->addFilter(new MyCssFilter2()); $bundle->setCssFilter($filterChain); ?>
Default filters
BundleFu provides the following filters out of the box.
CssUrlRewriteFilter
The DotsUnited\BundleFu\Filter\CssUrlRewriteFilter rewrites relative URLs in your CSS file to avoid broken image references:
<?php $bundle->setCssFilter(new \DotsUnited\BundleFu\Filter\CssUrlRewriteFilter()); ?>
ClosureCompilerServiceFilter
This filter compiles javascript code with the Google Closure Compiler using the Service API.
Simply add the DotsUnited\BundleFu\Filter\ClosureCompilerServiceFilter filter and your javascript bundles will be automatically compiled:
<?php $bundle->setJsFilter(new \DotsUnited\BundleFu\Filter\ClosureCompilerServiceFilter()); ?>
CallbackFilter
The DotsUnited\BundleFu\Filter\CallbackFilter can filter by using any PHP callback. If you want to compress your CSS using YUI Compressor you can either write a custom filter or use the following code leveraging the Callback filter:
<?php $filter = new \DotsUnited\BundleFu\Filter\CallbackFilter(function($content) { $descriptorspec = array( 0 => array('pipe', 'r'), // STDIN 1 => array('pipe', 'w'), // STDOUT 2 => array('pipe', 'a') // STDERR ); $handle = proc_open('java -jar /path/to/yuicompressor.jar --type css' , $descriptorspec, $pipes); if (is_resource($handle)) { fwrite($pipes[0], $content); fclose($pipes[0]); $compressed = stream_get_contents($pipes[1]); fclose($pipes[1]); proc_close($handle); if ($compressed) { return $compressed; } } return $content; }); $bundle->setCssFilter($filter); ?>
Notes
-
All content inside of
$bundle->start()and$bundle->end()will be lost. Be sure to only put css/javascript includes inside of the block. -
Scripts/stylesheets are detected by parsing the output and looking for include files. HTML comments are ignored, so if you comment out a script like this:
<!-- <script src="/js/script.js" type="text/javascript"></script> -->the comment will be ignored and the file will be bundled anyways. Be sure to comment out via PHP:
<?php /* <script src="/js/script.js" type="text/javascript"></script> */ ?>
-
External dependencies via querystring loading will not work:
<script src="/js/scriptaculous.js?load=effects,controls" type="text/javascript"></script>
Instead, you'll need to include each javascript file as normal.
License
BundleFu is released under the MIT License.
dotsunited/bundlefu 适用场景与选型建议
dotsunited/bundlefu 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.24k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2011 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「css」 「bundling」 「asset」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dotsunited/bundlefu 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dotsunited/bundlefu 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dotsunited/bundlefu 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
Twig extension for BundleFu
This is a php based js script file bundling composer package. Including this package into php project will allow the javascript files to be delivered to client as a bundle or individually
统计信息
- 总下载量: 29.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 70
- 点击次数: 24
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2011-11-25