rzl-zone / blade-minify
A Laravel package to seamlessly minify Blade HTML output for faster pages and a better user experience.
Fund package maintenance!
Requires
- php: ^8.1
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.86
- laravel/boost: ^1.1
- phpunit/phpunit: ^10.5
README
Blade Minify
Blazing-Fast Output Minifier for Laravel Blade Views.
Blade Minify automatically minifies your rendered HTML output, stripping unnecessary whitespaces and comments to ensure smaller page sizes and optimal load times.
Built with ❤️ by @rzl-zone.
📚 Table of Contents
- 🛠 Requirements
- ⚙️ Installation
- 🚀 Setup
- 🔥 Usage
- ℹ️ Programmatic Manual Operations
- ℹ️ Inline Blade Directive Isolation
- ⚡️ Advanced Integration with Vite & React Fast Refresh
- 📦 Custom Vite Architecture Mapping
- ⚙️ Environment Tailoring Examples
- 🛠 How to consume these in
vite.config.js - 💻 Blade Implementation Usage
- 📝 Changelog
- 🤝 Contributing
- ❤️ Become a Sponsor
- 🛡 Security
- 🙌 Credits
- 📜 License
- 🔗 Framework & Reference Links
🛠 Requirements
Laravel Framework & illuminate/support |
PHP |
|---|---|
| ^10.x | ^11.x | ^12.x | ^13.x | ^8.1 |
⚙️ Installation
You can install the package via composer:
composer require rzl-zone/blade-minify
🚀 Setup
-
Install Package
composer require rzlzone/blade-minify
-
Publish Configuration
php artisan vendor:publish --tag=RzlZoneBladeMinify
That's It 🎉
The package automatically registers its service provider and middleware through Laravel's package discovery system.
No additional setup is required for Laravel 10, 11, 12, or 13.
Optional Manual Registration
Service Provider
Only required if Laravel Package Discovery has been disabled.
'providers' => [ \RzlZone\BladeMinify\Providers\RzlBladeMinifyServiceProvider::class, ],
Middleware
The package automatically registers its middleware by default.
Manual registration is only necessary if you prefer to manage the middleware yourself.
Laravel 10
Add the middleware to the web middleware group in app/Http/Kernel.php:
protected $middlewareGroups = [ 'web' => [ \RzlZone\BladeMinify\Middleware\RzlBladeOutputMinifier::class, // Other middleware... ], ];
Laravel 11, 12 & 13
Add the middleware to the web middleware group in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \RzlZone\BladeMinify\Middleware\RzlBladeOutputMinifier::class, ]); })
🔥 Usage
Enable in .env
RZLZONE_MINIFY_ENABLE=true
Disable in .env
RZLZONE_MINIFY_ENABLE=false
Minify only in production
RZLZONE_MINIFY_ONLY_PROD=true
Minify at all mode APP Env (default)
RZLZONE_MINIFY_ONLY_PROD=false
Ignore specific route names from minifying render output
'ignore_route_name' => [ // 'dashboard', // 'home', ]
ℹ️ Programmatic Manual Operations
use RzlZone\BladeMinify\Facades\RzlBladeMinify; // Manually compress a raw HTML layout string $compressedHtml = RzlBladeMinify::minify("<div> <p>Hello World</p> </div>"); // Explicitly isolate a string container from compression cycles $isolatedHtml = RzlBladeMinify::ignoreMinify("<code> preserve whitespace </code>");
ℹ️ Inline Blade Directive Isolation
To prevent specific code segments or third-party blocks from being targeted by the regex compression loops, wrap your code inside the dedicated wrapper directive:
{{-- Everything inside this directive block remains completely raw and uncompressed --}} @ignoreRzlzoneMinify <div> Strictly Preserved Preformatted Whitespace Output </div> @endIgnoreRzlzoneMinify
⚡️ Advanced Integration with Vite & React Fast Refresh
No special layout modifications or fragile workarounds are required.
The package natively tracks, isolates, and guards core asset bundler structures—such as React Fast Refresh preambles—ensuring strict security tokens and javascript scopes are preserved post-minification.
📦 Custom Vite Architecture Mapping
Publishing the configuration creates the custom-vite block inside config/rzlzone-blade-minify.php. This gives you full control over customized asset folders, CSP nonces, and hot-reload file parameters:
<?php return [ /* |-------------------------------------------------------------------------- | Core Minifier Settings |-------------------------------------------------------------------------- */ 'enable' => env('RZLZONE_MINIFY_ENABLE', true), 'run_production_only' => env('RZLZONE_MINIFY_ONLY_PROD', false), 'ignore_route_name' => [], /* |-------------------------------------------------------------------------- | Custom Vite Configuration |-------------------------------------------------------------------------- | | This array contains the configuration settings for the custom Vite asset | bundler integration, including build directories, CSP nonce settings, | and manifest/hot file tracking. | | WARNING: If you modify these values, ensure that your `vite.config.js` | configuration (such as build.outDir, build.manifest, or server settings) | is updated accordingly to match these paths and filenames. | */ "custom-vite" => [ /* |-------------------------------------------------------------------------- | Build Directory |-------------------------------------------------------------------------- | | This value determines the directory where compiled frontend assets | will be stored. The framework and asset helpers may use this path | when resolving built files and generated manifests. | | If changed, make sure to update the `build.outDir` in `vite.config.js` | (e.g., `public/custom-build-dir`). | */ "build_dir" => env("APP_BUILD_DIR", "build"), /* |-------------------------------------------------------------------------- | Use Asset Nonce |-------------------------------------------------------------------------- | | This option determines whether a nonce attribute should be applied | to generated asset tags. Enabling this can help satisfy Content | Security Policy (CSP) requirements for scripts and styles. | */ "use_nonce" => env("APP_USE_NONCE", false), /* |-------------------------------------------------------------------------- | Build Manifest File |-------------------------------------------------------------------------- | | This value specifies the name of the asset manifest file generated | by the frontend build process. The manifest is used to map original | asset names to their versioned counterparts. | | If changed, ensure your `vite.config.js` has a matching filename | via `build.manifest` configuration. | */ "manifest_name" => __rzl_bm_get_path_file__(env("APP_BUILD_MANIFEST_NAME"), default: "manifest.json"), /* |-------------------------------------------------------------------------- | Hot Module Replacement File |-------------------------------------------------------------------------- | | This value defines the location of the hot file used during local | development. When present, the framework will use it to detect and | communicate with the running development server. | | If customized, you must configure Vite's plugin or server options | to output the hot file to this exact path. | */ "hot_file" => __rzl_bm_get_path_file__(env("APP_HOT_FILE")) ? public_path(__rzl_bm_get_path_file__(env("APP_HOT_FILE"))) : "hot", ] ];
⚙️ Environment Tailoring Examples (.env)
You can easily override your asset pipeline behavior dynamically without touching your production config file:
# Custom Asset Build Mapping APP_BUILD_DIR="assets/dist" APP_BUILD_MANIFEST_NAME="manifest-v2.json" # Strict Content Security Policy (CSP) Hardening APP_USE_NONCE=true # Custom Dev Server Communication Target APP_HOT_FILE="vite.hot" # Vite Environment Bridge (Exposes these variables to vite.config.js) VITE_APP_BUILD_DIR="${APP_BUILD_DIR}" VITE_APP_HOT_FILE="public/${APP_HOT_FILE}" VITE_APP_BUILD_MANIFEST_NAME="${APP_BUILD_MANIFEST_NAME}"
🛠 How to consume these in vite.config.js
To make your frontend build tool automatically adapt to the configuration above, load the environment variables securely and bind them to your Vite options:
import { defineConfig, loadEnv } from 'vite'; import laravel from 'laravel-vite-plugin'; export default defineConfig(({ mode }) => { // Safely load Vite-prefixed variables into a local object const env = loadEnv(mode, process.cwd(), 'VITE_'); // Check and normalize custom hot file path using the local env object const hotFile = env.VITE_APP_HOT_FILE?.trim(); // Safely check length using optional chaining to prevent crashes const hasCustomHotFile = hotFile?.length > 0 && hotFile !== 'public/hot' && hotFile !== '/public'; return { plugins: [ laravel({ input: ['resources/css/app.css', 'resources/js/app.js'], refresh: true, // Sync the hot file location hotFile: hasCustomHotFile ? hotFile : undefined, // Sync the build output directory (e.g., public/assets/dist) buildDirectory: env.VITE_APP_BUILD_DIR?.trim() || "build" }), ], build: { // Sync the manifest filename manifest: env.VITE_APP_BUILD_MANIFEST_NAME?.trim() || 'manifest.json', }, }; });
💻 Blade Implementation Usage
Standard framework asset directives compile seamlessly.
The core minifier flags these tags with internal structural attributes (rzlzone--blade-minify) to ensure they remain safe and functional:
@viteReactRefresh @vite(['resources/js/app.tsx'])
⚠️ Configuration Sync Warning: If you choose to modify build_dir or manifest_name within your environment configuration, ensure that your root vite.config.js script properties (build.outDir, build.manifest) are mirrored exactly to prevent absolute path breakdown cycles during deployments.
📝 Changelog
Please see CHANGELOG for more information what has changed recently.
🤝 Contributing
Please see CONTRIBUTING for details.
❤️ Become a Sponsor
🛡 Security
Please report issues to rzlzone.dev@gmail.com.
🙌 Credits
📜 License
The MIT License (MIT). Please see License File for more information.
🔗 Framework & Reference Links
| Reference | URL |
|---|---|
| 📝 Laravel Docs | https://laravel.com/docs |
| 🏗 Illuminate\Support | https://github.com/laravel/framework/tree/13.x/src/Illuminate/Support |
| 🐘 PHP Official | https://www.php.net |
✅ Enjoy rzl-zone/blade-minify?
Leave a ⭐ on GitHub — it keeps this project thriving!
✨ From rzl-zone — where code meets passion.