symfony/asset-mapper
Symfony AssetMapper exposes asset directories and publishes them to a public folder with digested (versioned) filenames. It can also generate an importmap, letting you use modern JavaScript modules without a build step.
FrameworkBundle, HttpKernel, and Symfony’s CLI commands) makes direct integration non-trivial.AssetMapper dynamically maps and digests assets at runtime. This creates duplication (e.g., both systems generating hashed filenames) or conflicts (e.g., Mix’s mix-manifest.json vs. AssetMapper's dynamic mapping).@vite(), @mix()).mix:version).config/app.php vs. Symfony’s config/packages/).AssetMapperBundle, AssetMapperInterface) into Laravel’s ecosystem. Key challenges:
AssetMapper as a Laravel service provider).bin/console commands (e.g., asset:map) must be rewritten as Laravel Artisan commands.public_path() vs. Symfony’s public/ directory structure needs alignment.AssetMapper would either:
import.meta.glob) may conflict with AssetMapper's importmap generation.AssetMapperBundle.AssetMapper may not be patched for Laravel.AssetMapper interact with Laravel’s cache tags or CDN invalidation?FrameworkBundle)?| Step | Action | Complexity | Laravel-Specific Notes |
|---|---|---|---|
| 1 | Assess Current Asset Workflow | Low | Document existing asset pipeline (e.g., manual hashing, no build tools). |
| 2 | Choose Integration Scope | Medium | Decide if replacing all asset handling or supplementing it (e.g., for static assets only). |
| 3 | Set Up Symfony Compatibility Layer | High | Create a Laravel service provider to bridge Symfony’s AssetMapper with Laravel’s container. Example: |
| ```php | |||
| // app/Providers/AssetMapperServiceProvider.php | |||
| namespace App\Providers; | |||
| use Illuminate\Support\ServiceProvider; | |||
| use Symfony\Component\AssetMapper\AssetMapper; | |||
| use Symfony\Component\AssetMapper\AssetMapperInterface; | |||
| class AssetMapperServiceProvider extends ServiceProvider { |
public function register() {
$this->app->singleton(AssetMapperInterface::class, function ($app) {
$mapper = new AssetMapper();
// Configure mappings (adapt Symfony’s config to Laravel paths)
$mapper->map('js', [$app->publicPath('js')]);
$mapper->map('css', [$app->publicPath('css')]);
return $mapper;
});
}
}
| Requires reverse-engineering Symfony’s `AssetMapperBundle`. | | 4 | **Adapt CLI Commands** | High | Rewrite Symfony’s `asset:map` command as a Laravel Artisan command. Example: | | |php
// app/Console/Commands/MapAssets.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\AssetMapper\AssetMapperInterface;
class MapAssets extends Command {
protected $signature = 'asset:map';
protected $description = 'Map assets to public directory with fingerprinted filenames';
public function handle(AssetMapperInterface $mapper) {
$mapper->dump();
$this->info('Assets mapped successfully!');
}
}
| 5 | **Configure Importmap** | Medium | Set up importmap generation in `config/app.php` or a custom config file. Example: |
| | ```php
// config/asset_mapper.php
return [
'public_directory' => public_path('build'),
'importmap' => [
'includes' => [
'react' => 'https://esm.sh/react',
'lodash' => 'https://esm.sh/lodash',
],
'scopes' => [
'/' => ['app.js'],
],
],
];
``` | No native Laravel support; requires manual integration. |
| 6 | **Update Asset References** | Low | Replace manual asset paths (e.g., `<script src="app.js">`) with `AssetMapper`-generated paths. Example: |
| | ```php
// Use a custom Blade directive or helper
{{ asset('app.js') }} // Laravel’s default (no has
How can I help you explore Laravel packages today?