> Symfony 6 >

Course Overview

Login to bookmark this course

AssetMapper: JS Moderno sin Sistema de Compilación

Want to write modern JavaScript without a build system? Learn the basics, install the AssetMapper component & more in this tutorial.

  • 1465 students
  • EN/ES Captions
  • EN/ES Script
  • Certificate of Completion

Your Guides

About this course

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^4.0", // v4.2.0
        "doctrine/doctrine-bundle": "^2.7", // 2.10.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.4
        "doctrine/orm": "^2.12", // 2.15.2
        "knplabs/knp-time-bundle": "^1.18", // v1.20.0
        "pagerfanta/doctrine-orm-adapter": "^4.0", // v4.1.0
        "pagerfanta/twig": "^4.0", // v4.1.0
        "stof/doctrine-extensions-bundle": "^1.7", // v1.7.1
        "symfony/asset": "6.3.*", // v6.3.0
        "symfony/asset-mapper": "6.3.*", // v6.3.0
        "symfony/console": "6.3.*", // v6.3.0
        "symfony/dotenv": "6.3.*", // v6.3.0
        "symfony/flex": "^2", // v2.3.1
        "symfony/framework-bundle": "6.3.*", // v6.3.0
        "symfony/http-client": "6.3.*", // v6.3.0
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/proxy-manager-bridge": "6.3.*", // v6.3.0
        "symfony/runtime": "6.3.*", // v6.3.0
        "symfony/stimulus-bundle": "^2.9", // v2.9.1
        "symfony/twig-bundle": "6.3.*", // v6.3.0
        "symfony/ux-turbo": "^2.9", // v2.9.1
        "symfony/web-link": "6.3.*", // v6.3.0
        "symfony/yaml": "6.3.*", // v6.3.0
        "twig/extra-bundle": "^2.12|^3.0", // v3.6.1
        "twig/twig": "^2.12|^3.0" // v3.6.1
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.4
        "symfony/debug-bundle": "6.3.*", // v6.3.0
        "symfony/maker-bundle": "^1.41", // v1.49.0
        "symfony/stopwatch": "6.3.*", // v6.3.0
        "symfony/web-profiler-bundle": "6.3.*", // v6.3.0
        "zenstruck/foundry": "^1.21" // v1.33.0
    }
}

¿Alguna vez has querido simplemente... escribir JavaScript moderno y que funcione sin necesidad de complicarte con un sistema de compilación?

¡Yo también! Y por fin, gracias a los avances en la web, los servidores web y el nuevo componente de Symfony, ¡es posible! ¡Hola AssetMapper! En este tutorial, vamos a aprender:

  • Los fundamentos del JavaScript moderno en un navegador, por qué y cómo las cosas finalmente "simplemente funcionan"
  • Instalar el nuevo componente AssetMapper
  • Entender las "rutas lógicas" de los activos y el versionado
  • Trabajar con el nuevo archivo importmap.php y importmap:require
  • Uso de CSS, junto con preprocesadores como Sass o Tailwind
  • Añadir StimulusBundle y controladores UX de terceros
  • ¡Controladores de Stimulus diferidos!
  • Encontrar archivos de paquetes y (opcionalmente) descargarlos
  • CSS y JS específicos de la página
  • Despliegue
  • Medición y ajuste del rendimiento + precarga

Con AssetMapper, se vuelve a lo bonito y básico de programar, refrescar y volver al trabajo.

Next courses in the Symfony 6: Tools, Tools, Tools! section of the Symfony 6 Track!

17 Comments

Sort By
Login or Register to join the conversation
Fabrice avatar Fabrice 1 year ago

Hello! I didn't know where to propose the idea, so I put it here:

A course to learn how to master DataTable with Symfony would be great! There are so many things to do with this library. Use Ajax, add fields at the top of each column to filter, add checkboxes, events.

All why not using Stimulus?

