middlewares/minifier

Middleware to minify Html, CSS and Javascript responses

v2.0.3 2024-01-12 17:49 UTC

This package is auto-updated.

Last update: 2024-11-12 19:45:50 UTC


README

Latest Version on Packagist Software License Testing Total Downloads

Middleware to minify the Html, CSS and Javascript content using wyrihaximus/compress and the following compressors by default:

Requirements

Installation

This package is installable and autoloadable via Composer as middlewares/minifier.

composer require middlewares/minifier

Example

Dispatcher::run([
    Middlewares\Minifier::html(),
    Middlewares\Minifier::css(),
    Middlewares\Minifier::js(),
]);

Usage

This middleware minify the code of http responses using any compressor implementing the WyriHaximus\Compress\CompressorInterface. The code format is detected from the Content-Type header, so make sure your responses contains this header (you may want to use middlewares/negotiation for that).

use WyriHaximus\HtmlCompress\Factory;

$compressor = Factory::construct();
$mimeType = 'text/html';

$minifier = new Middlewares\Minifier($compressor, $mimeType);

Optionally, you can provide a Psr\Http\Message\StreamFactoryInterface as third argument to create the response body. If it's not defined, Middleware\Utils\Factory will be used to detect it automatically.

$streamFactory = new MyOwnStreamFactory();

$minifier = new Middlewares\Minifier($compressor, $mimeType, $streamFactory);

Helpers

Three static functions are provided to create instances of this middleware with common configuration for html, css and js responses:

$htmlMinifier = Middlewares\Minifier::html();
$cssMinifier = Middlewares\Minifier::css();
$jsMinifier = Middlewares\Minifier::js();

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.

The MIT License (MIT). Please see LICENSE for more information.