mirror of
https://github.com/mrclay/minify.git
synced 2025-08-31 09:31:48 +02:00
Huge docs overhaul for 3.x
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
Unless mentioned, all the following snippets go in `min/config.php`.
|
||||
Unless mentioned, all the following snippets go in `config.php`.
|
||||
|
||||
## Faster Cache Performance
|
||||
|
||||
By default, Minify uses [Minify\_Cache\_File](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify/Cache/File.php). It uses `readfile`/`fpassthru` to improve performance over most file-based systems, but it's still file IO. I haven't done comparative benchmarks on all three, but APC/Memcache _should_ be faster. In all cases, Minify cache ids begin with `"minify_"`.
|
||||
By default, Minify uses `Minify_Cache_File`. It uses `readfile`/`fpassthru` to improve performance over most file-based systems, but it's still file IO, so the following caching options should be faster. In all cases, Minify cache ids begin with `"minify_"`.
|
||||
|
||||
### APC
|
||||
|
||||
```
|
||||
require 'lib/Minify/Cache/APC.php';
|
||||
```php
|
||||
$min_cachePath = new Minify_Cache_APC();
|
||||
```
|
||||
|
||||
### Memcache
|
||||
|
||||
You must create and connect your Memcache object then pass it to `Minify_Cache_Memcache`'s constructor.
|
||||
```
|
||||
require 'lib/Minify/Cache/Memcache.php';
|
||||
```php
|
||||
$memcache = new Memcache;
|
||||
$memcache->connect('localhost', 11211);
|
||||
$min_cachePath = new Minify_Cache_Memcache($memcache);
|
||||
@@ -23,30 +21,44 @@ $min_cachePath = new Minify_Cache_Memcache($memcache);
|
||||
|
||||
### Zend Platform
|
||||
|
||||
Patrick van Dissel has contributed a [cache adapter for Zend Platform](http://code.google.com/p/minify/issues/detail?id=167).
|
||||
```php
|
||||
$min_cachePath = new Minify_Cache_ZendPlatform();
|
||||
```
|
||||
|
||||
### XCache
|
||||
|
||||
```php
|
||||
$min_cachePath = new Minify_Cache_XCache();
|
||||
```
|
||||
|
||||
### WinCache
|
||||
|
||||
```php
|
||||
$min_cachePath = new Minify_Cache_WinCache();
|
||||
```
|
||||
|
||||
## Closure Compiler API Wrapper
|
||||
|
||||
An [experimental wrapper for Google's closure compiler API](https://github.com/mrclay/minify/blob/master/min/lib/Minify/JS/ClosureCompiler.php) is available for compressing Javascript. If the API fails for any reason, JSMin is used as the default backup minifier.
|
||||
```
|
||||
$min_serveOptions['minifiers']['application/x-javascript'] = array('Minify_JS_ClosureCompiler', 'minify');
|
||||
```php
|
||||
$min_serveOptions['minifiers'][Minify::TYPE_JS] = array('Minify_JS_ClosureCompiler', 'minify');
|
||||
```
|
||||
|
||||
## YUICompressor
|
||||
|
||||
If your host can execute Java, you can use Minify's YUI Compressor wrapper. You'll need the latest [yuicompressor-x.x.x.jar](http://yuilibrary.com/downloads/#yuicompressor) and a temp directory. Place the .jar in `min/lib`, then:
|
||||
```
|
||||
If your host can execute Java, you can use Minify's YUI Compressor wrapper. You'll need the latest [yuicompressor-x.x.x.jar](https://github.com/yui/yuicompressor/releases) and a temp directory. Place the .jar in `min/lib`, then:
|
||||
```php
|
||||
function yuiJs($js) {
|
||||
Minify_YUICompressor::$jarFile = __DIR__ . '/lib/yuicompressor-x.x.x.jar';
|
||||
Minify_YUICompressor::$tempDir = '/tmp';
|
||||
return Minify_YUICompressor::minifyJs($js);
|
||||
}
|
||||
$min_serveOptions['minifiers']['application/x-javascript'] = 'yuiJs';
|
||||
$min_serveOptions['minifiers'][Minify::TYPE_JS] = 'yuiJs';
|
||||
```
|
||||
|
||||
To use YUIC for CSS with fixed URIs:
|
||||
|
||||
```
|
||||
```php
|
||||
function yuiCss($css, $options) {
|
||||
Minify_YUICompressor::$jarFile = __DIR__ . '/lib/yuicompressor-x.x.x.jar';
|
||||
Minify_YUICompressor::$tempDir = '/tmp';
|
||||
@@ -60,49 +72,31 @@ function yuiCss($css, $options) {
|
||||
);
|
||||
return $css;
|
||||
}
|
||||
$min_serveOptions['minifiers']['text/css'] = 'yuiCss';
|
||||
```
|
||||
|
||||
### CSSmin PHP port
|
||||
|
||||
Minify has added Túbal Martín's [PHP port](https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port/blob/master/cssmin.php) of the YUI Compressor's CSSmin. While it is not completely integrated yet, you may try it out:
|
||||
|
||||
```
|
||||
function yuiCssPort($css, $options) {
|
||||
$compressor = new CSSmin();
|
||||
$css = $compressor->run($css, 9999999);
|
||||
|
||||
$css = Minify_CSS_UriRewriter::rewrite(
|
||||
$css,
|
||||
$options['currentDir'],
|
||||
isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'],
|
||||
isset($options['symlinks']) ? $options['symlinks'] : array()
|
||||
);
|
||||
return $css;
|
||||
}
|
||||
$min_serveOptions['minifiers']['text/css'] = 'yuiCssPort';
|
||||
```
|
||||
|
||||
As of commit [218f37](https://github.com/mrclay/minify/commit/218f37fb44f9be2ea138cf9efb8b7f6dc84bad7f), this is easier:
|
||||
|
||||
```
|
||||
$min_serveOptions['minifiers']['text/css'] = array('Minify_CSSmin', 'minify');
|
||||
$min_serveOptions['minifiers'][Minify::TYPE_CSS] = 'yuiCss';
|
||||
```
|
||||
|
||||
## JSMin+
|
||||
|
||||
Minify 2.1.3 comes with Tino Zijdel's [JSMin+](http://crisp.tweakblogs.net/blog/1665/a-new-javascript-minifier-jsmin+.html) 1.1. This is a full parser based on a port of [Narcissus](http://en.wikipedia.org/wiki/Narcissus_(JavaScript_engine)). To try it out:
|
||||
```
|
||||
$min_serveOptions['minifiers']['application/x-javascript'] = array('JSMinPlus', 'minify');
|
||||
```php
|
||||
$min_serveOptions['minifiers'][Minify::TYPE_JS] = array('JSMinPlus', 'minify');
|
||||
```
|
||||
This should yield smaller javascript files, but I've tested this only briefly. For production you may want to get the [latest version](http://crisp.tweakblogs.net/blog/cat/716) (you must rename it: `min/lib/JSMinPlus.php`).
|
||||
|
||||
Note: JSMin+ is memory intensive, so be prepared to up your memory limit. Also it has no [comment preservation](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/JSMin.php#10) as of 1.3, in case you rely on this.
|
||||
Note: JSMin+ is memory intensive, so be prepared to up your memory limit. Also it does not preserve comments that begin with `/*!` like JSMin does.
|
||||
|
||||
## Legacy CSS compressor
|
||||
|
||||
In 3.x, Minify uses [CSSmin](https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port), a PHP port of the YUI CSS compressor. To use the compressor that came with Minify 2.x (not recommended), uncomment this line in your `config.php` file:
|
||||
|
||||
```php
|
||||
//$min_serveOptions['minifiers'][Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
|
||||
```
|
||||
|
||||
## Server-specific Options
|
||||
|
||||
You may need to have different options depending on what server you're on. You can do this just how you'd expect:
|
||||
```
|
||||
```php
|
||||
if ($_SERVER['SERVER_NAME'] == 'myTestingWorkstation') {
|
||||
// testing
|
||||
$min_allowDebugFlag = true;
|
||||
@@ -127,7 +121,7 @@ If you test/develop sites in a subdirectory (e.g. `http://localhost/siteA/`), se
|
||||
## Group-specific Options
|
||||
|
||||
In "group" requests, `$_GET['g']` holds the group key, so you can code based on it:
|
||||
```
|
||||
```php
|
||||
if (isset($_GET['g'])) {
|
||||
switch ($_GET['g']) {
|
||||
case 'js' : $min_serveOptions['maxAge'] = 86400 * 7;
|
||||
@@ -145,7 +139,7 @@ See CustomSource.
|
||||
## Processing Output After Minification
|
||||
|
||||
If `$min_serveOptions['postprocessor']` is set to a callback, Minify will pass the minified content to this function with type as the second argument. This allows you to apply changes to your minified content without making your own custom minifier. E.g.:
|
||||
```
|
||||
```php
|
||||
function postProcess($content, $type) {
|
||||
if ($type === Minify::TYPE_CSS) {
|
||||
require_once 'CssColorReplacer.php';
|
||||
|
Reference in New Issue
Block a user