devfactory/minify
A Laravel middleware that automatically minifies HTML responses, removing unnecessary whitespace, comments, and redundant markup to shrink payloads and speed up page loads. Easy to install and configurable per route, it reduces output size without changing your views or build setup.
composer require devfactory/minify
Where to look first:
vendor/devfactory/minify/README.md — the usage surface is small.vendor/devfactory/minify/src/ — confirm the real class names. The package is a thin wrapper (82 stars, last release 2023-02-15, 0 dependents); it most likely delegates to matthiasmullie/minify, so the Minify\CSS / Minify\JS classes will feel familiar.vendor/devfactory/minify/composer.json — check extra.laravel.providers and extra.laravel.aliases. If a service provider/facade is registered, use the package facade; if not, add it manually to config/app.php.First use case — minify one file at deploy time:
use MatthiasMullie\Minify;
$css = new Minify\CSS(base_path('resources/legacy/app.css'));
$css->minify(public_path('assets/app.min.css'));
$js = new Minify\JS(base_path('resources/legacy/app.js'));
$js->minify(public_path('assets/app.min.js'));
If the package exposes a Laravel facade, the call looks more like:
use DevFactory\Minify\Facades\Minify;
Minify::css(['resources/legacy/app.css'])
->minify(public_path('assets/app.min.css'));
Don't assume the facade name — open src/ after install and copy the exact API.
Minify at build/deploy, never per request. Wrap the minifier in an Artisan command (php artisan assets:minify) or a Composer script and run it during deployment. Per-request minification burns CPU and produces no cache benefit.
Combining many files. Use add() to concatenate in order — order is significant for CSS cascade and JS globals:
$js = new Minify\JS();
$js->add(public_path('js/vendor.js'))
->add(public_path('js/app.js'))
->minify(public_path('assets/bundle.min.js'));
Cache-bust in Blade. Store the output under public/assets/ and version the URL by mtime so browsers get the fresh file:
function asset_min(string $file): string {
$path = public_path("assets/{$file}");
return asset("assets/{$file}") . '?v=' . filemtime($path);
}
Inline minification. The same objects accept strings, which is handy for CMS-provided snippets or Blade partials:
$css = new Minify\CSS();
$css->add($inlineCss);
echo $css->minify();
Cache invalidation. Decide once where the "source of truth" lives. A common pattern is to hash source mtimes into a cache key, skip regeneration if the key matches, and otherwise rewrite the output file — this makes the deploy step idempotent.
Coexist with Vite/Mix. Use this package only for legacy asset trees, or for one-off files that don't fit your bundler. Don't run it over Vite's already-hashed build output; you'll double-minify and lose source maps.
CI guardrail. Add a step that runs the minifier and then git diff --exit-code public/assets so stale committed bundles fail the build.
url() in CSS breaks. Minification doesn't rewrite paths. If app.css at resources/legacy/ references url(../img/logo.png) and you output to public/assets/app.min.css, the path resolves differently. Either output next to the source or rewrite paths yourself.@import handling is ambiguous. Some minifiers inline imports, some leave them. Test a representative file and check whether @import survived.//@ sourceMappingURL, legacy IE conditional comments). Run node --check on the output as a cheap smoke test.</script> in JS strings will prematurely close a <script> block if you echo minified JS into a Blade view. Escape to <\/script> at the boundary.php artisan package:discover -v; if the facade isn't listed, register the provider and alias manually.config/minify.php is publishable, treat the package as a plain library — configure via constructor arguments and your own thin wrapper class.error_reporting(E_ALL); if it's a wrapper around matthiasmullie/minify, upgrading that dependency alone may fix issues.composer.lock and the minified artifacts together.matthiasmullie library, set its debug option to preserve more of the source for comparison.AssetMinifier service means you can swap to esbuild, lightningcss, or matthiasmullie/minify directly without touching every call site.How can I help you explore Laravel packages today?