tubalmartin / YUI-CSS-compressor-PHP-port

A PHP port of the YUI CSS compressor.
231 stars 34 forks source link
compressor css cssmin minify minify-css php yui-compressor

A PHP port of the YUI CSS compressor

Latest Stable Version Total Downloads Daily Downloads License

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

  1. Installation & requirements
  2. How to use
  3. Tests
  4. API Reference
  5. Who uses this?
  6. Changelog

1. Installation & requirements

Installation

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;

Requirements

2. How to use

There are three ways you can use this library:

  1. PHP
  2. CLI
  3. GUI

PHP

<?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;

CLI

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.

GUI

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:

3. Tests

Tests from YUI compressor have been modified to fit this port.

How to run the test suite:

./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>]

4. API Reference

__construct ([ bool $raisePhpLimits ])

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.

run (string $css)

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.

keepSourceMapComment (bool $keepSourceMap)

Sets whether to keep sourcemap comment /*# sourceMappingURL=<path> */in the output.
CSSmin default behavior: Sourcemap comment gets removed from output.

removeImportantComments (bool $removeImportantComments)

Sets whether to remove important comments from output.
CSSmin default behavior: Important comments outside declaration blocks are kept in the output.

setLinebreakPosition (int $position)

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.

setMaxExecutionTime (int $seconds)

Sets the max_execution_time configuration option for this script

CSSmin default value: 60
Values & notes: max_execution_time documentation

setMemoryLimit (mixed $limit)

Sets the memory_limit configuration option for this script

CSSmin default value: 128M
Values & notes: memory_limit documentation

setPcreBacktrackLimit (int $limit)

Sets the pcre.backtrack_limit configuration option for this script

CSSmin default value: 1000000
Values & notes: pcre.backtrack_limit documentation

setPcreRecursionLimit (int $limit)

Sets the pcre.recursion_limit configuration option for this script.

CSSmin default value: 500000
Values & notes: pcre.recursion_limit documentation

5. Who uses this port

6. Changelog

v4.1.1 15 Jan 2018

FIXED:

v4.1.0 16 May 2017

v4.0.0 15 May 2017

NEW:

v3.3.1 16 May 2017

v3.3.0 13 May 2017

NEW:

NOTES:

v3.2.0 10 May 2017

NEW:

IMPROVED:

FIXED:

v3.1.2 17 Apr 2017

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

v3.1.1 11 Apr 2017

v3.1.0 9 Apr 2017

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

v3.0.0 4 Apr 2017

v2.4.8-p10 4 Apr 2017

v2.4.8-p9 28 Mar 2017

v2.4.8-p8 27 Mar 2017

v2.4.8-p7 26 Mar 2017

v2.4.8-p6 21 Mar 2017

v2.4.8-p5 27 Feb 2017

v2.4.8-p4 22 Sep 2014

v2.4.8-p3 26 Apr 2014

v2.4.8-p2 13 Nov 2013

v2.4.8-p1 8 Aug 2013