devlabs91/assetic
Laravel package integrating Assetic for managing and compiling web assets (CSS/JS) with filters and bundling, helping organize, minify, and combine frontend resources within your application build and deployment workflow.
Installation
composer require devlabs91/assetic
Add the service provider to config/app.php:
'providers' => [
// ...
Devlabs91\Assetic\AsseticServiceProvider::class,
],
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Devlabs91\Assetic\AsseticServiceProvider" --tag="config"
Edit config/assetic.php to define your asset paths, filters, and bundles.
First Use Case: Compiling a Bundle Define a bundle in a controller or service:
use Devlabs91\Assetic\Bundle\BundleInterface;
use Devlabs91\Assetic\Bundle\AssetCollection;
$bundle = new AssetCollection('app');
$bundle->add('css/style.css');
$bundle->add('js/main.js');
$output = $bundle->dump();
file_put_contents(public_path('assets/compiled.css'), $output);
Asset Organization
admin, frontend) using named bundles.$adminBundle = new AssetCollection('admin');
$adminBundle->add('/css/admin/dashboard.css');
$adminBundle->add('/js/admin/utils.js');
Filter Chains
$bundle = new AssetCollection('app');
$bundle->add('css/app.css')
->setTarget('css/compiled.css')
->setFilters([
'cssrewrite', // Rewrite URLs in CSS
'cssmin', // Minify CSS
'timestamp', // Add fingerprint
]);
Dynamic Asset Loading
// In a middleware
$view->share('appAssets', $this->assetic->getBundle('app')->getUrl());
Versioning & Cache Busting
timestamp filter for automatic cache busting:
$bundle->setFilters(['timestamp']);
echo $bundle->getUrl(); // Outputs: /assets/compiled.css?v=123456789
php-sass) before handing off to JS tools.Blade::directive('assets', function ($expression) {
return "<?php echo app('assetic')->getBundle({$expression})->getUrl(); ?>";
});
Usage:
<link rel="stylesheet" href="{{ assets('app') }}">
Filter Order Matters
cssrewrite must run before cssmin to avoid broken paths in minified output.$bundle->setFilters(['cssmin', 'cssrewrite']); // ❌ Broken paths
File Permissions
storage/app/assetic directory is writable:
mkdir -p storage/app/assetic && chmod -R 775 storage/app/assetic
Circular Dependencies
@import (CSS) or require (JS) statements—Assetic may hang or fail silently.Missing Filters
uglifyjs) is missing, install it via Composer:
composer require assetic/uglifyjs-filter
Enable Verbose Output
Set debug: true in config/assetic.php to log filter execution:
'debug' => env('APP_DEBUG', false),
Check Compiled Output
Inspect storage/app/assetic for intermediate files to debug filter failures.
Custom Filters
Create a filter class and register it in config/assetic.php:
'filters' => [
'my_custom_filter' => \App\Filters\MyCustomFilter::class,
],
Bundle Events Listen for bundle compilation events:
Assetic::getDispatcher()->addListener('bundle.compile', function ($event) {
// Log or modify bundle before compilation
});
Asset Fingerprinting Override the default timestamp format in config:
'timestamp_format' => 'md5', // Use MD5 hashes instead of Unix timestamps
How can I help you explore Laravel packages today?