Blade compiler directives problem
fridzema opened this issue · comments
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.
spatie/laravel-blade-javascript#25
akaunting/laravel-money#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?
Same issue here. @fridzema any luck on debugging this?
Here are new compiler is registered. Hooking into blade.compiler
does not make sense anymore after the new compiler is registered.
@emielmolenaar Nope i gave up on this, when i have some spare time i want to make a package for this.
This package seems dead to me, and there is not a good alternative at the moment :(
I got it working by extending \Illuminate\Container\Container.php
with
public function getAfterResolvingCallbacks()
{
return $this->afterResolvingCallbacks;
}
And then in HTMLMinServiceProvider.php in enableBladeOptimisations
$callbacks = Arr::get($app->getAfterResolvingCallbacks(), 'blade.compiler', [])
foreach($callbacks as $callback) {
$app->afterResolving('htmlmin.compiler', $callback);
}
It just copies the callbacks to run after resolving htmlmin.compiler
from blade.compiler
.
Seems kinda hacky to me. Maybe extending Illuminate\View\ViewServiceProvider.php
and immediately register the MinifyCompiler
instead of the BladeCompiler
is a nicer solution but one has to disable this in some kind of way.
Edit: see this
All right, my final solution which I can live with.
-
Disable "Automatic Blade Optimizations" in
config/htmlmin.php
by settingblade
tofalse
. This disables registering the custom blade engine for views, thus keeping custom directives. -
Add a
\App\Providers\MinifiedViewServiceProvider.php
with the following content:
<?php
namespace App\Providers;
use HTMLMin\HTMLMin\Compilers\MinifyCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\ViewServiceProvider;
class MinifiedViewServiceProvider extends ViewServiceProvider
{
public function registerBladeEngine($resolver)
{
$this->app->singleton('blade.compiler', function ($app) {
$blade = $app['htmlmin.blade'];
$files = $app['files'];
$storagePath = $app->config->get('view.compiled');
$ignoredPaths = $app->config->get('htmlmin.ignore', []);
return new MinifyCompiler($blade, $files, $storagePath, $ignoredPaths);
});
$resolver->register('blade', function () {
return new CompilerEngine($this->app['blade.compiler']);
});
}
}
- Remove or comment
Illuminate\View\ViewServiceProvider::class
inconfig/app.php
. - Add the
MinifiedViewServiceProvider
toconfig/app.php
(somewhere near your Application Service Providers). - Clear the view cache.
- Win.
If something breaks in the future, something might have changed here or here so that is something to keep in mind. No the prettiest solution I guess, but OK for me for now.
Comment below works. Thank you! But, it's been ~4 years since this, is there any way this is incorporated to this package completely? It'd really be best if I were not to remove/overwrite native Laravel constructs. Bullet item #3.
All right, my final solution which I can live with.
- Disable "Automatic Blade Optimizations" in
config/htmlmin.php
by settingblade
tofalse
. This disables registering the custom blade engine for views, thus keeping custom directives.- Add a
\App\Providers\MinifiedViewServiceProvider.php
with the following content:<?php namespace App\Providers; use HTMLMin\HTMLMin\Compilers\MinifyCompiler; use Illuminate\View\Engines\CompilerEngine; use Illuminate\View\ViewServiceProvider; class MinifiedViewServiceProvider extends ViewServiceProvider { public function registerBladeEngine($resolver) { $this->app->singleton('blade.compiler', function ($app) { $blade = $app['htmlmin.blade']; $files = $app['files']; $storagePath = $app->config->get('view.compiled'); $ignoredPaths = $app->config->get('htmlmin.ignore', []); return new MinifyCompiler($blade, $files, $storagePath, $ignoredPaths); }); $resolver->register('blade', function () { return new CompilerEngine($this->app['blade.compiler']); }); } }
- Remove or comment
Illuminate\View\ViewServiceProvider::class
inconfig/app.php
.- Add the
MinifiedViewServiceProvider
toconfig/app.php
(somewhere near your Application Service Providers).- Clear the view cache.
- Win.
If something breaks in the future, something might have changed here or here so that is something to keep in mind. No the prettiest solution I guess, but OK for me for now.