kriswallsmith/assetic
Assetic is a PHP asset management framework for loading, filtering, combining, and dumping CSS/JS and other assets via collections and filters (LESS, YUI, etc.). Note: this repo is unmaintained; development continues at assetic-php/assetic (assetic/framework).
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require kriswallsmith/assetic:^1.4.0
Add to composer.json under autoload-dev:
"autoload-dev": {
"files": ["vendor/kriswallsmith/assetic/src/Assetic/Extension.php"]
}
Basic Configuration:
Create config/assetic.php (or publish with):
php artisan config:publish --provider="Assetic\Bundle\AsseticBundle\AsseticBundle"
Define a simple asset dump command:
'commands' => [
'app:assets' => [
'dump' => true,
'watch' => false,
'verbose' => true,
],
],
First Use Case: Define a basic asset with Sass support:
'assets' => [
'app_css' => [
'inputs' => [
'resources/scss/app.scss',
'public/css/vendor.css',
],
'output' => 'public/build/css/app.css',
'filters' => ['sassphp', 'cssrewrite'],
],
],
Run the dump command:
php artisan assetic:dump --env=production
Sass Integration:
Use the new sassphp filter for Libsass-based Sass compilation:
'assets' => [
'admin_scss' => [
'inputs' => ['resources/scss/admin.scss'],
'output' => 'public/build/admin.css',
'filters' => ['sassphp' => ['style' => 'compressed']],
],
],
Note: Requires league/sass-php (composer require league/sass-php).
Asset Grouping: Organize assets by feature with environment-specific filters:
'assets' => [
'frontend' => [
'inputs' => ['public/js/*.js', 'public/css/*.css'],
'output' => 'public/build/all.js',
'filters' => config('app.env') === 'production' ? ['uglifyjs', 'cssmin'] : [],
],
],
Dynamic Asset Loading: Load assets conditionally in Blade:
@foreach (Config::get('assetic.assets') as $name => $asset)
@if ($asset['enabled'] ?? true)
@if (str_contains($asset['output'], '.css'))
<link rel="stylesheet" href="{{ $asset['output'] }}">
@endif
@endif
@endforeach
Filter Chaining: Chain filters for complex pipelines (e.g., Sass → CSS minification):
'filters' => [
'sassphp' => ['style' => 'expanded'],
'cssrewrite',
'cssmin',
],
sassphp filter replaces deprecated Twig-based Sass filters.sassphp):
'filters' => [
'sassphp' => [
'style' => 'compressed',
'precision' => 10,
],
],
Twig Deprecation:
sassphp instead of Twig-based Sass).Sassphp Dependency:
sassphp filter requires league/sass-php (≥v1.0.0).composer require league/sass-php
Cache Invalidation:
php artisan assetic:dump --clear
Path Resolution:
inputs paths are correct (e.g., resources/scss/ vs. public/css/).'inputs' => [resource_path('scss/app.scss')],
php artisan assetic:dump --dry-run
php artisan assetic:dump --verbose
sassphp fails:
php artisan assetic:dump --verbose | grep -i sass
Custom Sass Options:
Extend sassphp filter options:
'filters' => [
'sassphp' => [
'style' => 'compressed',
'include_paths' => [resource_path('scss')],
],
],
Asset Loaders: Override default asset loading (e.g., for remote files):
$loader = new \Assetic\Asset\AssetLoader();
$loader->set('https://cdn.example.com/', new \Assetic\Asset\FtpAssetReader());
Event Listeners: Hook into Assetic’s lifecycle (if using AsseticBundle):
// Example: Log asset dump events
$dispatcher->addListener(AsseticEvents::ON_DUMP, function ($event) {
\Log::info('Asset dumped: ' . $event->getAsset()->getTargetPath());
});
'enabled': false in assetic.php for local environments.--jobs=N for multi-core compilation (if supported):
php artisan assetic:dump --jobs=4
'inputs' => [
'public/css/*.css',
'!public/css/*.min.css',
],
NO_UPDATE_NEEDED would not apply here due to the new `sassphp` filter and Twig deprecation handling.
How can I help you explore Laravel packages today?