minify/README.md

89 lines
3.1 KiB
Markdown
Raw Normal View History

2012-10-09 12:48:24 +02:00
# Minify
2012-10-09 02:29:59 -07:00
2014-10-08 17:03:29 +02:00
[![Build status](https://api.travis-ci.org/matthiasmullie/minify.svg?branch=master)](https://travis-ci.org/matthiasmullie/minify)
2014-10-12 10:37:54 +02:00
[![Code coverage](http://img.shields.io/coveralls/matthiasmullie/minify.svg)](https://coveralls.io/r/matthiasmullie/minify)
2014-10-12 22:42:00 +02:00
[![Code quality](http://img.shields.io/scrutinizer/g/matthiasmullie/minify.svg)](https://scrutinizer-ci.com/g/matthiasmullie/minify)
2014-10-08 17:03:29 +02:00
[![Latest version](http://img.shields.io/packagist/v/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify)
[![Downloads total](http://img.shields.io/packagist/dt/matthiasmullie/minify.svg)](https://packagist.org/packages/matthiasmullie/minify)
[![License](http://img.shields.io/packagist/l/matthiasmullie/minify.svg)](https://github.com/matthiasmullie/minify/blob/master/LICENSE)
2014-10-09 10:56:50 +02:00
## Usage
### CSS
use MatthiasMullie\Minify;
$sourcePath = '/path/to/source/css/file.css';
$minifier = new Minify($file);
// we can even add another file, they'll then be joined in 1 output file
$sourcePath2 = '/path/to/second/source/css/file.css';
$minifier->add($sourcePath2);
// or we can just add plain CSS
$css = 'body { color: #000000; }';
$minifier->add($css);
// save minified file to disk
$minifiedPath = '/path/to/minified/css/file.css';
$minifier->minify($minifiedPath);
// or just output the content
echo $minifier->minify();
### JS
// just look at the CSS example; it's exactly the same, but with JS files :)
2012-10-09 12:48:24 +02:00
## Methods
Available methods, for both CSS & JS minifier, are:
### __construct(/* overload paths */)
2014-10-09 10:56:50 +02:00
The object constructor accepts 0, 1 or multiple paths of files, or even complete CSS/JS content, that should be minified.
All CSS/JS passed along, will be combined into 1 minified file.
2012-10-09 12:48:24 +02:00
use MatthiasMullie\Minify;
2014-10-09 10:56:50 +02:00
$minifier = new Minify\JS($path1, $path2);
2012-10-09 12:48:24 +02:00
### add($path, /* overload paths */)
2014-10-09 10:56:50 +02:00
2012-10-09 12:48:24 +02:00
This is roughly equivalent to the constructor.
$minifier->add($path3);
2014-10-09 10:56:50 +02:00
$minifier->add($js);
2012-10-09 12:48:24 +02:00
### minify($path)
2014-10-09 10:56:50 +02:00
2012-10-09 12:48:24 +02:00
This will minify the files' content, save the result to $path and return the resulting content.
2014-10-09 10:56:50 +02:00
If the $path parameter is false, the result will not be written anywhere.
2012-10-09 12:48:24 +02:00
2014-10-09 10:56:50 +02:00
*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.*
2012-10-09 12:48:24 +02:00
2014-10-09 10:56:50 +02:00
$minifier->minify('/target/path.js');
2012-10-09 12:48:24 +02:00
2014-10-09 10:56:50 +02:00
## Installation
2012-10-09 12:48:24 +02:00
2014-10-09 10:56:50 +02:00
Simply add a dependency on matthiasmullie/minify to your project's composer.json file if you use [Composer](https://getcomposer.org/) to manage the dependencies of your project:
2012-10-09 12:48:24 +02:00
2014-10-09 10:56:50 +02:00
{
"require": {
"matthiasmullie/minify": "1.3.*"
}
}
Although it's recommended to use Composer, you can actually include these files anyway you want.
2012-10-09 12:48:24 +02:00
## License
2014-10-09 18:12:54 +02:00
2013-04-15 15:09:28 +03:00
Minify is [MIT](http://opensource.org/licenses/MIT) licensed.
2014-10-09 18:12:54 +02:00
## Challenges
If you're interested in learning some of the harder technical challenges I've encounterd building this, you probably want to take a look at [what I wrote about it](http://www.mullie.eu/dont-build-your-own-minifier/) on my blog.