This port is based on version 2.4.8 (Jun 12, 2013) of the YUI compressor.
This port contains fixes & features not present in the original YUI compressor.
Table of Contents
Use Composer to include the library into your project:
$ composer.phar require tubalmartin/cssmin
Require Composer's autoloader file:
<?php
require './vendor/autoload.php';
use tubalmartin\CssMin\Minifier as CSSmin;
// Use it!
$compressor = new CSSmin;
There are three ways you can use this library:
<?php
// Autoload libraries
require './vendor/autoload.php';
use tubalmartin\CssMin\Minifier as CSSmin;
// Extract the CSS code you want to compress from your CSS files
$input_css = file_get_contents('test.css');
// Create a new CSSmin object.
// By default CSSmin will try to raise PHP settings.
// If you don't want CSSmin to raise the PHP settings pass FALSE to
// the constructor i.e. $compressor = new CSSmin(false);
$compressor = new CSSmin;
// Set the compressor up before compressing (global setup):
// Keep sourcemap comment in the output.
// Default behavior removes it.
$compressor->keepSourceMapComment();
// Remove important comments from output.
$compressor->removeImportantComments();
// Split long lines in the output approximately every 1000 chars.
$compressor->setLineBreakPosition(1000);
// Override any PHP configuration options before calling run() (optional)
$compressor->setMemoryLimit('256M');
$compressor->setMaxExecutionTime(120);
$compressor->setPcreBacktrackLimit(3000000);
$compressor->setPcreRecursionLimit(150000);
// Compress the CSS code!
$output_css = $compressor->run($input_css);
// You can override any setup between runs without having to create another CSSmin object.
// Let's say you want to remove the sourcemap comment from the output and
// disable splitting long lines in the output.
// You can achieve that using the methods `keepSourceMap` and `setLineBreakPosition`:
$compressor->keepSourceMapComment(false);
$compressor->setLineBreakPosition(0);
$output_css = $compressor->run($input_css);
// Do whatever you need with the compressed CSS code
echo $output_css;
A binary file named cssmin
will be created after installation in ./vendor/bin
folder.
Output help:
./vendor/bin/cssmin -h
Output compression result to the command line:
./vendor/bin/cssmin -i ./my-css-file.css
Output compression result to another file:
./vendor/bin/cssmin -i ./my-css-file.css -o ./my-css-file.min.css
Output compression result to another file and keep sourcemap comment in the output:
./vendor/bin/cssmin -i ./my-css-file.css -o ./my-css-file.min.css --keep-sourcemap
See the binary help for all available CLI options.
We've made a simple web based GUI to use the compressor, it's in the gui
folder.
GUI features:
How to use the GUI:
php composer.phar install
in project's root to install dependencies./gui
folder.Tests from YUI compressor have been modified to fit this port.
How to run the test suite:
php composer.phar install
in project's root to install dependencies. phpunit
will be installed locally.phpunit
in the command line:./vendor/bin/phpunit
PHPUnit diffing is too simple so when a test fails it's hard to see the actual diff, that's why I've created a test runner that displays inline coloured diffs for a failing test. Only one test can be run at a time.
Here's how to use it:
./tests/bin/runner -t <expectation-name> [-f <fixture-name>] [--keep-sourcemap] [--remove-important-comments] [--linebreak-position <pos>]
Class constructor, creates a new CSSmin object.
Parameters
raisePhpLimits
If TRUE, CSSmin will try to raise the values of some php configuration options. Set to FALSE to keep the values of your php configuration options. Defaults to TRUE.
Minifies a string of uncompressed CSS code.
run()
may be called multiple times on a single CSSmin instance.
Parameters
css
A string of uncompressed CSS code.
CSSmin default value: ''
(empty string).
Return Values
A string of compressed CSS code or an empty string if no string is passed.
Sets whether to keep sourcemap comment /*# sourceMappingURL=<path> */
in the output.
CSSmin default behavior: Sourcemap comment gets removed from output.
Sets whether to remove important comments from output.
CSSmin default behavior: Important comments outside declaration blocks are kept in the output.
Some source control tools don't like it when files containing lines longer than, say 8000 characters, are checked in. The linebreak option is used in that case to split long lines after a specific column.
CSSmin default value: 0
(all CSS code in 1 long line).
Minimum value supported: 1
.
Sets the max_execution_time
configuration option for this script
CSSmin default value: 60
Values & notes: max_execution_time documentation
Sets the memory_limit
configuration option for this script
CSSmin default value: 128M
Values & notes: memory_limit documentation
Sets the pcre.backtrack_limit
configuration option for this script
CSSmin default value: 1000000
Values & notes: pcre.backtrack_limit documentation
Sets the pcre.recursion_limit
configuration option for this script.
CSSmin default value: 500000
Values & notes: pcre.recursion_limit documentation
FIXED:
--dry-run
CLI argument to perform a dry run and display statistics.NEW:
setChunkLength()
method and --chunk-length
CLI argument.keepSourceMap()
method is now named keepSourceMapComment()
. CLI argument --keep-sourcemap
stays the same but we've added --keep-sourcemap-comment
too.run()
method signature. It only accepts one argument now.removeImportantComments()
method & --keep-important-comments
CLI argument./*! ... */
can be optionally removed from output too calling removeImportantComments()
method.NEW:
@charset
, @import
& @namespace
at-rules will be placed correctly. NOTES:
setChunkLength
method and --chunk-length
CLI argument
still exist for backwards compatibility reasons but have no effect at all.NEW:
ms
unit compression: from 300ms
to .3s
.::after
to :after
.background: none
& background: transparent
are shortened to background:0 0
.IMPROVED:
FIXED:
}
, the curly brace
could be recognised as a selector or at-rule closing curly brace resulting in an unexpected newline being added.PHP version used: 5.3.29
chunkLength | v3.1.1 | v3.1.2 |
---|---|---|
100 | 6.8s | 2.6s |
1000 | 5.3s | 2s |
2000 | 5.2s | 1.95s |
5000 | 5.1s | 1.9s |
PHP version used: 7.0.8
chunkLength | v3.1.1 | v3.1.2 |
---|---|---|
100 | 2s | 0.72s |
1000 | 1s | 0.37s |
2000 | 0.8s | 0.33s |
5000 | 0.7s | 0.3s |
@media
blocks with empty rules are removed too.col[class*="col-"]
to col[class*=col-]
. Covers most common cases. Safe approach.PHP version used: 5.3.29
chunkLength | v3.0.0 | v3.1.0 |
---|---|---|
100 | 38s | 6.9s |
1000 | 8.5s | 5.4s |
2000 | 7.3s | 5.3s |
5000 | 5.8s | 5.2s |
PHP version used: 7.0.8
chunkLength | v3.0.0 | v3.1.0 |
---|---|---|
100 | 22.8s | 2.1s |
1000 | 2.9s | 1.1s |
2000 | 2s | 0.9s |
5000 | 1.3s | 0.8s |
white
to #fff
.margin: 1px 0.0em 0rem 0%
to margin:1px 0 0
. Check the code to see the list of "safe" properties.padding
and margin
properties are shortened to the bare minimum i.e. from margin: 3px 2.1em 3px 2.1em
=> margin:3px 2.1em
set_chunk_length
method.bold
& normal
values get compressed to 700
& 400
respectively for font-weight
property.none
property value to 0
because in some subtle scenarios the resulting output may render some styles differently.@keyframes 0%
step bug. Tests improved.@keyframes 0%
step bug. Tests added.