How to reference assets in a .js file dynamically when using Asset Mapper? #60683
Replies: 2 comments 9 replies
-
I don’t think it is possible right now 🤔 @smnandre do you think it would make sense to map images to a script exporting their filename? E.g. if an importmap included "/assets/images/flags/au.png": "data:application/javascript,export%20default%20'/assets/images/flags/au-B8Lgenv.png'" scripts could then import |
Beta Was this translation helpful? Give feedback.
-
If the images are available in AssetMapper, you can reference them via the That said, @MatTheCat, I don’t think this would be a good idea -- even if it technically worked. Including every image (like flags for all countries or a full emoji set) would create a huuuge import map, which ends up in the HTML on every request. Personally, I saw the CSS trick more as a clever hack than a sustainable solution. I’m not sure it makes sense to push import maps into doing things they weren’t designed for. Instead, we should probably focus on supporting the official features we haven’t implemented yet. @kekkoudesu Do you really need to version the flags? Couldn’t that be done manually --say, by placing them in a 1.0/ folder? I’m not sure this is something that changes often, is it? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using Asset Mapper for my files with a Symfony 7 project.
I have a file tree like this:
I have some Javascript code in a script file that checks which country is selected from a
<select>
and uses that value to determine the filename of an image:However, this doesn't work when I load the page, because Asset Mapper is versioning the assets.
asset()
won't even work in an external script. And when the user interacts with the dropdown menu, this filename needs to change dynamically.I can't figure out how to reference these assets dynamically while using Asset Mapper... any ideas?
Beta Was this translation helpful? Give feedback.
All reactions