google / google/site-kit-wp

Update Babel Configuration to Support `toSorted()` Method

Open
#7,571 10 comments 1 reaction 0 assignees View on GitHub
Module: Analytics P2 Type: Enhancement
Dominant language
JavaScript
Stars
1.4k
Forks
383
Avg merge
4d 12h
Merged PRs (30d)
80

Description

## Feature Description

While developing #7458, we encountered an issue regarding the Redux state mutation using the `state.sort()` method in the `getAnalyticsConfigByMeasurementIDs` selector. We used cloning as a temporary solution to maintain immutability. However, updating the Babel configuration to support the `toSorted()` method is considered a more efficient and long-term solution for better maintainability and cleaner code.

Update: As discussed [below](https://github.com/google/site-kit-wp/issues/7571#issuecomment-1942154324), we are going to hold off on this one until we have upgraded, or are in a position to upgrade `@wordpress/babel-preset-default` to version `7.22.0` or above. Issues #6026 and #6357 may facilitate this, so have been added as speculative dependencies.

---------------

_Do not alter or remove anything below. The following sections will be managed by moderators only._

## Acceptance criteria

* Update the Babel configuration to the version that supports `toSorted()`.
* Replace the current use of cloning in the [`getAnalyticsConfigByMeasurementIDs`](https://github.com/google/site-kit-wp/blob/3e4b803f2d6a77f2892efdafad2af6d472096a78/assets/js/modules/analytics-4/datastore/webdatastreams.js#L457-L459) selector being implemented in #7458 with `toSorted()` and verify that it works as intended.
* The new Babel setup shouldn't break the existing functionalities or introduce regressions.

## Implementation Brief

* Add `core-js@3.28.0` as a dev dependency to for this version to be installed.
* Import the core-js toSorted polyfill with `import 'core-js/actual/array/to-sorted';` into `assets/js/modules/analytics-4/datastore/webdatastreams.js`
* Replace `summaries.sort(...` in [`getAnalyticsConfigByMeasurementIDs`](https://github.com/google/site-kit-wp/blob/3e4b803f2d6a77f2892efdafad2af6d472096a78/assets/js/modules/analytics-4/datastore/webdatastreams.js#L457-L459) with `const sortedSummaries = toSorted(...` and update the use of `summaries` below to use `sortedSummaries`.

### Test Coverage

* No additional tests are required but confirm that tests can run successfully using node 14. Specifically focussing on the `assets/js/modules/analytics-4/datastore/webdatastreams.test.js` tests.

## QA Brief

*

## Changelog entry

*

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.