By the way, I've always wondered if it was possible to override EasyAdmin to use DataTables instead, and have it use Ajax requests.

In short, if one day you decide to create this course, it will be insane!

1 | Reply |

Hey @Fabrice

Thank you for your feedback! I'll share it with the team

By the way, I've always wondered if it was possible to override EasyAdmin to use DataTables instead and have it use Ajax requests.

Honestly, I don't know how hard/easy it would be to do so. I know you can override a bunch of stuff of EasyAdmin though

Cheers!

1 | Reply |
Amine avatar Amine 7 months ago

Hi All

How can i get this course. Just this course ?

| Reply |

Hi @Amine

It's easy, just click the link "Buy Access" and after the subscription block find link with text "No thanks, I just want to buy AssetMapper: Modern JS with Zero Build System for $12.00." and go through normal checkout process

Cheers!

| Reply |

Thank you for your feedback. What I often do is just check if you may have changed the design.

I suggest revisiting your designs as they no longer seem to be suitable in terms of accessibility.

| Reply |
Milica-S avatar Milica-S 7 months ago

Hi! How can I dynamically load versioned images in my JavaScript file, specifically using jQuery? I'm looking to incorporate images into my webpage via JavaScript, and I'm curious about the best approach for accessing images with versioning applied.

| Reply |

Hey @Milica-S!

That's an excellent question! Currently, it's not possible with AssetMapper - and maybe it will never be. In Webpack Encore, you can import images to get their versioned filename... but that's really a Webpack trick: it doesn't make sense in a browser environment to import images.

And, it is probably ok that this doesn't work :). There are 2 alternative solutions:

1) Pass the image filenames to your JavaScript. If using Stimulus, you would do this with values - https://stimulus.hotwired.dev/reference/values - e.g.

<div
    {{ stimulus_controller('gallery', {
        images: images
    }) }}
>

Where images is an array of the image filenames. To populate this in a controller, you should be able to autowire the Symfony\Component\Asset\Packages and pass the "logical path" to any of your images.

2) An even BETTER approach is just to... generate the <img> tags in Twig. Even if you wanted to do something fancy with your images in JavaScript, you could still start with all of the <img> tags in Twig - e.g.

<div data-controller="gallery">
    <img src="{{ asset('logical-path1.png') }}">
    <img src="{{ asset('logical-path2.png') }}">
</div>

If you need to do something specifically with the <img> tags, you could use Stimulus targets to easily fetch those items.

So, this feature doesn't exist, but that's in part because we're embracing passing things TO JavaScript or, better, rendering the HTML in Twig instead of JavaScript.

Cheers!

| Reply |
BigBenJr avatar BigBenJr 7 months ago

Thanks for the tutorial. But I wonder if enabling versioning for images is a good idea in a SEO perspective for 2 reasons:

1) If I want my pictures to be well ranked on Google image, it would be bad if the url changed every time I do a slight modification on the image (for example correction of an error or improving quality of the image). I guess that to get a good position on google search, images are like web pages, you don't want them to change their url but be stable as pyramid.

2) Bu the way my images already exist in my website without any version number in their name. So if I decide now to enable versioning, all url will change ! My google ranking will collapse and will have to restart from scratch, is that correct ?

| Reply |

Hey @BigBenJr!

Sorry for my very slow reply!

1) If I want my pictures to be well ranked on Google image, it would be bad if the url changed every time I do a slight modification on the image (for example correction of an error or improving quality of the image).

If you're actually worried about the google ranking of your images themselves, this very well may be true. Most of the time, images are not the "star" themselves: they are just component we use on our site. And I'd argue that, for situations where the image IS so important, it's more likely these are images you're uploading to your site (vs creating and committing to your code). And in that case, the images aren't versioned. But if you have this use-case of wanting the images to have a high ranking and your images are committed to your code, then thinking about this is a good idea. In that case, you could put the images directly into public/

Cheers!

1 | Reply |

