Skip to content
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

Merged
merged 7 commits into from
May 21, 2020
Merged

Allow individual script imports #1472

merged 7 commits into from
May 21, 2020

Conversation

alex-ju
Copy link
Contributor

@alex-ju alex-ju commented Apr 24, 2020

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:

For applications dependent on static/slimmer, static requires to be updated first to use the modules system from this gem instead of govuk_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.

GOVUK.Modules.TestModule = function () {
  var that = this
  that.start = function (element) {
    //
  }
}

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 a start function that takes the element that initialises on. This start function was preserved so that the module initialisation script (modules.js) would work for both legacy and new scripts without any alteration required.

function TestModule () { }
TestModule.prototype.start = function (element) {
  //
}

GOV.UK Frontend Modules

Used in govuk-frontend scripts. Relies on the classic prototype pattern with an init function. The element is passed to the constructor function.

function TestModule (element) {
  this.element = element
}
TestModule.prototype.init = function () {
  //
}

Visual Changes

No visual changes.

@alex-ju alex-ju changed the title Allow individual js imports Allow individual script imports Apr 24, 2020
@bevanloon bevanloon temporarily deployed to govuk-publis-allow-indi-dpjqxl April 24, 2020 17:28 Inactive
@andysellick
Copy link
Contributor

@alex-ju this looks good - will there be a 'suggested JS' feature in the guide like for the Sass?

@alex-ju alex-ju marked this pull request as ready for review April 28, 2020 17:21
@alex-ju
Copy link
Contributor Author

alex-ju commented Apr 28, 2020

@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.

@andysellick
Copy link
Contributor

@alex-ju that's sensible, but can you provide that code in a comment so we can test this in an application?

@alex-ju alex-ju force-pushed the allow-individual-js-imports branch from f175a96 to a2714e2 Compare May 4, 2020 17:28
@alex-ju
Copy link
Contributor Author

alex-ju commented May 4, 2020

@alex-ju that's sensible, but can you provide that code in a comment so we can test this in an application?

@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.
Screenshot 2020-05-04 at 17 22 20

Copy link
Contributor

@bilbof bilbof left a 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.

:shipit:

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)
When we decided to initialise all govuk-frontend components at once (92bef7e) we added all scripts on top of the existing imports at the components level. This commit removes the redundant import, similarly with #1331 for SCSS.
@alex-ju alex-ju force-pushed the allow-individual-js-imports branch from a2714e2 to c7f05aa Compare May 21, 2020 14:26
@alex-ju
Copy link
Contributor Author

alex-ju commented May 21, 2020

Thank you, @bilbof! I rebased and updated the JavaScript usage section and referred it from the changelog.

@alex-ju alex-ju merged commit ee53b58 into master May 21, 2020
@alex-ju alex-ju deleted the allow-individual-js-imports branch May 21, 2020 14:32
alex-ju added a commit that referenced this pull request May 21, 2020
* 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.
This was referenced May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants