alphagov / govuk-frontend

GOV.UK Frontend contains the code you need to start building a user interface for government platforms and services.
https://frontend.design-system.service.gov.uk/
MIT License
1.16k stars 319 forks source link

JavaScript modules are attached to the same global variable when imported individually #1836

Closed alex-ju closed 2 years ago

alex-ju commented 4 years ago

Context

When we compile the JavaScript assets to UMD we attach them to GOVUKFrontend, which works well when importing all components, but not so great when importing individual scripts.

Problem

For example, in GOV.UK Publishing Components, when importing multiple components individually from govuk-frontend only the last one will be available.

//= require govuk/components/accordion/accordion.js
# accordion.js is exposed via window.GOVUKFrontend; not GOVUKFrontend.Accordion as expected
//= require govuk/components/accordion/button.js
# button.js is exposed via window.GOVUKFrontend; accordion.js is lost

As a workaround, we attached the component to another global GOVUK.Modules.[ComponentName] right after importing it.

Potential solution

Update compile-assets.js script to attach components to GOVUKFrontend.[ComponentName] instead of GOVUKFrontend.

Done when

Details of breaking change

36degrees commented 4 years ago

Although the current behaviour definitely doesn't seem 'correct', I think we'd have to treat this as a breaking change so I'm going to attach this to the 4.0 milestone.

alex-ju commented 4 years ago

Thank you @36degrees! I'm pretty confident I introduced this problem, so happy to work on it if the team decides so.

vanitabarrett commented 3 years ago

Had a discussion in the v4 planning session about whether we should make this change backwards compatible by supporting the current way of doing things with the new way (and then removing that in a later release). I think we're all in agreement that the current behaviour is a bug that people are now probably relying on working in that way (as we haven't had many reports of this issue). Probably needs a little bit of investigation into what the fix would look like before we know for certain whether it's something to include in v4.

vanitabarrett commented 2 years ago

Moving this into Blocked for now as I've asked Alex from GOV.UK to test this change to make sure it works for them. Waiting to hear back.