Is it possible (would you recommend) using react or vue, ux-turbo, stimulus, assetmapper AND webpack encore at the same time in one project? I really like assetmapper so i want to use it in my new project but i also would like to keep my "react or vue-parts" in my new project. I understand that for vue i nee a build system (in this case webpack encore). Can i use it next to eachother like standalone modules or should there be some other method to make sure they can live next to eachother in the same project? If i check the symfony docs it seems that react is easier to handle with assetmapper. Is it then possible to use react without a build system?

| Reply |

Hey @lexhartman!

Good question! You can use vue, ux-turbo, stimulus, etc with Webpack Encore just fine. So the REAL question is: can I use Encore & AssetMapper in the same project. The answer is: theoretically yes, but I wouldn't recommend it. They weren't designed with this in mind, so you would likely need to do some manual, weird stuff to get them to play together nicely.

I really like assetmapper so i want to use it in my new project but i also would like to keep my "react or vue-parts" in my new project

Get rid of those parts ;). Ok, real answer: you DO need a build system still for React and Vue. React is actually fairly easy: you just need something to convert the .jsx files to .js files... then those can be served directly via AssetMapper. We do this on ux.symfony.com. It's not ideal - but works - https://github.com/symfony/ux/blob/2.x/ux.symfony.com/package.json#L12

Vue is much harder, because the SFC can even contain styling. I actually could NOT figure out a way to make this work. Vue REALLY wants you to be inside a full build system like Vite or Webpack that handles JS, CSS, etc. In the end, for ux.symfony.com, I had to NOT use SFC - and instead use the template key: https://github.com/symfony/ux/blob/2.x/ux.symfony.com/assets/vue/controllers/PackageSearch.js

If i check the symfony docs it seems that react is easier to handle with assetmapper. Is it then possible to use react without a build system?

Actually, it's HARDER to use React with AssetMapper. Well, React isn't too bad. But the point is, React & Vue need a build. AssetMapper, by design, doesn't provide that. My overall advice these days is: (1) build a full SPA with something like React/Vue and use their tooling all the way and Symfony as an API OR (2) get rid of React/Vue and build a HTML-powered app like we do in the LAST Stack tutorial - https://symfonycasts.com/screencast/last-stack. That's what we do and it feels GREAT

Cheers!

1 | Reply |

Thanks for the comprehensive answer!

For my purpose i would prefer scenario 2 (get rid of React/Vue and build a HTML-powered app like we do in the LAST Stack tutorial). BUT i am lazy and i really like the https://jsonforms.io/ for quick and dynamic building forms/ questionnary's/ quizes (it uses React or Vue) instead of doing all the complex and hard work with entities and formtypes and so on. Is there any way (or maybe an alternative) that you can think of using this json formbuilder without React or Vue? Otherwise i am going for scenario 1 😅

| Reply |

hey!
i'd like also to use jsonforms with symfony (or laravel) : how could I proceed ? did you follow your idea?
Thx

| Reply |

I finally chose for the combination of Assetmapper without react/ vue and/or jsonforms. The dynamicforms fulfilled my needs! It's explained here: https://symfonycasts.com/screencast/last-stack/live-forms#dependent-form-fields

1 | Reply |

Hey @lexhartman!

that you can think of using this json formbuilder without React or Vue?

I don't know that library, but I don't think there's anything quite the automatic. I don't mind Symfony's form types at all - but fair enough if you want to use something like this :). If you build this with React, you could still have a fairly simple path to scenario 1.

Cheers!

| Reply |
Ruslan avatar Ruslan 1 year ago

Hi. When we will test for Stymulus controllers? :)

| Reply |

Hey @Ruslan!

By "test for Stimulus controllers", you do mean actually write tests (e.g. unit/functional tests) for Stimulus controllers? If so, not in this tutorial - but that IS something we should chat about. We're releasing some testing courses, that should be part of it somewhere I realize 🤔.

2 | Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!