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

Add Babel to transform ES2020 public and static class fields #3820

Merged
merged 10 commits into from
Jul 14, 2023

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Jun 19, 2023

This PR adds Babel transpilation as decided in:

I've added transforms for two ES2020 features back to ES2015—without polyfills or helper functions

Public static fields

After merging Migrate JavaScript to ES2015 classes we were left with "optional" class properties appended in the constructor that would be clearly defined as static in ES2020

Before

export class I18n {
  constructor() {
    // ...
  }
}

I18n.pluralRulesMap = {}
I18n.pluralRules = {}

After

export class I18n {
  constructor() {
    // ...
  }
  
  static pluralRulesMap = {}
  static pluralRules = {}
}

Public fields

We also have "optional" class properties appended in the constructor that would be public class fields

Before

export class Radios {
  constructor($module) {
    if (!($module instanceof HTMLElement)) {
      return this
    }
  
    this.$module = $module
    this.$inputs = $module.querySelectorAll('input[type="radio"]')
  }
}

After

Both $module and $inputs can be declared as class fields (with default values if required)

export class Radios {
  $module
  $inputs

  constructor($module) {
    if (!($module instanceof HTMLElement)) {
      return this
    }
  
    this.$module = $module
    this.$inputs = $module.querySelectorAll('input[type="radio"]')
  }
}

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 19, 2023 08:57 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 19, 2023 18:28 Inactive
@colinrotherham colinrotherham requested a review from a team as a code owner June 20, 2023 12:50
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 20, 2023 12:50 Inactive
@colinrotherham colinrotherham changed the base branch from browserslist-support to module-default June 20, 2023 12:51
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 20, 2023 15:20 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 20, 2023 15:22 Inactive
@colinrotherham colinrotherham changed the title [SPIKE] Babel with ES2020 class fields [SPIKE] Babel with ES2020 public and static class fields Jun 20, 2023
@colinrotherham colinrotherham marked this pull request as draft June 20, 2023 15:28
@colinrotherham colinrotherham force-pushed the module-default branch 2 times, most recently from 646916f to 1bd3aba Compare June 23, 2023 11:03
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 23, 2023 11:04 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 June 23, 2023 11:16 Inactive
Copy link
Contributor

@domoscargin domoscargin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diff looks good to me - time to use all the shiny things!

As noted above, we're holding off merging this till Exit This Page gets merged.

@colinrotherham
Copy link
Contributor Author

colinrotherham commented Jul 13, 2023

Thanks @domoscargin

My only concern (now) is it's slightly jarring seeing JSDoc comments moved to the bottom of the class

@36degrees @romaricpascal Any further thoughts on removing comments to keep KB size down? Hadn't realised how easy it was with Babel generatorOpts: { comments: false } and they're all gone

Comments can use @preserve to keep important blocks too:

Seeing ~40-50% drop in file size KB

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 July 13, 2023 12:05 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 July 13, 2023 12:22 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat ! And thanks for the review @domoscargin 🙌🏻 I'm pretty keen to remove unnecessary comments (especially given the gains highlighted by the spike), but let's discuss that as a separate PR so we can have this merged on its own 😊


/** @private */
boundOnHashChange

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question Noticed while looking at #3958 that this list is missing the mql field, which is still first used in setupResponsiveChecks a little further. Is it intentional, or just that is was missed among all the other fields? 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romaricpascal Nice spot, yeah it was missed. Added it 👍

Transforms will be applied based on `.browserslistrc` supported browsers, with helper functions bundled if needed
We enable `{ bugfixes: true }` to let Babel use smaller transforms for partially supported browser features

For example, Babel knows that full support for template strings as “tagged templates with escape sequences” only shipped in Safari 11

But we can now use template strings in Safari 9.1 too

https://caniuse.com/mdn-javascript_grammar_template_literals_template_literal_revision
Babel transpiles ES2020 class fields for our Browserslist supported browsers
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 July 14, 2023 16:04 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3820 July 14, 2023 16:05 Inactive
@colinrotherham
Copy link
Contributor Author

@romaricpascal @domoscargin Updated with Exit this page from #3953

@romaricpascal
Copy link
Member

Transformation looks fine to me on the file for Exit this page as well 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set up Babel transpilation for browser compatibility
4 participants