Skip to content

Commit aa31e85

Browse files
committed
Don’t destructure require from react to improve the esbuild bundle size.
See evanw/esbuild#1183 .
1 parent 5ed4b4c commit aa31e85

17 files changed

Lines changed: 47 additions & 46 deletions

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Test the bundle size using [`esbuild`](https://npm.im/esbuild) instead of [`webpack`](https://npm.im/webpack) and [`disposable-directory`](https://npm.im/disposable-directory).
1818
- Increased the documented bundle size to “< 4 kB” to match that of [`esbuild`](https://npm.im/esbuild) instead of [`webpack`](https://npm.im/webpack).
1919
- Use the correct `kB` symbol instead of `KB` wherever bundle size is mentioned in the package description and readme.
20+
- Don’t destructure `require` from [`react`](https://npm.im/react) to slightly improve the [`esbuild`](https://npm.im/esbuild) bundle size.
2021
- Updated the [example Next.js app](https://graphql-react.vercel.app) URL in the readme.
2122

2223
## 13.0.0

private/useForceUpdate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useReducer } = require('react');
3+
const React = require('react');
44

55
/**
66
* A React hook to force the component to update and re-render.
@@ -12,5 +12,5 @@ const { useReducer } = require('react');
1212
* @ignore
1313
*/
1414
module.exports = function useForceUpdate() {
15-
return useReducer(() => Symbol())[1];
15+
return React.useReducer(() => Symbol())[1];
1616
};

public/CacheContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { createContext } = require('react');
3+
const React = require('react');
44

55
/**
66
* React context for a [`Cache`]{@link Cache} instance.
@@ -26,7 +26,7 @@ const { createContext } = require('react');
2626
* const CacheContext = require('graphql-react/public/CacheContext.js');
2727
* ```
2828
*/
29-
const CacheContext = createContext();
29+
const CacheContext = React.createContext();
3030

3131
if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
3232
CacheContext.displayName = 'CacheContext';

public/HydrationTimeStampContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { createContext } = require('react');
3+
const React = require('react');
44

55
/**
66
* React context for the client side hydration [time stamp]{@link HighResTimeStamp}.
@@ -26,7 +26,7 @@ const { createContext } = require('react');
2626
* const HydrationTimeStampContext = require('graphql-react/public/HydrationTimeStampContext.js');
2727
* ```
2828
*/
29-
const HydrationTimeStampContext = createContext();
29+
const HydrationTimeStampContext = React.createContext();
3030

3131
if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
3232
HydrationTimeStampContext.displayName = 'HydrationTimeStampContext';

public/LoadingContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { createContext } = require('react');
3+
const React = require('react');
44

55
/**
66
* React context for a [`Loading`]{@link Loading} instance.
@@ -26,7 +26,7 @@ const { createContext } = require('react');
2626
* const LoadingContext = require('graphql-react/public/LoadingContext.js');
2727
* ```
2828
*/
29-
const LoadingContext = createContext();
29+
const LoadingContext = React.createContext();
3030

3131
if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
3232
LoadingContext.displayName = 'LoadingContext';

public/Provider.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useRef } = require('react');
3+
const React = require('react');
44
const { jsx } = require('react/jsx-runtime');
55
const Cache = require('./Cache');
66
const CacheContext = require('./CacheContext');
@@ -48,11 +48,11 @@ const LoadingContext = require('./LoadingContext');
4848
* ```
4949
*/
5050
module.exports = function Provider({ cache, children }) {
51-
const hydrationTimeStampRef = useRef();
51+
const hydrationTimeStampRef = React.useRef();
5252
if (!hydrationTimeStampRef.current)
5353
hydrationTimeStampRef.current = performance.now();
5454

55-
const loadingRef = useRef();
55+
const loadingRef = React.useRef();
5656
if (!loadingRef.current) loadingRef.current = new Loading();
5757

5858
if (!(cache instanceof Cache))

public/useAutoAbortLoad.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useCallback, useEffect, useRef } = require('react');
3+
const React = require('react');
44
const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
55

66
/**
@@ -37,9 +37,9 @@ module.exports = function useAutoAbortLoad(load) {
3737
: createArgErrorMessageProd(1)
3838
);
3939

40-
const lastLoadingCacheValueRef = useRef();
40+
const lastLoadingCacheValueRef = React.useRef();
4141

42-
useEffect(
42+
React.useEffect(
4343
() => () => {
4444
if (lastLoadingCacheValueRef.current)
4545
// Abort the last loading as it’s now redundant due to the changed
@@ -50,7 +50,7 @@ module.exports = function useAutoAbortLoad(load) {
5050
[load]
5151
);
5252

53-
return useCallback(() => {
53+
return React.useCallback(() => {
5454
if (lastLoadingCacheValueRef.current)
5555
// Ensure the last loading is aborted before starting new loading.
5656
// Checking if it’s already ended or aborted first is unnecessary.

public/useCache.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useContext, useDebugValue } = require('react');
3+
const React = require('react');
44
const Cache = require('./Cache');
55
const CacheContext = require('./CacheContext');
66

@@ -27,11 +27,11 @@ const CacheContext = require('./CacheContext');
2727
* ```
2828
*/
2929
module.exports = function useCache() {
30-
const cache = useContext(CacheContext);
30+
const cache = React.useContext(CacheContext);
3131

3232
if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
3333
// eslint-disable-next-line react-hooks/rules-of-hooks
34-
useDebugValue(cache);
34+
React.useDebugValue(cache);
3535

3636
if (cache === undefined) throw new TypeError('Cache context missing.');
3737

public/useCacheEntry.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useCallback, useDebugValue, useEffect } = require('react');
3+
const React = require('react');
44
const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
55
const useForceUpdate = require('../private/useForceUpdate');
66
const useCache = require('./useCache');
@@ -40,11 +40,11 @@ module.exports = function useCacheEntry(cacheKey) {
4040
const cache = useCache();
4141
const forceUpdate = useForceUpdate();
4242

43-
const onTriggerUpdate = useCallback(() => {
43+
const onTriggerUpdate = React.useCallback(() => {
4444
forceUpdate();
4545
}, [forceUpdate]);
4646

47-
useEffect(() => {
47+
React.useEffect(() => {
4848
const eventNameSet = `${cacheKey}/set`;
4949
const eventNameDelete = `${cacheKey}/delete`;
5050

@@ -61,7 +61,7 @@ module.exports = function useCacheEntry(cacheKey) {
6161

6262
if (typeof process === 'object' && process.env.NODE_ENV !== 'production')
6363
// eslint-disable-next-line react-hooks/rules-of-hooks
64-
useDebugValue(value);
64+
React.useDebugValue(value);
6565

6666
return value;
6767
};

public/useCacheEntryPrunePrevention.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { useCallback, useEffect } = require('react');
3+
const React = require('react');
44
const createArgErrorMessageProd = require('../private/createArgErrorMessageProd');
55
const useCache = require('./useCache');
66

@@ -38,11 +38,11 @@ module.exports = function useCacheEntryPrunePrevention(cacheKey) {
3838

3939
const cache = useCache();
4040

41-
const onCacheEntryPrune = useCallback((event) => {
41+
const onCacheEntryPrune = React.useCallback((event) => {
4242
event.preventDefault();
4343
}, []);
4444

45-
useEffect(() => {
45+
React.useEffect(() => {
4646
const eventNamePrune = `${cacheKey}/prune`;
4747

4848
cache.addEventListener(eventNamePrune, onCacheEntryPrune);

0 commit comments

Comments
 (0)