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

[SPIKE] Configure Babel to add ES shims polyfills #4301

Closed
wants to merge 6 commits into from

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Oct 4, 2023

This PR adds the Babel polyfill provider ES shims via:
https://www.npmjs.com/package/babel-plugin-polyfill-es-shims

This is part of a two spike series:

Findings

Lots of polyfills are missing in ES shims when compared to core-js in #4557

We also have some unwanted bug fixes:

Remaining issues


Automatic polyfills

When Babel was added we chose "transforms only" without polyfills (see decision record)

This PR ensures early investigations aren't lost for:

Decision record implications

Since Babel with "transforms only" silently skips polyfills, we noted that care must be taken:

When writing or changing JavaScript we currently have to remember not to use features that aren't available in the oldest browsers we support, or to manually add a polyfill. We also rely heavily on manual testing across multiple browsers.

This spike PR ensures:

  1. Babel logger lists required polyfills
  2. Babel injects imports for required polyfills
  3. Rollup builds fail when imports are missing

i.e. Support for both polyfills and protection from missed polyfills all-in-one

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 October 4, 2023 10:10 Inactive
@github-actions
Copy link

github-actions bot commented Oct 4, 2023

📋 Stats

File sizes

File Size
dist/govuk-frontend-development.min.css 112.47 KiB
dist/govuk-frontend-development.min.js 38.58 KiB
packages/govuk-frontend/dist/govuk/all.bundle.js 78.74 KiB
packages/govuk-frontend/dist/govuk/all.bundle.mjs 73.99 KiB
packages/govuk-frontend/dist/govuk/all.mjs 3.86 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend-component.mjs 359 B
packages/govuk-frontend/dist/govuk/govuk-frontend.min.css 112.46 KiB
packages/govuk-frontend/dist/govuk/govuk-frontend.min.js 38.57 KiB
packages/govuk-frontend/dist/govuk/i18n.mjs 5.38 KiB

Modules

File Size
all.mjs 70.32 KiB
components/accordion/accordion.mjs 21.67 KiB
components/button/button.mjs 4.7 KiB
components/character-count/character-count.mjs 21.24 KiB
components/checkboxes/checkboxes.mjs 5.83 KiB
components/error-summary/error-summary.mjs 6.57 KiB
components/exit-this-page/exit-this-page.mjs 16.08 KiB
components/header/header.mjs 4.46 KiB
components/notification-banner/notification-banner.mjs 4.93 KiB
components/radios/radios.mjs 4.83 KiB
components/skip-link/skip-link.mjs 4.39 KiB
components/tabs/tabs.mjs 10.16 KiB

View stats and visualisations on the review app


Action run for 9234e61

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 October 5, 2023 16:37 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 October 17, 2023 09:52 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 October 24, 2023 09:20 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 October 31, 2023 12:46 Inactive
@colinrotherham colinrotherham changed the title [SPIKE] Configure Babel to add polyfills [SPIKE] Configure Babel to add ES shims polyfills Oct 31, 2023
@alphagov alphagov deleted a comment from github-actions bot Nov 27, 2023
@alphagov alphagov deleted a comment from github-actions bot Nov 27, 2023
@alphagov alphagov deleted a comment from github-actions bot Nov 27, 2023
@colinrotherham colinrotherham force-pushed the babel-polyfills branch 2 times, most recently from 1a09dd5 to 259bedb Compare November 30, 2023 13:56
This option is no longer `@babel/preset-env` specific since v7.13.0
Opera Mobile has relaunched for Android using Chromium but the compatibility data still thinks lots of features are unsupported:

```
The es-shims polyfill added the following polyfills:
  Array.prototype.every { "opera_mobile":"73" }
  Array.prototype.indexOf { "opera_mobile":"73" }
  Array.prototype.keys { "opera_mobile":"73" }
  Date.now { "opera_mobile":"73" }
  Function.prototype.name { "opera_mobile":"73" }
  Object.entries { "opera_mobile":"73" }
  String.prototype.split { "opera_mobile":"73" }
  String.prototype.trim { "opera_mobile":"73" }
```

See related issues:

browserslist/browserslist#766
babel/babel#15711
Rollup will log a warning for code requiring polyfills:

> (!) Unresolved dependencies
> https://rollupjs.org/troubleshooting/#warning-treating-module-as-external-dependency
> error-cause/Error (imported by "src/govuk/errors/index.mjs")

Babel will also log a warning:

> Some polyfills have been added but are not present in your dependencies.
>
> Please run one of the following commands:
> 	npm install --save error-cause@^1.0.1
> 	yarn add error-cause@^1.0.1

See ES shims on GitHub: https://github.com/es-shims
Although this polyfill is required for feature complete Error support, we don’t use the `.cause` property yet

```
The es-shims polyfill added the following polyfills:
  Error cause { "android":"61", "chrome":"61", "edge":"16", "firefox":"60", "ios":"10.3", "opera":"48", "safari":"10.1", "samsung":"8.2" }
```
Although this polyfill is required for bug-free `[].includes()` in legacy Firefox versions with sparse arrays, we don’t have any:

```
The es-shims polyfill added the following polyfills:
  Array.prototype.includes { "firefox":"60" }
```
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-4301 February 19, 2024 19:59 Inactive
@querkmachine
Copy link
Member

querkmachine commented Apr 15, 2024

Closing for housekeeping reasons, as this spike seems unlikely to be resolved at this time.

The related issue remains open for comments. #3948

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.

3 participants