symfony / stimulus-bridge

Stimulus integration bridge for Symfony projects
https://symfony.com/ux
75 stars 15 forks source link

[Bug] Removing unnecessary Promise in object of controllers to be loaded #81

Closed weaverryan closed 1 year ago

weaverryan commented 1 year ago

Hi!

Subtle change. Previously, the webpack loader parsed controllers.json (which eventually becomes the symfonyControllers variable in index.ts) into something that exported an object where each key was a promise - something like:

export default {
    'symfony--ux-autocomplete--autocomplete': import(/* webpackMode: \"eager\" */ '@symfony/ux-autocomplete/dist/controller.js')
}

Then, in startStimulusApp(), we called used .then() to wait for that promise to resolve then registered its resolved value:

symfonyControllers[controllerName].then((module) => {
    application.register(controllerName, module.default);
});

This is totally unnecessary and a relic of how this library was originally built. It also causes the UX controllers to be registered late. It's barely noticeable, but in practice, instead of the UX controllers being registered BEFORE the DOM is ready, they are registered after. This actually causes a problem with a new feature from LiveComponents.

The new parsed version of controllers.json from the loader looks much simpler:

+ import controller_0 from '@symfony/ux-autocomplete/dist/controller.js';

export default {
-    'symfony--ux-autocomplete--autocomplete': import(/* webpackMode: \"eager\" */ '@symfony/ux-autocomplete/dist/controller.js')
+    'symfony--ux-autocomplete--autocomplete': controller_0,
}

(I don't show it here, but lazy controllers have a similar simplification).

In short: it's a bug fix & an easy win. Controllers will load slightly earlier as a result.

Thanks!