-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow individual script imports #1472
Conversation
@alex-ju this looks good - will there be a 'suggested JS' feature in the guide like for the Sass? |
It would be great to have a similar feature indeed, but I would rather do it as a separate PR – not to overload this with changes and because it feels like an independent piece of work. |
@alex-ju that's sensible, but can you provide that code in a comment so we can test this in an application? |
f175a96
to
a2714e2
Compare
@andysellick I realised it might be simpler to add the suggested imports to this PR instead of providing a few examples that would limit testing. Added links to branches in repos I tested this with. I found some time today and updated the PR to contain suggested imports as shown below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work 👍
I'm really pleased by this change! It implements the RFC as expected.
My only suggestion would be to share details of an upgrade path in the release notes; the examples in the PR description are probably enough.
Adds missing scripts from govuk-frontend (button, header and tabs) and attach all GOVUKFrontend modules to GOV.Modules. Unfortunately, the UMD files imported from govuk-frontend are wrapped/exposed as a `GOVUKFrontend` variable (both when exporting the entire library and when exporting individual scripts), this being the reason for using `= window.GOVUKFrontend`, which could be rather confusing.
Previously the modules script was dealing only with GOV.UK Frontend Toolkit and GOV.UK Publishing scripts. It now handles GOV.UK Frontend modules initialisation too. This means we don't need to call `GOVUKFrontend.initAll` anymore, but we replace this with an empty function for backwards compatibility (othwerwise it will cause a hard error on projects calling this function)
a2714e2
to
c7f05aa
Compare
Thank you, @bilbof! I rebased and updated the JavaScript usage section and referred it from the changelog. |
* Change `_all_components.scss` to use dependencies from support stylesheets ([PR #1502](#1502)) * Allow individual JavaScript imports ([PR #1472](#1472)) To import individual components refer to ['Include the assets section in docs'](https://github.com/alphagov/govuk_publishing_components/blob/master/docs/install-and-use.md) and the 'Suggested imports for this application' section available on `/component-guide` in your application.
What
Allow individual, component-based, JavaScript imports.
Why
Follow up on #1159 to complete RFC 108
To avoid excessive code from being imported in the application.
Notes
This PR will remain draft until tested with applications consuming the gem but comments/reviews are welcome.Tested with:
content-publisher
finder-frontend
frontend
For applications dependent on
static
/slimmer
,static
requires to be updated first to use the modules system from this gem instead ofgovuk_frontend_toolkit
.Modules in GOV.UK
JavaScript modules in GOV.UK are actually design patterns for keeping pieces of code independent of other components. We currently have 3 design patterns for writing JavaScript in GOV.UK applications.
GOV.UK Frontend Toolkit Modules
Mostly found in legacy scripts, either migrated from Toolkit, Elements, or written in the same era.
GOV.UK Publishing Modules
Found across GOV.UK applications, on relatively new scripts (2 years or younger). Similar to the
govuk-frontend
approach described below, it relies on the classic prototype pattern but with astart
function that takes the element that initialises on. Thisstart
function was preserved so that the module initialisation script (modules.js
) would work for both legacy and new scripts without any alteration required.GOV.UK Frontend Modules
Used in
govuk-frontend
scripts. Relies on the classic prototype pattern with aninit
function. The element is passed to the constructor function.Visual Changes
No visual changes.