Skip to content

Commit 78d2e9e

Browse files
authored
Replace DevTools semver usages with compare-versions for smaller bundle size (#26122)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn debug-test --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary This PR: - Replaces the existing usages of methods from the `semver` library in the React DevTools source with an inlined version based on https://www.npmjs.com/package/semver-compare. This appears to drop the unminified bundle sizes of 3 separate `react-devtools-extensions` build artifacts by about 50K: ![image](https://user-images.githubusercontent.com/1128784/217326947-4c26d1be-d834-4f77-9e6e-be2d5ed0954d.png) ## How did you test this change? I was originally working on [a fork of React DevTools](replayio#2) for use with https://replay.io , specifically our integration of the React DevTools UI to show the React component tree while users are debugging a recorded application. As part of the dev work on that fork, I wanted to shrink the bundle size of the extension's generated JS build artifacts. I noted that the official NPM `semver` library was taking up a noticeable chunk of space in the bundles, and saw that it's only being used in a handful of places to do some very simple version string comparisons. I was able to replace the `semver` imports and usages with a simple alternate comparison function, and confirmed via hands-on checks and console logging that the checks behaved the same way. Given that, I wanted to upstream this particular change to help shrink the real extension's bundle sizes. I know that it's an extension, so bundle size isn't _as_ critical a concern as it would be for a pure library. But, smaller download sizes do benefit all users, and that also includes sites like CodeSandbox and Replay that are using the React DevTools as a library as well. I'm happy to tweak this PR if necessary. Thanks!
1 parent a3152ed commit 78d2e9e

6 files changed

Lines changed: 35 additions & 4 deletions

File tree

packages/react-devtools-shared/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"@reach/menu-button": "^0.16.1",
1919
"@reach/tooltip": "^0.16.0",
2020
"clipboard-js": "^0.3.6",
21+
"compare-versions": "^5.0.3",
2122
"json5": "^2.1.3",
2223
"local-storage-fallback": "^4.1.1",
2324
"lodash.throttle": "^4.1.1",
2425
"memoize-one": "^3.1.1",
25-
"react-virtualized-auto-sizer": "^1.0.6",
26-
"semver": "^6.3.0"
26+
"react-virtualized-auto-sizer": "^1.0.6"
2727
}
2828
}

packages/react-devtools-shared/src/__tests__/utils-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {stackToComponentSources} from 'react-devtools-shared/src/devtools/utils'
1515
import {
1616
format,
1717
formatWithStyles,
18+
gt,
19+
gte,
1820
} from 'react-devtools-shared/src/backend/utils';
1921
import {
2022
REACT_SUSPENSE_LIST_TYPE as SuspenseList,
@@ -252,4 +254,18 @@ describe('utils', () => {
252254
]);
253255
});
254256
});
257+
258+
describe('semver comparisons', () => {
259+
it('gte should compare versions correctly', () => {
260+
expect(gte('1.2.3', '1.2.1')).toBe(true);
261+
expect(gte('1.2.1', '1.2.1')).toBe(true);
262+
expect(gte('1.2.1', '1.2.2')).toBe(false);
263+
});
264+
265+
it('gt should compare versions correctly', () => {
266+
expect(gt('1.2.3', '1.2.1')).toBe(true);
267+
expect(gt('1.2.1', '1.2.1')).toBe(false);
268+
expect(gt('1.2.1', '1.2.2')).toBe(false);
269+
});
270+
});
255271
});

packages/react-devtools-shared/src/backend/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @flow
88
*/
99

10-
import {gt, gte} from 'semver';
1110
import {
1211
ComponentFilterDisplayName,
1312
ComponentFilterElementType,
@@ -39,6 +38,7 @@ import {
3938
utfEncodeString,
4039
} from 'react-devtools-shared/src/utils';
4140
import {sessionStorageGetItem} from 'react-devtools-shared/src/storage';
41+
import {gt, gte} from 'react-devtools-shared/src/backend/utils';
4242
import {
4343
cleanForBridge,
4444
copyToClipboard,

packages/react-devtools-shared/src/backend/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import {copy} from 'clipboard-js';
12+
import {compareVersions} from 'compare-versions';
1213
import {dehydrate} from '../hydration';
1314
import isArray from 'shared/isArray';
1415

@@ -275,3 +276,11 @@ export function isSynchronousXHRSupported(): boolean {
275276
window.document.featurePolicy.allowsFeature('sync-xhr')
276277
);
277278
}
279+
280+
export function gt(a: string = '', b: string = ''): boolean {
281+
return compareVersions(a, b) === 1;
282+
}
283+
284+
export function gte(a: string = '', b: string = ''): boolean {
285+
return compareVersions(a, b) > -1;
286+
}

packages/react-devtools-shell/src/e2e-regression/app-legacy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import * as React from 'react';
66
import * as ReactDOM from 'react-dom';
7-
import {gte} from 'semver';
87
import ListApp from '../e2e-apps/ListApp';
98
import ListAppLegacy from '../e2e-apps/ListAppLegacy';
9+
import {gte} from 'react-devtools-shared/src/backend/utils';
10+
1011
const version = process.env.E2E_APP_REACT_VERSION;
1112

1213
function mountApp(App: () => React$Node) {

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5458,6 +5458,11 @@ commondir@^1.0.1:
54585458
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
54595459
integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=
54605460

5461+
compare-versions@^5.0.3:
5462+
version "5.0.3"
5463+
resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-5.0.3.tgz#a9b34fea217472650ef4a2651d905f42c28ebfd7"
5464+
integrity sha512-4UZlZP8Z99MGEY+Ovg/uJxJuvoXuN4M6B3hKaiackiHrgzQFEe3diJi1mf1PNHbFujM7FvLrK2bpgIaImbtZ1A==
5465+
54615466
component-emitter@^1.2.1:
54625467
version "1.3.0"
54635468
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"

0 commit comments

Comments
 (0)