matthiasmullie / minify
CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.
Fund package maintenance!
matthiasmullie
Installs: 20 636 487
Dependents: 347
Suggesters: 16
Security: 0
Stars: 1 971
Watchers: 78
Forks: 310
Open Issues: 107
Requires
- php: >=5.3.0
- ext-pcre: *
- matthiasmullie/path-converter: ~1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: >=2.0
- matthiasmullie/scrapbook: >=1.3
- phpunit/phpunit: >=4.8
- squizlabs/php_codesniffer: >=3.0
Suggests
- psr/cache-implementation: Cache implementation to use with Minify::cache
- dev-master
- 1.3.73
- 1.3.72
- 1.3.71
- 1.3.70
- 1.3.69
- 1.3.68
- 1.3.67
- 1.3.66
- 1.3.65
- 1.3.64
- 1.3.63
- 1.3.62
- 1.3.61
- 1.3.60
- 1.3.59
- 1.3.58
- 1.3.57
- 1.3.56
- 1.3.55
- 1.3.54
- 1.3.53
- 1.3.52
- 1.3.51
- 1.3.50
- 1.3.49
- 1.3.48
- 1.3.47
- 1.3.46
- 1.3.45
- 1.3.44
- 1.3.43
- 1.3.42
- 1.3.41
- 1.3.40
- 1.3.39
- 1.3.38
- 1.3.37
- 1.3.36
- 1.3.35
- 1.3.34
- 1.3.33
- 1.3.32
- 1.3.31
- 1.3.30
- 1.3.29
- 1.3.28
- 1.3.27
- 1.3.26
- 1.3.25
- 1.3.24
- 1.3.23
- 1.3.22
- 1.3.21
- 1.3.20
- 1.3.19
- 1.3.18
- 1.3.17
- 1.3.16
- 1.3.15
- 1.3.14
- 1.3.13
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
This package is auto-updated.
Last update: 2024-11-15 13:18:28 UTC
README
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:
JavaScript
object['property']
->object.property
true
,false
->!0
,!1
while(true)
->for(;;)
CSS
@import url("http://path")
->@import "http://path"
#ff0000
,#ff00ff
->red
,#f0f
-0px
,50.00px
->0
,50px
bold
->700
p {}
-> removed
And it comes with a huge test suite.
Usage
CSS
use MatthiasMullie\Minify; $sourcePath = '/path/to/source/css/file.css'; $minifier = new Minify\CSS($sourcePath); // 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 the JS class & JS files :)
Methods
Available methods, for both CSS & JS minifier, are:
__construct(/* overload paths */)
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.
use MatthiasMullie\Minify; $minifier = new Minify\JS($path1, $path2);
add($path, /* overload paths */)
This is roughly equivalent to the constructor.
$minifier->add($path3); $minifier->add($js);
minify($path)
This will minify the files' content, save the result to $path and return the resulting content. If the $path parameter is omitted, the result will not be written anywhere.
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.
$minifier->minify('/target/path.js');
gzip($path, $level)
Minifies and optionally saves to a file, just like minify()
, but it also gzencode()
s the minified content.
$minifier->gzip('/target/path.js');
setMaxImportSize($size) (CSS only)
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.
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.)
This method allows the max size of files to import into the minified CSS to be set (in kB). The default size is 5.
$minifier->setMaxImportSize(10);
setImportExtensions($extensions) (CSS only)
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.
This methods allows the type of files to be specified, along with their data:mime type.
The default embedded file types are gif, png, jpg, jpeg, svg, apng, avif, webp, woff and woff2.
$extensions = array( 'gif' => 'data:image/gif', 'png' => 'data:image/png', ); $minifier->setImportExtensions($extensions);
Installation
Simply add a dependency on matthiasmullie/minify
to your composer.json file if you use Composer to manage the dependencies of your project:
composer require matthiasmullie/minify
Although it's recommended to use Composer, you can actually include these files anyway you want.
License
Minify is MIT licensed.