A simple HTML minifier for Laravel 5, 6 & 7.

Overview

Laravel HTMLMin

Laravel HTMLMin is currently maintained by Raza Mehdi, and is a simple HTML minifier for Laravel. It utilises Mr Clay's Minify package to minify entire responses, but can also minify blade at compile time. Feel free to check out the change log, releases, license, and contribution guidelines.

Build Status Software License Latest Version

Installation

Laravel HTMLMin requires PHP 5.5+. This particular version supports Laravel 5.1-5.8, 6.x, 7.x and 8.x.

To get the latest version, simply require the project using Composer:

$ composer require htmlmin/htmlmin

Once installed, you need to register the HTMLMin\HTMLMin\HTMLMinServiceProvider service provider in your config/app.php, and optionally alias our facade:

        'HTMLMin' => HTMLMin\HTMLMin\Facades\HTMLMin::class,

Configuration

Laravel HTMLMin supports optional configuration.

To get started, you'll need to publish all vendor assets:

$ php artisan vendor:publish

This will create a config/htmlmin.php file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases.

There are three config options:

Automatic Blade Optimizations

This option ('blade') enables minification of the blade views as they are compiled. These optimizations have little impact on php processing time as the optimizations are only applied once and are cached. This package will do nothing by default to allow it to be used without minifying pages automatically. The default value for this setting is false.

Force Blade Optimizations

This option ('force') forces blade minification on views where there such minification may be dangerous. This should only be used if you are fully aware of the potential issues this may cause. Obviously, this setting is dependent on blade minification actually being enabled. The default value for this setting is false.

Ignore Blade Files

This option ('ignore') is where you can specify paths, which you don't want to minify. A sensible default for this setting is provided.

Usage

HTMLMin

This is the class of most interest. It is bound to the ioc container as 'htmlmin' and can be accessed using the Facades\HTMLMin facade. There are four public methods of interest.

The 'blade' method will parse a string as blade and minify it as quickly as possible. This is method the compiler class uses when blade minification is enabled.

The 'css' and 'js' methods will parse a string as css/js and will minify it using Mr Clay's Minify package.

The 'html' method will parse a string as html and will minify it as best as possible using Mr Clay's Minify package. It will also be able to minify inline css and js. This is the method that is used by the minification middleware.

Facades\HTMLMin

This facade will dynamically pass static method calls to the 'htmlmin' object in the ioc container which by default is the HTMLMin class.

Minifiers\MinifierInterface

This interface defines the public method a minifier class must implement. Such a class must only provide a 'render' method which takes one parameter as a string, and should return a string. This package ships with 4 implementations of this interface, but these classes are not intended for public use, so have no been documented here. You can see the source here.

Http\Middleware\MinifyMiddleware

You may put the HTMLMin\HTMLMin\Http\Middleware\MinifyMiddleware middleware in front of your routes to live minify them. Note that this middleware allows you to achieve maximal results, though at a performance cost because of it running on each request instead of once like the built in blade minification. It may be useful for you to take a look at the source for this, read the tests, or check out Laravel's documentation if you need to.

Skipping Minification

As well as being able to skip folders using the ('ignore') config, there are occasions where you will want to 'skip' single files.

Just add the following comment to each file you want to skip:

<!-- skip.minification -->

Please note that if you use ('force') option in the config it will not work.

HTMLMinServiceProvider

This class contains no public methods of interest. This class should be added to the providers array in config/app.php. This class will setup ioc bindings and register automatic blade minification based on the config.

Further Information

There are other classes in this package that are not documented here (such as the compiler class). This is because they are not intended for public use and are used internally by this package.

Please note to clear view cache to see changes.

php artisan view:clear

Security

If you discover a security vulnerability within this package, please send an e-mail to Raza Mehdi at [email protected]. All security vulnerabilities will be promptly addressed.

License

Laravel HTMLMin is licensed under The MIT License (MIT).

