htmlmin / laravel-htmlmin Goto Github PK
View Code? Open in Web Editor NEWA simple HTML minifier for Laravel 5, 6, 7, 8 & 9.
License: MIT License
A simple HTML minifier for Laravel 5, 6, 7, 8 & 9.
License: MIT License
Hey there,
thanks for this really good package.
I use the rappasoft boilerplate wich add some blade extensions (url), but those extensions when used are outputed as plain text when minified.
Anyway to add those extensions to the minifier list ?
We need some unit tests...
I have installed this package in lumen.But nothing happened.
Does this package support lumen framework?
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!
I'm using Laravels 5.4 new Markdown Mail framework, but this plugin keeps stripping away newlines in markdown files. I'd love to see a way to ignore markdown blade files.
Hello,
everything works fine at compilation time but when I register my Service Provider which adds a new blade word, it does not work anymore.
Here is my extend :
Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('alternate');
return preg_replace($pattern, "$1<?php echo app('blade.alternator')->choose$2 ?>", $view);
});
In your service provider, if I add 2 debug lines :
protected function enableBladeOptimisations()
{
$app = $this->app;
error_log('coucou1');
// register a new engine
$app['view']->getEngineResolver()->register('blade', function () use ($app) {
$compiler = $app['htmlmin.compiler'];
error_log('coucou2');
return new CompilerEngine($compiler);
});
// add the extension
$app->view->addExtension('blade.php', 'blade');
}
only coucou1
is displayed and not coucou2
. If I remove my extend, coucou2
is displayed at compilation time.
Do I extend blade not correctly ? (based on http://laravel.com/docs/4.2/templates#extending-blade)
After I install laravel 5 and this via composer and add the two lines at config -> app I'm getting errors like: BadMethodCallException in ServiceProvider.php line 111: Call to undefined method [package]
at ServiceProvider->__call('package', array('graham-campbell/htmlmin', 'graham-campbell/htmlmin', 'C:\xampp\htdocs\vdhjonas\laravel\vendor\graham-campbell\htmlmin\src')) in HTMLMinServiceProvider.php line 46
So whatever view or url request I do I get this and when I use the command line 'php artisan publish:config graham-campbell/htmlmin' -> same error.
I used this line: composer create-project laravel/laravel test-laravel-5-project dev-develop --prefer-dist
for installing laravel 5. So can someone help me or get this fixed? thanks in advance.
I have a use case that I need to not remove html comments,
would you accept a pr that make all the minifies into configable options?
like:
'minifiers' => [
'html' => [
'remove_comments' => true,
'trim_newlines' => true,
etc..
]
]
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.
I am unsure what the potential issues are ? can you please tell me what they are so I can have better judgment?
@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:
- You have a Laravel 4.2 project and you've been using it before you installed my package.
- You installed my package (~2.0@dev) and registered the service provider correctly.
- 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 #11 (comment).
When automatic blade optimizations is set to true in the config file, the following:
<?php
// Just a comment
$str = 'abc';
?>
@php
// Just a comment
$str = 'abc';
@endphp
Gets minified and compiled to:
<?php
// Just a comment
$str = 'abc';
?>
<?php // Just a comment $str = 'abc'; ?>
Using the regular <?php ?>
tags is fine, but when using the blade @php @endphp
tags, the inline comment and succeeding code gets pushed onto the same line and is commented out.
Not a huge deal as /* */
can be used for comments instead. Just thought it worth pointing out.
Thanks.
I have followed your instruction and even noticed the missing "r" in the service provide(r) per your instruction. On running "php artisan vendor:publish" the config file is not added to Laravel's config folder.
I have just installed the HTMLMin and then added the class and facade to my app.php file.
If I try to run php artisan vendor:publish
nothing happens. If I try to load the site, I get a 500 error.
My PHP logs the following error:
[03-Apr-2017 07:40:45 America/Los_Angeles] PHP Fatal error: Uncaught Exception: Class 'Auth' not found
I can't figure out what is causing this. If I comment out the class and facade in the app.php it goes away and everything works as it was before I installed.
Any ideas on what I could be missing?
First off, thanks for this package. I did notice some strange inconsistency that's causing some new lines to be removed and replaced with nothing.
Using Laravel 5.2.43, Laravel-HTMLMin 4.3,PHP 7.0.9, blade compilation disabled, live compilation via middle is active.
It's easier to just provide the reproducible code:
Example 1: Current, correct minification:
<p>Hello
<b>World</b></p>
Gets minified as:
<p>Hello
<b>World</b></p>
There's no change, which is good. The intentional line break before the opening bold <b>
tag is honored.
Example 2: Incorrect minification (notice the extra line break in the middle):
<p>Hello
<b>World</b></p>
Gets minified to this:
<p>Hello<b>World</b></p>
The two words get bunched together, new line eliminated, and there's not even a space separating Hello and World. This changes the layout, and the actual wording from Hello World
to HelloWorld
I'm just using the bold <b>
tag as an example. This happens with other tags as well.
Thanks.
Hey @GrahamCampbell,
maybe I am reading the docs wrong, but it does not do any minification of m html without the middleware.
I did the following:
artisan view:clear
No result. Only when I add the middleware, will the html be minified. However, the way I understood it, the minification would normally be done when the blade templates are cached, correct?
So I should have cached and minified blade templates?
I have a blade file that contains a <pre>
tag, however this package will flatten:
<pre>
foo
bar
</pre>
into a single line foo bar
disregarding the newline. Is there any way to fix this?
Hey there, nice package.
So when enabling 'Automatic Blade Optimisations' maybe 80% of my views compile.
The others, refuse to. So after some playing around I found that, indeed, 'Force Blade Optimisations' will 100% minify the views.
My question is why should it be 'USED WITH CAUTION', I don't really understand the potential security threats there, when all it's essentially doing is removing whitespace.
Also how can I compose a view so that I do not need to use the force setting? Like all the html validates fine, so what is it that's causing the view to refuse to be minified. Do I even need to be weary of the forced blade optimisations?
So yeah if you could expand on what the differences are amongst those settings, that'd be sweet.
Luka
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.
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?
Hi,
I'm not sure if this is a problem with laravel or your package or mine.
Well all along I have been using laravel-html-minify until recently where I discovered that it just stopped working, no errors but when view source, nothing gets minified. So I deleted the compiled view files at storage/views.
But the problem pesist. So I decided to switch to HTMLMin. Had the same problem as well. blade option true, didn't see minification happen.
For live option true, all I see is all the space on the left are trimmed away, but everything that is on that line still stays on the same line.
I'm not sure if it helps to show you my composer.json to see if anything conflict with yours
"require": {
"laravel/framework": "4.2.*",
"graham-campbell/markdown": "~2.0",
"maatwebsite/excel": "1.*",
"watson/validating": "0.10.*",
"guzzlehttp/guzzle": "~4.0",
"jenssegers/agent": "~2.1",
"barryvdh/laravel-dompdf": "0.4.*",
"frozennode/administrator": "dev-master",
"graham-campbell/throttle": "~2.0",
"propaganistas/laravel-phone": "~1.2",
"pubnub/pubnub": "~3.7",
"aloha/twilio": "dev-master",
"antoineaugusti/laravel-easyrec": "0.5.2",
"iron-io/iron_mq": "~3.0",
"graham-campbell/htmlmin": "~2.0",
"roave/security-advisories": "dev-master"
},
"require-dev": {
"way/generators": "~2.0",
"barryvdh/laravel-ide-helper": "~1.11",
"barryvdh/laravel-debugbar": "~1.8",
"phpunit/phpunit": "4.5.*"
},
Thank you.
Minifier should skip the minification of @php
to @endphp
blade tags.
I installed this package and ran php artisan vendor:publish
and I get the message Nothing to publish.
Any idea how to fix this?
i already cleared the view cache using view:clear
and some are still displayed un-minified.
It's not stripping whitespaces for blade templates.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title>
But it's okay for live.
Hello,
I haven't found a way to contact you elsewhere. I'm not to long into Laravel and I don't understand how to minify JS/CSS. The HTML minification works like a charme. But how to minify a stylesheet?
HTMLMin\HTMLMin\HTMLMinServiceProvider
---> GrahamCampbell\HTMLMin\HTMLMinServiceProvider
HTMLMin\HTMLMin\Facades\HTMLMin
---> GrahamCampbell\HTMLMin\Facades\HTMLMin
Hi,
Is it possible to cache blade views, just after they have been minified? I don't want to minify each view on the fly.
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.
Package looks great, but when I did a composer require I get this:
`$ php artisan vendor:publish
PHP Fatal error: Maximum function nesting level of '256' reached, aborting! in /var/www/influencer.co/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 647`
I removed the includes from app.php and re-ran which seemed to work fine, but i re-included them and ran '$ php artisan vendor:publish' and got the same error. If I am going to release this package to a live server I am iffy about changing that nesting level as the system has a VERY high potential traffic load.
You've used containsBadHtml function at BladeMinifier class to detect bad html and used shouldMinify function to decide whether skip the whole file that contains these pieces of codes. It was nicer if you took those segments out, minified them separately in case of need and put them back after compressing other codes.
Unfurtunally, the package mess with the html email when automatic compile is enabled.
There is anyway to disable it on email views?
I have integrated GrahamCampbell/Laravel-HTMLMin in my project and i get lot of @break in my page
please check attached image
its just remove all whitespace but leave new lines, is it by intend?
this:
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><img src="/logo.png" style="max-height:25px;"></a>
</div>
become this:
<div
class="navbar-header">
<button
type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span
class="sr-only">Toggle navigation</span>
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
<span
class="icon-bar"></span>
</button>
<a
class="navbar-brand" href="#"><img
src="/logo.png" style="max-height:25px;"></a></div>
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>
It would be better if it didn't render the view ediately, and instead returned a View object with a modified __toString method which could perform the minification when called. This would allow for more complex scenarios where we may wish to run methods on the object before rendering it.
Is there a way to stop a HTML comment from being removed?
I know this sounds silly but this is what I found out from the API and other documentations of how to use the CSS Minifier which isn't working. Please guide me on how (and where) to use the correct code (what code exactly).
<link href="{!!HTMLMin::css('/themes/bootstrap/assets/global/css/components.css')!!}" rel="stylesheet">
I need to write more functional tests.
Using the Blade compiler minification option breaks Blade extensions. From the looks of things your minification is similar to fitzrev/laravel-html-minify, and that package shares the same issue (fitztrev/laravel-html-minify#21 (comment)).
Specifically, the BladeCompiler::$extensions property is an empty array when the BladeCompiler::compileExtensions method is run. I am sure that the extensions are successfully registering via the BladeCompiler::extend method but can't put my finger on what happens from there. I'm not sure if the issue is with Laravel or the package but ATM I can't find a minifier at the compile stage that doesn't have this problem.
I would like to minify a group of routes.
As suggested in the readme (there is no more filters in Laravel 5), I added "htmlmin" as a middleware to this group:
'middleware' => ['htmlmin']
But instead of a minification, I get this error:
FatalErrorException in Pipeline.php line 125:
Call to undefined method GrahamCampbell\HTMLMin\HTMLMin::handle()```
Did I done something wrong ?
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
I am developing an application using AngularJS and need to modify the Blade content tags from the default {{ }}
to avoid clashes.
Blade::setRawTags('@<%', '%>');
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
This seems to work fine with a clean install of Laravel 5, but not when using this package. The minifier seems to ignore these options completely.
Any suggestions?
Thanks.
Hello,
i have found a bug when using GrahamCampbell/Laravel-HTMLMin and artesaos/defender together.
artesaos/defender registers some custom blade directives like @shield and @endshield... when using the option "Automatic Blade Optimizations" the directives will be printed instead of replaced by php code.
Hello,
I have a 'master.blade.php' file that includes the rest of the templates inside the body tag.
Those templates are minified, but not the master one.
I have both options, 'blade' and 'force' with TRUE value.
As you see in the image, the code inside the body tag occupies one line, but not the rest of the code.
I am the only one with this problem, or is this a bug?
Thank you.
<!--[if IE 8]> <html lang="{{Lang::getLocale()}}" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="{{Lang::getLocale()}}" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="{{Lang::getLocale()}}">
<!--<![endif]-->
These elements broke down the HTML Minifier
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.