mrclay/minify
Minify is a PHP asset server for combining and compressing JS/CSS with cache-friendly headers, conditional GET, long Expires, and CSS URL rewriting. Note: no longer regularly maintained and may break modern JS/CSS; use newer tooling.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require mrclay/minify:^4.0
Publish the config file (if needed):
php artisan vendor:publish --provider="Mrclay\Minify\MinifyServiceProvider"
Basic Usage
Add the package to your app/Providers/AppServiceProvider.php:
use Mrclay\Minify\Facades\Minify;
public function boot()
{
Minify::addResource('css', [
'path/to/main.css',
'path/to/extra.css'
]);
}
First Use Case Generate a minified URL in your Blade template:
<link href="{{ Minify::url('css') }}" rel="stylesheet">
SCSS Support
The package now supports SCSS preprocessing via scssphp 2.x while maintaining backward compatibility with older CSS files. No additional configuration is required—simply include .scss files as you would .css files:
Minify::addResource('scss-bundle', [
'resources/scss/main.scss',
'resources/scss/theme.scss'
]);
Minify::addResource('admin-scss', [
'resources/scss/admin/dashboard.scss',
'resources/scss/admin/components/*.scss'
]);
Minify::addResource('app-assets', [
'public/css/vendor.css', // Plain CSS
'resources/scss/app.scss', // SCSS
'public/css/legacy/old.css' // Legacy CSS
]);
SCSS Processing in Mix
Configure webpack.mix.js to compile SCSS:
mix.sass('resources/scss/app.scss', 'public/css')
.then(() => {
console.log('SCSS compiled to:', mix.config.output.css.paths);
});
Then feed the output to mrclay/minify:
Minify::addResource('app-css', [
'public/css/app.css' // Compiled from SCSS
]);
Vite + SCSS
Ensure Vite processes SCSS in vite.config.js:
export default {
css: {
preprocessorOptions: {
scss: {
additionalData: '@import "variables";'
}
}
}
};
Use the generated CSS in mrclay/minify:
$manifest = json_decode(file_get_contents(public_path('build/assets/manifest.json')));
Minify::addResource('app-scss', [
'public/build/assets/' . $manifest->{'app.css'}
]);
'cache_filename' => 'min_' . sha1_file(public_path('resources/scss/main.scss')),
<link href="{{ Minify::url('scss-bundle', true) }}" rel="stylesheet">
Minify::addResource('dark-theme', [
'resources/scss/theme/dark.scss',
'resources/scss/components/dark/*.scss'
]);
Minify::addResource('light-theme', [
'resources/scss/theme/light.scss'
]);
Toggle themes dynamically:
if (session('theme') === 'dark') {
Minify::addResource('active-theme', ['dark-theme']);
} else {
Minify::addResource('active-theme', ['light-theme']);
}
Blade Directives for SCSS Extend the custom directive to handle SCSS:
Blade::directive('minify', function ($expression) {
return "<?php echo Minify::url($expression); ?>";
});
Usage in Blade:
<link href="{{ minify('scss-bundle') }}" rel="stylesheet">
Dynamic SCSS Loading via Middleware Load theme-specific SCSS based on user preferences:
public function handle($request, Closure $next) {
$theme = session('theme', 'light');
Minify::addResource("theme-{$theme}", [
"resources/scss/theme/{$theme}.scss"
]);
return $next($request);
}
Preload Critical SCSS
Add SCSS files to the preload config:
'preload' => [
'css' => [
'/css/main.css',
'/css/theme.css' // Compiled from SCSS
]
],
Blade usage:
@foreach(config('minify.preload.css') as $asset)
<link rel="preload" href="{{ Minify::url($asset) }}" as="style">
@endforeach
Lazy-Load Non-Critical SCSS Exclude lazy-loaded SCSS bundles:
Minify::exclude('js', ['/js/lazy-loader.js']);
Minify::exclude('css', ['/css/lazy-components.css']); // Compiled from SCSS
MINIFY_DEBUG=true
<link href="{{ Minify::url('scss-bundle') }}?debug=true" rel="stylesheet">
@import paths).@import paths are absolute or resolved correctly:
@import "variables"; /* Use full path: @import "../variables.scss"; */
scssphp logs in Laravel logs (storage/logs/laravel.log).Minify::setDebugMode(true) to compare outputs.$theme-dark-primary).Minify::addResource('theme', [
'resources/scss/variables.scss', // First
'resources/scss/theme.scss' // Then
]);
scssphp memory usage.MEMORY_LIMIT=512M
// _partial1.scss
@import "partial2";
mrclay/minify’s streaming option:
Minify::setStreaming(true);
php artisan minify:clear
<link href="{{ Minify::url('scss-bundle', true) }}" rel="stylesheet">
mrclay/minify:
Minify::setPreprocessScss(false); // Only combine, don’t preprocess
mrclay/minify to handle.How can I help you explore Laravel packages today?