| 1 |
# Minify |
| 2 |
|
| 3 |
[](https://travis-ci.org/matthiasmullie/minify](https://travis-ci.org/matthiasmullie/minify](https://travis-ci.org/matthiasmullie/minify) |
| 4 |
[](https://codecov.io/github/matthiasmullie/minify](https://codecov.io/github/matthiasmullie/minify](https://codecov.io/github/matthiasmullie/minify) |
| 5 |
[](https://scrutinizer-ci.com/g/matthiasmullie/minify](https://scrutinizer-ci.com/g/matthiasmullie/minify](https://scrutinizer-ci.com/g/matthiasmullie/minify) |
| 6 |
[](https://packagist.org/packages/matthiasmullie/minify](https://packagist.org/packages/matthiasmullie/minify](https://packagist.org/packages/matthiasmullie/minify) |
| 7 |
[](https://packagist.org/packages/matthiasmullie/minify](https://packagist.org/packages/matthiasmullie/minify](https://packagist.org/packages/matthiasmullie/minify) |
| 8 |
[](https://github.com/matthiasmullie/minify/blob/master/LICENSE](https://github.com/matthiasmullie/minify/blob/master/LICENSE](https://github.com/matthiasmullie/minify/blob/master/LICENSE) |
| 9 |
|
| 10 |
|
| 11 |
**[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=73CVYMBETDAP4Donate/Support: ](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=73CVYMBETDAP4](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=73CVYMBETDAP4)** |
| 12 |
|
| 13 |
Removes whitespace, strips comments, combines files (incl. `@import` statements and small assets in CSS files), and optimizes/shortens a few common programming patterns, such as: |
| 14 |
|
| 15 |
**JavaScript** |
| 16 |
* `object['property']` -> `object.property` |
| 17 |
* `true`, `false` -> `!0`, `!1` |
| 18 |
* `while(true)` -> `for(;;)` |
| 19 |
|
| 20 |
**CSS** |
| 21 |
* `@import url("http://path")` -> `@import "http://path"` |
| 22 |
* `#ff0000`, `#ff00ff` -> `red`, `#f0f` |
| 23 |
* `-0px`, `50.00px` -> `0`, `50px` |
| 24 |
* `bold` -> `700` |
| 25 |
* `p {}` -> removed |
| 26 |
|
| 27 |
And it comes with a huge test suite. |
| 28 |
|
| 29 |
|
| 30 |
## Usage |
| 31 |
|
| 32 |
### CSS |
| 33 |
|
| 34 |
```php |
| 35 |
use MatthiasMullie\Minify; |
| 36 |
|
| 37 |
$sourcePath = '/path/to/source/css/file.css'; |
| 38 |
$minifier = new Minify\CSS($sourcePath); |
| 39 |
|
| 40 |
// we can even add another file, they'll then be |
| 41 |
// joined in 1 output file |
| 42 |
$sourcePath2 = '/path/to/second/source/css/file.css'; |
| 43 |
$minifier->add($sourcePath2); |
| 44 |
|
| 45 |
// or we can just add plain CSS |
| 46 |
$css = 'body { color: #000000; }'; |
| 47 |
$minifier->add($css); |
| 48 |
|
| 49 |
// save minified file to disk |
| 50 |
$minifiedPath = '/path/to/minified/css/file.css'; |
| 51 |
$minifier->minify($minifiedPath); |
| 52 |
|
| 53 |
// or just output the content |
| 54 |
echo $minifier->minify(); |
| 55 |
``` |
| 56 |
|
| 57 |
### JS |
| 58 |
|
| 59 |
```php |
| 60 |
// just look at the CSS example; it's exactly the same, but with the JS class & JS files :) |
| 61 |
``` |
| 62 |
|
| 63 |
|
| 64 |
## Methods |
| 65 |
|
| 66 |
Available methods, for both CSS & JS minifier, are: |
| 67 |
|
| 68 |
### __construct(/* overload paths */) |
| 69 |
|
| 70 |
The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified. |
| 71 |
All CSS/JS passed along, will be combined into 1 minified file. |
| 72 |
|
| 73 |
```php |
| 74 |
use MatthiasMullie\Minify; |
| 75 |
$minifier = new Minify\JS($path1, $path2); |
| 76 |
``` |
| 77 |
|
| 78 |
### add($path, /* overload paths */) |
| 79 |
|
| 80 |
This is roughly equivalent to the constructor. |
| 81 |
|
| 82 |
```php |
| 83 |
$minifier->add($path3); |
| 84 |
$minifier->add($js); |
| 85 |
``` |
| 86 |
|
| 87 |
### minify($path) |
| 88 |
|
| 89 |
This will minify the files' content, save the result to $path and return the resulting content. |
| 90 |
If the $path parameter is omitted, the result will not be written anywhere. |
| 91 |
|
| 92 |
*CAUTION: If you have CSS with relative paths (to imports, images, ...), you should always specify a target path! Then those relative paths will be adjusted in accordance with the new path.* |
| 93 |
|
| 94 |
```php |
| 95 |
$minifier->minify('/target/path.js'); |
| 96 |
``` |
| 97 |
|
| 98 |
### gzip($path, $level) |
| 99 |
|
| 100 |
Minifies and optionally saves to a file, just like `minify()`, but it also `gzencode()`s the minified content. |
| 101 |
|
| 102 |
```php |
| 103 |
$minifier->gzip('/target/path.js'); |
| 104 |
``` |
| 105 |
|
| 106 |
### setMaxImportSize($size) *(CSS only)* |
| 107 |
|
| 108 |
The CSS minifier will automatically embed referenced files (like images, fonts, ...) into the minified CSS, so they don't have to be fetched over multiple connections. |
| 109 |
|
| 110 |
However, for really large files, it's likely better to load them separately (as it would increase the CSS load time if they were included.) |
| 111 |
|
| 112 |
This method allows the max size of files to import into the minified CSS to be set (in kB). The default size is 5. |
| 113 |
|
| 114 |
```php |
| 115 |
$minifier->setMaxImportSize(10); |
| 116 |
``` |
| 117 |
|
| 118 |
### setImportExtensions($extensions) *(CSS only)* |
| 119 |
|
| 120 |
The CSS minifier will automatically embed referenced files (like images, fonts, ...) into minified CSS, so they don't have to be fetched over multiple connections. |
| 121 |
|
| 122 |
This methods allows the type of files to be specified, along with their data:mime type. |
| 123 |
|
| 124 |
The default embedded file types are gif, png, jpg, jpeg, svg, apng, avif, webp, woff and woff2. |
| 125 |
|
| 126 |
```php |
| 127 |
$extensions = array( |
| 128 |
'gif' => 'data:image/gif', |
| 129 |
'png' => 'data:image/png', |
| 130 |
); |
| 131 |
|
| 132 |
$minifier->setImportExtensions($extensions); |
| 133 |
``` |
| 134 |
|
| 135 |
|
| 136 |
## Installation |
| 137 |
|
| 138 |
Simply add a dependency on matthiasmullie/minify to your composer.json file if you use [](https://getcomposer.org/Composer](https://getcomposer.org/](https://getcomposer.org/) to manage the dependencies of your project: |
| 139 |
|
| 140 |
```sh |
| 141 |
composer require matthiasmullie/minify |
| 142 |
``` |
| 143 |
|
| 144 |
Although it's recommended to use Composer, you can actually [](https://github.com/matthiasmullie/minify/issues/83include these files](https://github.com/matthiasmullie/minify/issues/83](https://github.com/matthiasmullie/minify/issues/83) anyway you want. |
| 145 |
|
| 146 |
|
| 147 |
## License |
| 148 |
|
| 149 |
Minify is [](http://opensource.org/licenses/MITMIT](http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT) licensed. |
| 150 |
|
| 151 |
|
| 152 |
## Challenges |
| 153 |
|
| 154 |
If you're interested in learning some of the harder technical challenges I've encountered building this, you probably want to take a look at [](http://www.mullie.eu/dont-build-your-own-minifier/what I wrote about it](http://www.mullie.eu/dont-build-your-own-minifier/](http://www.mullie.eu/dont-build-your-own-minifier/) on my blog. |
| 155 |
|