Asset Mapper
| Import maps | |
| Link assets | |
| Compile | |
| Versioned URLs |
Import maps
Asset Mapper serves your CSS and JS without a build step. The file importmap.php maps short names to files inside assets, and the browser loads them as modules. One entry per file, one flag for the entrypoint.
// importmap.php: app maps to ./assets/app.js as entrypoint
return app => path ./assets/app.js, entrypoint true
Link assets
Load the entrypoint in your layout with one Twig line. The helper prints the script tags for the entrypoint and everything it imports, so templates never hardcode file paths.
Twig layout block: importmap helper with name app
Renders script tags for app and its imports
Compile
For production, compile the assets once during deploy. The command copies every mapped file into public/assets with a versioned name, so the live server serves plain static files.
$ php bin/console asset-map:compile
Compiled assets written to public/assets/
Versioned URLs
Compiled files carry a content hash, so browsers cache them for a long time and still fetch the new version after each deploy. The asset() helper prints the hashed URL for you, with no manual renaming.
Template call: asset helper with images/logo.png
/assets/images/logo-a1b2c3.png
Next: Deployment
Article author: Arthur Isaev