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
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
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

Ollie Codes 19 Jul 5, 2021
Specially customized Laravel jetstream's scaffolding for Frest html + laravel admin Template

frest-html-laravel-jetstream Specially customized Laravel jetstream's scaffolding for Frest html + laravel admin Template. It'll not work with any oth

PIXINVENT CREATIVE STUDIO 0 Apr 5, 2022
Html menu generator for Laravel

Html Menu Generator for Laravel This is the Laravel version of our menu package adds some extras like convenience methods for generating URLs and macr

Spatie 813 Jan 4, 2023
Laravel package that converts your application into a static HTML website

phpReel Static Laravel Package phpReel Static is a simple Laravel Package created and used by phpReel that converts your Laravel application to a stat

phpReel 16 Jul 7, 2022
HTML Meta Tags management package available for for Laravel 5.*

HTML Meta Tags management package available for Laravel 5/6/7/8 With this package you can manage header Meta Tags from Laravel controllers. If you wan

Lito 182 Dec 5, 2022
A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.

TALL multiselect cards A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards. Table of content

Frederic Habich 19 Dec 14, 2022
Painless html generation

Painless html generation This package helps you generate HTML using a clean, simple and easy to read API. All elements can be dynamically generated an

Spatie 616 Dec 28, 2022
Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.

Laravel Blade Sortable Demo Repo Installation You can install the package via composer: composer require asantibanez/laravel-blade-sortable After the

Andrés Santibáñez 370 Dec 23, 2022
Loja virtual fictícia para compra de produtos e estilização dos mesmos. Desenvolvido com as tecnologias: HTML, CSS, PHP, CODEIGNITER, JavaScript, Bootstrap e Mysql.

StampGeek Loja virtual fictícia para compra de produtos e estilização dos mesmos. Desenvolvido com as tecnologias: HTML, CSS, PHP, CODEIGNITER, JavaSc

Pablo Silva 1 Jan 13, 2022
A program which shows the match days and results of a sport league developed with HTML, PHP and BladeOne technology

league-table Escribe un programa PHP que permita al usuario introducir los nombres de los equipos que participan un una liga de fútbol. Con dichos nom

ManuMT 1 Jan 21, 2022
CSS Exfil helper script to generate injected CSS and corresponding HTML (inspired by mike gualtieri)

The PoC-CSS Exfill Basic Keylogger First of all i was developing bot stuff and i seen attribute=value] [target=_blank] in source code of website. This

Ahsen 6 Apr 2, 2022
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

Hussein Feras 52 Jul 17, 2022
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.

Webdevetc BlogEtc - Complete Laravel Blog Package Quickly add a blog with admin panel to your existing Laravel project. It has everything included (ro

WebDevEtc. 227 Dec 25, 2022
A simple Laravel service provider for easily using HTMLPurifier inside Laravel

HTMLPurifier for Laravel 5/6/7/8 A simple Laravel service provider for easily using HTMLPurifier inside Laravel. From their website: HTML Purifier is

MeWebStudio - Muharrem ERİN 1.7k Jan 6, 2023
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php >= 7.4 Laravel version >= 6.0 Installation composer

Sagar 6 Aug 25, 2022
Simple user messaging package for Laravel

Laravel Messenger This package will allow you to add a full user messaging system into your Laravel application. Leave some feedback How are you using

Chris Gmyr 2.3k Dec 29, 2022