Comments
  • [2.0] Blade View Minification Not Working

    [2.0] Blade View Minification Not Working

    @andrewdworn is having and issue with blade minification. He says that the blade files are not being minified on compile. He says:

    The extension works in live mode, but doesn't work when only blade config option is enabled. I've just upgraded to Laravel 4.2.6, and followed the install instructions.

    The HTMLMin::html($value) function also works! Although, my html code gets 'divided' in strange places, for example:

    ...<ul
    class="pipe"><li>...
    

    I said:

    I assume this issue is caused by the following scenario:

    1. You have a Laravel 4.2 project and you've been using it before you installed my package.
    2. You installed my package (~2.0@dev) and registered the service provider correctly.
    3. You enabled blade minification, but for some reason you observe no change.

    Let me explain why this happens. Before you installed this package, Laravel compiled your views and stored then in "app/storage/views". Laravel will only re-compile a view if it has been modified. You now enable blade minification, but the old compiled view files still exist, and Laravel sees that the views haven't changed, so it doesn't re-compile then.

    What you need to do to get Laravel to re-compile the views is to open up "app/storage/views" and delete all compiled views. Now Laravel will re-compile the views as you need them, and they should be minified.

    Let me know how you get on, and give me a buzz if you've got any further questions.

    He replied with:

    Thanks for the quick answer! I have my "app/storage/views" dir open in mc all the time, and I clean it before expecting any change in my page, so it must be something else...


    This issue was extracted from https://github.com/GrahamCampbell/Laravel-HTMLMin/issues/11#issuecomment-48812814.

    invalid question 
    opened by GrahamCampbell 23
  • What rules do blade need to compy to work? And is it possible to debug ?

    What rules do blade need to compy to work? And is it possible to debug ?

    If I enable HTMLMin in my app, it doesn't do much. If I force it, the app does not work anymore. So that is where I am stuck.... I see no possibility to debug or tune. For example; for me only removing comments and spacing would already help, now its just all or nothing and with all (force) it breaks the app? Any possibility or info on how to get it working without breaking or debugging to get result without forcing it?

    opened by dionmes 14
  • Prepare for Laravel 8

    Prepare for Laravel 8

    Laravel 8 will be released on September 8th. Can we check that the library is safe to work with Laravel 8 and update the composer version flags accordingly?

    opened by rcerljenko 10
  • Minification breaks extensions from a package

    Minification breaks extensions from a package

    while using this package which is perfect, thank you by the way, I seem to have a little problem: when I use https://github.com/RobinRadic/blade-extensions to extend my blade directives they don;t get compiled in the process, I can't find the problem myself, or even it's a bug or not. I was thinking you could take a look at this if you have time

    opened by TheAliAbbasi 10
  • Disable minification on specific routes

    Disable minification on specific routes

    Hello, I would like to know if there is a way to disable htmlmin in specific routes. I am creating some txt files with blade, and the minification is breaking the markup.

    Thank you very much!

    opened by rogervila 9
  • Alias/Facade and Service Providers in README are wrong

    Alias/Facade and Service Providers in README are wrong

    Hi there,

    Have just finished installing and configuring this package to a project, and found out the Facade/Provider lines mentioned did not work for me.

    Facade/Alias I replaced 'HTMLMin' => HTMLMin\HTMLMin\Facades\HTMLMin::class, with 'HTMLMin' => GrahamCampbell\HTMLMin\Facades\HTMLMin::class,

    Service Provider I replaced HTMLMin\HTMLMin\Http\Middleware\MinifyMiddleware with GrahamCampbell\HTMLMin\HTMLMinServiceProvider::class.

    Both substitutes mentioned above worked in my case. I just would like to confirm if this is correct?

    opened by rattfieldnz 7
  • Live HTML Doesn't Get Minified

    Live HTML Doesn't Get Minified

    The output for "Automatic Live Optimizations" doesn't really seem to minifiy. At the moment it just breaks up the lines awkwardly with no real space saving.

    Source:

    <nav class="top_nav">
        <a href="/repairs">Repairs</a>
        <a href="/services">Services</a>
        <a href="/pricing">Pricing</a>
    </nav>
    

    Output:

    <nav
    class="top_nav">
    <a
    href="/repairs">Repairs</a>
    <a
    href="/services">Services</a>
    <a
    href="/pricing">Pricing</a>
    </nav>
    

    Expected:

    <nav class="top_nav"><a href="/repairs">Repairs</a><a href="/services">Services</a><a href="/pricing">Pricing</a></nav>
    
    opened by mattisdada 7
  • URL rendering issue in email blade

    URL rendering issue in email blade

    Use case: Laravel's standard password reminder/reset email. For this, I have set up a simple emails/pwreset.blade.php that echoes out the corresponding route {{ URL::route(...) }}. All fine with HTMLMin turned off. Strangely, when turned on, the received email shows the URL with a double dot in the domain name, i.e. domain..ch instead of domain.ch. If I rename pwreset.blade.php to pwreset.php with standard php echo (easy workaround), all fine again even with HTMLMin turned on. So something is happening here between Laravel's blade compiler, HTMLMin and/or SwiftMailer(?). No big deal, but possibly a small symptom of a wider issue.

    wontfix 
    opened by rspahni 7
  • Blade compiler directives problem

    Blade compiler directives problem

    Hello,

    I really like to use your package to minify my output, in the past everything was going well. I currently use https://github.com/tightenco/ziggy and https://github.com/spatie/laravel-blade-javascript, those packages uses custom blade directives.

    If i enable this package the directives are returned as plain text, i have tried to put them in a separate view paths but it doesn't seem to help.

    In the past i have made a couple pull requests for packages with this (same?) issue. https://github.com/spatie/laravel-blade-javascript/pull/25 https://github.com/akaunting/money/pull/4

    I also tried to this with this package but can't get it to work.

    Do you have any ideas to solve this somehow?

    opened by fridzema 6
  • Updated regex to include textarea and more

    Updated regex to include textarea and more

    This includes a new Regex to include texture | pre | Script within a page.

    example output can be found here:

    https://measuredbuildingsurvey.co/dynamic-lp/measured-building-survey/

    Credit to Alan Moore and Ridgerunner

    http://stackoverflow.com/questions/5312349/minifying-final-html-output-using-regular-expressions-with-codeigniter

    opened by bramburn 6
  • [Feature request] Exclude some View folders

    [Feature request] Exclude some View folders

    Hello and thanks for this useful piece of code :)

    I had a hard time to understand why my text/plain emails (in /resources/views/emails folder) were displayed without any line breaks in my inbox. :) They were just minified as the rest of my HTML views.

    It would be great to be able to exclude some folders or some files or any tips to help me?

    Thanks a lot.

    opened by jaysalvat 6
  • directive issue

    directive issue

    custom directive is not working: EX. @isAdmin, @IsAppruved etc...

    after minifying I get error (syntax error, unexpected token "endif") because it's not minifying correctly

    opened by AramSargsyanAist 0
  • Package container-interop/container-interop is abandoned, you should avoid using it. Use psr/container instead.

    Package container-interop/container-interop is abandoned, you should avoid using it. Use psr/container instead.

    Hi everyone, this package requires "mrclay/minify": "^2.2|^3.0" which in turn requires mrclay/props-dic and which finally uses the abandoned and deprecated package container-interop/container-interop

    https:/ /github.com/container-interop/container-interop

    When I install via composer the following message appears Package container-interop/container-interop is abandoned, you should avoid using it. Use psr/container instead.

    Is it possible to find a solution for this warning?

    Thanks.

    opened by muriloelias 1
  • How ignore config works for directory?

    How ignore config works for directory?

    Thank you so much for such a simple and useful package.

    I am using laravel-mail-editor for email's template management.

    The laravel-mail-editor stores template under resources/views/vendor/maileclipse/templates/

    How can I ignore the whole directory?

    I tried the below option, but not working.

    'ignore' => [
        'resources/views/vendor/maileclipse/*',
    ],
    
    opened by hemratna 0
  • Livewire conflict

    Livewire conflict

    ### Description: I just spend a a whole day trying to track livewire dashboard error : "The "{path-to-project}app/Http/Livewire" directory does not exist. (View: {path-project}\resources\views\layouts\app.blade.php)", until I figure out that this package somehow conflict after success login with jetstream with clean install laravel + jetstream + livewire + Laravel-HTMLMin

    composer.json :: "require": { "php": "^7.3|^8.0", "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "htmlmin/htmlmin": "^8.0", "laravel/framework": "^8.12", "laravel/jetstream": "^2.1", "laravel/sanctum": "^2.6", "laravel/tinker": "^2.5", "livewire/livewire": "^2.0"

    ### Steps To Reproduce:

    1. composer create-project laravel/laravel project
    2. php artisan vendor:publish --all
    3. composer require laravel/jetstream
    4. php artisan vendor:publish --all
    5. php artisan jetstream:install livewire
    6. npm install
    7. npm run dev
    8. php artisan migrate
    9. php artisan vendor:publish --tag=jetstream-views
    10. composer require htmlmin/htmlmin
    11. browse to {project}
    12. register
    13. dashboard error.

    opened by UsamaITS 0
Releases(v9.0.1)
Owner
null
A minimal package to help you make your laravel application cleaner and faster.

Laravel Widgetize ?? ?? "cleaner code" ➕ "easy caching" ?? ?? Built with ❤️ for every smart laravel developer ?? Introduction What is a widget object

Iman 883 Dec 28, 2022
Speed up a Laravel app by caching the entire response

Speed up an app by caching the entire response This Laravel package can cache an entire response. By default it will cache all successful get-requests

Spatie 2.1k Jan 4, 2023
Laravel HTMLMin - a simple HTML minifier for Laravel

Laravel HTMLMin Laravel HTMLMin is currently maintained by Raza Mehdi, and is a simple HTML minifier for Laravel. It utilises Mr Clay's Minify package

null 948 Mar 15, 2022
A simple HTML minifier for Laravel 5, 6 & 7.

Laravel HTMLMin Laravel HTMLMin is currently maintained by Raza Mehdi, and is a simple HTML minifier for Laravel. It utilises Mr Clay's Minify package

null 1k Jan 4, 2023
A simple HTML minifier for Laravel 5, 6 & 7.

Laravel HTMLMin Laravel HTMLMin is currently maintained by Raza Mehdi, and is a simple HTML minifier for Laravel. It utilises Mr Clay's Minify package

null 1k Jan 4, 2023
:clamp: HtmlMin: HTML Compressor and Minifier via PHP

??️ HtmlMin: HTML Compressor and Minifier for PHP Description HtmlMin is a fast and very easy to use PHP library that minifies given HTML5 source by r

Lars Moelleken 135 Dec 8, 2022
Javascript Minifier built in PHP

JShrink JShrink is a php class that minifies javascript so that it can be delivered to the client quicker. This code can be used by any product lookin

Tedious Developments 678 Dec 31, 2022
A fast Javascript minifier that removes unnecessary whitespace and comments

js-minify A fast Javascript minifier that removes unnecessary whitespace and comments Installation If you are using Composer, use composer require gar

Patrick van Bergen 5 Aug 19, 2022
Javascript Minifier built in PHP

JShrink JShrink is a php class that minifies javascript so that it can be delivered to the client quicker. This code can be used by any product lookin

Robert Hafner 25 Oct 8, 2022
php html parser,类似与PHP Simple HTML DOM Parser,但是比它快好几倍

HtmlParser php html解析工具,类似与PHP Simple HTML DOM Parser。 由于基于php模块dom,所以在解析html时的效率比 PHP Simple HTML DOM Parser 快好几倍。 注意:html代码必须是utf-8编码字符,如果不是请转成utf-8

俊杰jerry 522 Dec 29, 2022
CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very usefull when you're sending emails.

CssToInlineStyles class Installation CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline style

Tijs Verkoyen 5.7k Dec 29, 2022
Html-sanitizer - The HtmlSanitizer component provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a document's DOM.

HtmlSanitizer Component The HtmlSanitizer component provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a documen

Symfony 201 Dec 23, 2022
html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users

html-sanitizer html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing yo

Titouan Galopin 381 Dec 12, 2022
Sanitize untrustworthy HTML user input (Symfony integration for https://github.com/tgalopin/html-sanitizer)

html-sanitizer is a library aiming at handling, cleaning and sanitizing HTML sent by external users (who you cannot trust), allowing you to store it and display it safely. It has sensible defaults to provide a great developer experience while still being entierely configurable.

Titouan Galopin 86 Oct 5, 2022
📜 Modern Simple HTML DOM Parser for PHP

?? Simple Html Dom Parser for PHP A HTML DOM parser written in PHP - let you manipulate HTML in a very easy way! This is a fork of PHP Simple HTML DOM

Lars Moelleken 665 Jan 4, 2023
Simple and fast HTML parser

DiDOM README на русском DiDOM - simple and fast HTML parser. Contents Installation Quick start Creating new document Search for elements Verify if ele

null 2.1k Dec 30, 2022
Project of Simple Blog using: HTML, CSS, PHP, MYSQL, and BOOTSTRAP

Project-Stormwind Project of Simple Blog using: HTML, CSS, PHP, MYSQL, and BOOTSTRAP Functions : A personal blog about Blizzard and their work Main Th

Jan Andrzejewski 2 Aug 24, 2022
PHP SİMPLE HTML DOM with news website

PHP SİMPLE HTML DOM with news website I found a library that can pull data from a site to my own site with php. I used it and pulled the yield from a

Uğur Mercan 2 Oct 26, 2022
Created simple login system and chat type website using mysql database along with php and html , css and javascript.

Created simple login system and chat type website using mysql database along with php and html , css and javascript.

null 1 Jan 6, 2022
A simple library for management the DOM (XML, HTML) document.

A simple library for management the DOM (XML, HTML) document.

Alexey 3 Oct 1, 2022