From 1ad8d81292415e26ac070dec03ad84c11fbe207d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Wed, 23 Feb 2022 19:34:24 -0500 Subject: [PATCH] Remove object-assign polyfill (#23351) * Remove object-assign polyfill We really rely on a more modern environment where this is typically polyfilled anyway and we don't officially support IE with more extensive polyfilling anyway. So all environments should have the native version by now. * Use shared/assign instead of Object.assign in code This is so that we have one cached local instance in the bundle. Ideally we should have a compile do this for us but we already follow this pattern with hasOwnProperty, isArray, Object.is etc. * Transform Object.assign to now use shared/assign We need this to use the shared instance when Object.spread is used. --- package.json | 1 - packages/jest-react/package.json | 3 -- packages/react-art/npm/Circle.js | 2 +- packages/react-art/npm/Rectangle.js | 2 +- packages/react-art/npm/Wedge.js | 2 +- packages/react-art/package.json | 1 - packages/react-client/package.json | 3 +- packages/react-debug-tools/package.json | 3 +- .../react-debug-tools/src/ReactDebugHooks.js | 3 +- .../src/backend/views/Highlighter/Overlay.js | 3 +- packages/react-dom/package.json | 1 - .../react-dom/src/client/ReactDOMInput.js | 3 +- .../react-dom/src/client/ReactDOMSelect.js | 3 +- .../react-dom/src/events/SyntheticEvent.js | 3 +- .../src/server/ReactPartialRenderer.js | 21 ++++++------ .../src/test-utils/ReactTestUtils.js | 3 +- packages/react-native-renderer/package.json | 1 - .../src/legacy-events/SyntheticEvent.js | 8 +++-- packages/react-noop-renderer/package.json | 1 - packages/react-reconciler/package.json | 1 - .../src/ReactFiberClassComponent.new.js | 3 +- .../src/ReactFiberClassComponent.old.js | 3 +- .../src/ReactFiberLazyComponent.new.js | 4 ++- .../src/ReactFiberLazyComponent.old.js | 4 ++- .../src/ReactUpdateQueue.new.js | 4 ++- .../src/ReactUpdateQueue.old.js | 4 ++- packages/react-server-dom-relay/package.json | 1 - .../react-server-dom-webpack/package.json | 3 +- .../react-server-native-relay/package.json | 1 - packages/react-server/package.json | 3 +- .../src/ReactFizzClassComponent.js | 7 ++-- packages/react-server/src/ReactFizzServer.js | 3 +- packages/react-test-renderer/package.json | 1 - packages/react/package.json | 3 +- packages/react/src/ReactBaseClasses.js | 3 +- packages/react/src/ReactElement.js | 3 +- packages/react/src/ReactSharedInternals.js | 3 -- .../src/forks/ReactSharedInternals.umd.js | 3 -- packages/scheduler/package.json | 3 +- .../{forks/object-assign.umd.js => assign.js} | 6 ++-- .../shared/forks/object-assign.inline-umd.js | 31 ------------------ packages/use-subscription/package.json | 3 -- ...-require.js => transform-object-assign.js} | 10 +++--- scripts/rollup/build.js | 20 ++---------- scripts/rollup/forks.js | 32 ------------------- 45 files changed, 73 insertions(+), 156 deletions(-) rename packages/shared/{forks/object-assign.umd.js => assign.js} (57%) delete mode 100644 packages/shared/forks/object-assign.inline-umd.js rename scripts/babel/{transform-object-assign-require.js => transform-object-assign.js} (77%) diff --git a/package.json b/package.json index 9dc97a27eb429..ec226416e2117 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,6 @@ "minimist": "^1.2.3", "mkdirp": "^0.5.1", "ncp": "^2.0.0", - "object-assign": "^4.1.1", "pacote": "^10.3.0", "prettier": "1.19.1", "prop-types": "^15.6.2", diff --git a/packages/jest-react/package.json b/packages/jest-react/package.json index 0a1c764be2fe7..78907eeb32c77 100644 --- a/packages/jest-react/package.json +++ b/packages/jest-react/package.json @@ -23,9 +23,6 @@ "react": "^17.0.0", "react-test-renderer": "^17.0.0" }, - "dependencies": { - "object-assign": "^4.1.1" - }, "files": [ "LICENSE", "README.md", diff --git a/packages/react-art/npm/Circle.js b/packages/react-art/npm/Circle.js index 1100a3e5a5b9f..4e0cca3a47eb9 100644 --- a/packages/react-art/npm/Circle.js +++ b/packages/react-art/npm/Circle.js @@ -17,7 +17,7 @@ 'use strict'; -var assign = require('object-assign'); +var assign = Object.assign; var PropTypes = require('prop-types'); var React = require('react'); var ReactART = require('react-art'); diff --git a/packages/react-art/npm/Rectangle.js b/packages/react-art/npm/Rectangle.js index 54b33e69a1830..89f443e9ceecc 100644 --- a/packages/react-art/npm/Rectangle.js +++ b/packages/react-art/npm/Rectangle.js @@ -24,7 +24,7 @@ 'use strict'; -var assign = require('object-assign'); +var assign = Object.assign; var PropTypes = require('prop-types'); var React = require('react'); var ReactART = require('react-art'); diff --git a/packages/react-art/npm/Wedge.js b/packages/react-art/npm/Wedge.js index 91c8bc4158b8e..90de17e071f61 100644 --- a/packages/react-art/npm/Wedge.js +++ b/packages/react-art/npm/Wedge.js @@ -20,7 +20,7 @@ 'use strict'; -var assign = require('object-assign'); +var assign = Object.assign; var PropTypes = require('prop-types'); var React = require('react'); var ReactART = require('react-art'); diff --git a/packages/react-art/package.json b/packages/react-art/package.json index fabdcd560f9ce..a819d3eb269a2 100644 --- a/packages/react-art/package.json +++ b/packages/react-art/package.json @@ -25,7 +25,6 @@ "art": "^0.10.1", "create-react-class": "^15.6.2", "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", "scheduler": "^0.20.1" }, "peerDependencies": { diff --git a/packages/react-client/package.json b/packages/react-client/package.json index 7752fcb73f8a5..64670a870d8a8 100644 --- a/packages/react-client/package.json +++ b/packages/react-client/package.json @@ -27,8 +27,7 @@ "react": "^17.0.0" }, "dependencies": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" + "loose-envify": "^1.1.0" }, "browserify": { "transform": [ diff --git a/packages/react-debug-tools/package.json b/packages/react-debug-tools/package.json index 69327edaeb1f3..f3ee5806f722a 100644 --- a/packages/react-debug-tools/package.json +++ b/packages/react-debug-tools/package.json @@ -28,7 +28,6 @@ "react": "^17.0.0" }, "dependencies": { - "error-stack-parser": "^2.0.2", - "object-assign": "^4.1.1" + "error-stack-parser": "^2.0.2" } } diff --git a/packages/react-debug-tools/src/ReactDebugHooks.js b/packages/react-debug-tools/src/ReactDebugHooks.js index 895e75359867c..0ffec00bd3f8d 100644 --- a/packages/react-debug-tools/src/ReactDebugHooks.js +++ b/packages/react-debug-tools/src/ReactDebugHooks.js @@ -20,6 +20,7 @@ import type { } from 'react-reconciler/src/ReactInternalTypes'; import ErrorStackParser from 'error-stack-parser'; +import assign from 'shared/assign'; import ReactSharedInternals from 'shared/ReactSharedInternals'; import { FunctionComponent, @@ -720,7 +721,7 @@ function inspectHooksOfForwardRef( function resolveDefaultProps(Component, baseProps) { if (Component && Component.defaultProps) { // Resolve default props. Taken from ReactElement - const props = Object.assign({}, baseProps); + const props = assign({}, baseProps); const defaultProps = Component.defaultProps; for (const propName in defaultProps) { if (props[propName] === undefined) { diff --git a/packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js b/packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js index 7e6cc595654b8..ba763562a1629 100644 --- a/packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js +++ b/packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js @@ -7,9 +7,10 @@ * @flow */ -import assign from 'object-assign'; import {getElementDimensions, getNestedBoundingClientRect} from '../utils'; +const assign = Object.assign; + import type {DevToolsHook} from 'react-devtools-shared/src/backend/types'; import type {Rect} from '../utils'; diff --git a/packages/react-dom/package.json b/packages/react-dom/package.json index 71d15b5670dc5..25207ffc9c39f 100644 --- a/packages/react-dom/package.json +++ b/packages/react-dom/package.json @@ -18,7 +18,6 @@ "homepage": "https://reactjs.org/", "dependencies": { "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", "scheduler": "^0.20.1" }, "peerDependencies": { diff --git a/packages/react-dom/src/client/ReactDOMInput.js b/packages/react-dom/src/client/ReactDOMInput.js index 8f5098405b576..a4b8dede45a73 100644 --- a/packages/react-dom/src/client/ReactDOMInput.js +++ b/packages/react-dom/src/client/ReactDOMInput.js @@ -16,6 +16,7 @@ import {getToStringValue, toString} from './ToStringValue'; import {checkControlledValueProps} from '../shared/ReactControlledValuePropTypes'; import {updateValueIfChanged} from './inputValueTracking'; import getActiveElement from './getActiveElement'; +import assign from 'shared/assign'; import {disableInputAttributeSyncing} from 'shared/ReactFeatureFlags'; import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion'; @@ -62,7 +63,7 @@ export function getHostProps(element: Element, props: Object) { const node = ((element: any): InputWithWrapperState); const checked = props.checked; - const hostProps = Object.assign({}, props, { + const hostProps = assign({}, props, { defaultChecked: undefined, defaultValue: undefined, value: undefined, diff --git a/packages/react-dom/src/client/ReactDOMSelect.js b/packages/react-dom/src/client/ReactDOMSelect.js index df336d4b92254..ece9275e4b37e 100644 --- a/packages/react-dom/src/client/ReactDOMSelect.js +++ b/packages/react-dom/src/client/ReactDOMSelect.js @@ -12,6 +12,7 @@ import {getCurrentFiberOwnerNameInDevOrNull} from 'react-reconciler/src/ReactCur import {checkControlledValueProps} from '../shared/ReactControlledValuePropTypes'; import {getToStringValue, toString} from './ToStringValue'; +import assign from 'shared/assign'; import isArray from 'shared/isArray'; let didWarnValueDefaultValue; @@ -134,7 +135,7 @@ function updateOptions( */ export function getHostProps(element: Element, props: Object) { - return Object.assign({}, props, { + return assign({}, props, { value: undefined, }); } diff --git a/packages/react-dom/src/events/SyntheticEvent.js b/packages/react-dom/src/events/SyntheticEvent.js index 649cab082e61a..3609e6794a50d 100644 --- a/packages/react-dom/src/events/SyntheticEvent.js +++ b/packages/react-dom/src/events/SyntheticEvent.js @@ -9,6 +9,7 @@ /* eslint valid-typeof: 0 */ +import assign from 'shared/assign'; import getEventCharCode from './getEventCharCode'; type EventInterfaceType = { @@ -78,7 +79,7 @@ function createSyntheticEvent(Interface: EventInterfaceType) { return this; } - Object.assign(SyntheticBaseEvent.prototype, { + assign(SyntheticBaseEvent.prototype, { preventDefault: function() { this.defaultPrevented = true; const event = this.nativeEvent; diff --git a/packages/react-dom/src/server/ReactPartialRenderer.js b/packages/react-dom/src/server/ReactPartialRenderer.js index 1902dc65690c8..41c2885680437 100644 --- a/packages/react-dom/src/server/ReactPartialRenderer.js +++ b/packages/react-dom/src/server/ReactPartialRenderer.js @@ -80,6 +80,7 @@ import warnValidStyle from '../shared/warnValidStyle'; import {validateProperties as validateARIAProperties} from '../shared/ReactDOMInvalidARIAHook'; import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook'; import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook'; +import assign from 'shared/assign'; import hasOwnProperty from 'shared/hasOwnProperty'; // Based on reading the React.Children implementation. TODO: type this somewhere? @@ -563,7 +564,7 @@ function resolve( } if (partialState != null) { - inst.state = Object.assign({}, inst.state, partialState); + inst.state = assign({}, inst.state, partialState); } } } else { @@ -695,9 +696,9 @@ function resolve( if (partialState != null) { if (dontMutate) { dontMutate = false; - nextState = Object.assign({}, nextState, partialState); + nextState = assign({}, nextState, partialState); } else { - Object.assign(nextState, partialState); + assign(nextState, partialState); } } } @@ -745,7 +746,7 @@ function resolve( } } if (childContext) { - context = Object.assign({}, context, childContext); + context = assign({}, context, childContext); } } } @@ -1192,7 +1193,7 @@ class ReactDOMServerRenderer { const nextChildren = [ React.createElement( elementType.type, - Object.assign({ref: element.ref}, element.props), + assign({ref: element.ref}, element.props), ), ]; const frame: Frame = { @@ -1291,7 +1292,7 @@ class ReactDOMServerRenderer { const nextChildren = [ React.createElement( result, - Object.assign({ref: element.ref}, element.props), + assign({ref: element.ref}, element.props), ), ]; const frame: Frame = { @@ -1413,7 +1414,7 @@ class ReactDOMServerRenderer { } } - props = Object.assign( + props = assign( { type: undefined, }, @@ -1485,7 +1486,7 @@ class ReactDOMServerRenderer { if (__DEV__) { checkFormFieldValueStringCoercion(initialValue); } - props = Object.assign({}, props, { + props = assign({}, props, { value: undefined, children: '' + initialValue, }); @@ -1531,7 +1532,7 @@ class ReactDOMServerRenderer { } this.currentSelectValue = props.value != null ? props.value : props.defaultValue; - props = Object.assign({}, props, { + props = assign({}, props, { value: undefined, }); } else if (tag === 'option') { @@ -1577,7 +1578,7 @@ class ReactDOMServerRenderer { selected = '' + selectValue === value; } - props = Object.assign( + props = assign( { selected: undefined, }, diff --git a/packages/react-dom/src/test-utils/ReactTestUtils.js b/packages/react-dom/src/test-utils/ReactTestUtils.js index c83b072447ba7..2c89baf398a90 100644 --- a/packages/react-dom/src/test-utils/ReactTestUtils.js +++ b/packages/react-dom/src/test-utils/ReactTestUtils.js @@ -21,6 +21,7 @@ import { rethrowCaughtError, invokeGuardedCallbackAndCatchFirstError, } from 'shared/ReactErrorUtils'; +import assign from 'shared/assign'; import isArray from 'shared/isArray'; // Keep in sync with ReactDOM.js: @@ -596,7 +597,7 @@ function makeSimulator(eventType) { // Since we aren't using pooling, always persist the event. This will make // sure it's marked and won't warn when setting additional properties. event.persist(); - Object.assign(event, eventData); + assign(event, eventData); if (directDispatchEventTypes.has(eventType)) { accumulateDirectDispatchesSingle(event); diff --git a/packages/react-native-renderer/package.json b/packages/react-native-renderer/package.json index bc5066c43ba0c..9df888773f2ca 100644 --- a/packages/react-native-renderer/package.json +++ b/packages/react-native-renderer/package.json @@ -8,7 +8,6 @@ "directory": "packages/react-native-renderer" }, "dependencies": { - "object-assign": "^4.1.1", "scheduler": "^0.11.0" }, "peerDependencies": { diff --git a/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js b/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js index 84d39bbd603d2..9e3009d9d8d06 100644 --- a/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js +++ b/packages/react-native-renderer/src/legacy-events/SyntheticEvent.js @@ -7,6 +7,8 @@ /* eslint valid-typeof: 0 */ +import assign from 'shared/assign'; + const EVENT_POOL_SIZE = 10; /** @@ -110,7 +112,7 @@ function SyntheticEvent( return this; } -Object.assign(SyntheticEvent.prototype, { +assign(SyntheticEvent.prototype, { preventDefault: function() { this.defaultPrevented = true; const event = this.nativeEvent; @@ -236,11 +238,11 @@ SyntheticEvent.extend = function(Interface) { function Class() { return Super.apply(this, arguments); } - Object.assign(prototype, Class.prototype); + assign(prototype, Class.prototype); Class.prototype = prototype; Class.prototype.constructor = Class; - Class.Interface = Object.assign({}, Super.Interface, Interface); + Class.Interface = assign({}, Super.Interface, Interface); Class.extend = Super.extend; addEventPoolingTo(Class); diff --git a/packages/react-noop-renderer/package.json b/packages/react-noop-renderer/package.json index 22eab24f8c5a4..aae14070714c0 100644 --- a/packages/react-noop-renderer/package.json +++ b/packages/react-noop-renderer/package.json @@ -11,7 +11,6 @@ }, "license": "MIT", "dependencies": { - "object-assign": "^4.1.1", "react-reconciler": "*", "react-client": "*", "react-server": "*" diff --git a/packages/react-reconciler/package.json b/packages/react-reconciler/package.json index a6b9986adaea3..9dc5ae52bcc88 100644 --- a/packages/react-reconciler/package.json +++ b/packages/react-reconciler/package.json @@ -30,7 +30,6 @@ }, "dependencies": { "loose-envify": "^1.1.0", - "object-assign": "^4.1.1", "scheduler": "^0.20.1" }, "browserify": { diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.new.js b/packages/react-reconciler/src/ReactFiberClassComponent.new.js index 0279cabd519be..23bc16ecce99a 100644 --- a/packages/react-reconciler/src/ReactFiberClassComponent.new.js +++ b/packages/react-reconciler/src/ReactFiberClassComponent.new.js @@ -35,6 +35,7 @@ import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap'; import shallowEqual from 'shared/shallowEqual'; import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber'; import getComponentNameFromType from 'shared/getComponentNameFromType'; +import assign from 'shared/assign'; import isArray from 'shared/isArray'; import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols'; @@ -186,7 +187,7 @@ function applyDerivedStateFromProps( const memoizedState = partialState === null || partialState === undefined ? prevState - : Object.assign({}, prevState, partialState); + : assign({}, prevState, partialState); workInProgress.memoizedState = memoizedState; // Once the update queue is empty, persist the derived state onto the diff --git a/packages/react-reconciler/src/ReactFiberClassComponent.old.js b/packages/react-reconciler/src/ReactFiberClassComponent.old.js index 184993ff35b05..793156401efb7 100644 --- a/packages/react-reconciler/src/ReactFiberClassComponent.old.js +++ b/packages/react-reconciler/src/ReactFiberClassComponent.old.js @@ -35,6 +35,7 @@ import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap'; import shallowEqual from 'shared/shallowEqual'; import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber'; import getComponentNameFromType from 'shared/getComponentNameFromType'; +import assign from 'shared/assign'; import isArray from 'shared/isArray'; import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols'; @@ -186,7 +187,7 @@ function applyDerivedStateFromProps( const memoizedState = partialState === null || partialState === undefined ? prevState - : Object.assign({}, prevState, partialState); + : assign({}, prevState, partialState); workInProgress.memoizedState = memoizedState; // Once the update queue is empty, persist the derived state onto the diff --git a/packages/react-reconciler/src/ReactFiberLazyComponent.new.js b/packages/react-reconciler/src/ReactFiberLazyComponent.new.js index 3773f640439bf..4bf227dc5e802 100644 --- a/packages/react-reconciler/src/ReactFiberLazyComponent.new.js +++ b/packages/react-reconciler/src/ReactFiberLazyComponent.new.js @@ -7,10 +7,12 @@ * @flow */ +import assign from 'shared/assign'; + export function resolveDefaultProps(Component: any, baseProps: Object): Object { if (Component && Component.defaultProps) { // Resolve default props. Taken from ReactElement - const props = Object.assign({}, baseProps); + const props = assign({}, baseProps); const defaultProps = Component.defaultProps; for (const propName in defaultProps) { if (props[propName] === undefined) { diff --git a/packages/react-reconciler/src/ReactFiberLazyComponent.old.js b/packages/react-reconciler/src/ReactFiberLazyComponent.old.js index 3773f640439bf..4bf227dc5e802 100644 --- a/packages/react-reconciler/src/ReactFiberLazyComponent.old.js +++ b/packages/react-reconciler/src/ReactFiberLazyComponent.old.js @@ -7,10 +7,12 @@ * @flow */ +import assign from 'shared/assign'; + export function resolveDefaultProps(Component: any, baseProps: Object): Object { if (Component && Component.defaultProps) { // Resolve default props. Taken from ReactElement - const props = Object.assign({}, baseProps); + const props = assign({}, baseProps); const defaultProps = Component.defaultProps; for (const propName in defaultProps) { if (props[propName] === undefined) { diff --git a/packages/react-reconciler/src/ReactUpdateQueue.new.js b/packages/react-reconciler/src/ReactUpdateQueue.new.js index c9a980890a0ad..1f665a88dfa49 100644 --- a/packages/react-reconciler/src/ReactUpdateQueue.new.js +++ b/packages/react-reconciler/src/ReactUpdateQueue.new.js @@ -112,6 +112,8 @@ import { import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.new'; import {setIsStrictModeForDevtools} from './ReactFiberDevToolsHook.new'; +import assign from 'shared/assign'; + export type Update = {| // TODO: Temporary field. Will remove this by storing a map of // transition -> event time on the root. @@ -442,7 +444,7 @@ function getStateFromUpdate( return prevState; } // Merge the partial state and the previous state. - return Object.assign({}, prevState, partialState); + return assign({}, prevState, partialState); } case ForceUpdate: { hasForceUpdate = true; diff --git a/packages/react-reconciler/src/ReactUpdateQueue.old.js b/packages/react-reconciler/src/ReactUpdateQueue.old.js index 9ef09b17efd10..e59ea26f2161a 100644 --- a/packages/react-reconciler/src/ReactUpdateQueue.old.js +++ b/packages/react-reconciler/src/ReactUpdateQueue.old.js @@ -112,6 +112,8 @@ import { import {pushInterleavedQueue} from './ReactFiberInterleavedUpdates.old'; import {setIsStrictModeForDevtools} from './ReactFiberDevToolsHook.old'; +import assign from 'shared/assign'; + export type Update = {| // TODO: Temporary field. Will remove this by storing a map of // transition -> event time on the root. @@ -442,7 +444,7 @@ function getStateFromUpdate( return prevState; } // Merge the partial state and the previous state. - return Object.assign({}, prevState, partialState); + return assign({}, prevState, partialState); } case ForceUpdate: { hasForceUpdate = true; diff --git a/packages/react-server-dom-relay/package.json b/packages/react-server-dom-relay/package.json index eeceab0da3e04..793363bb30ddc 100644 --- a/packages/react-server-dom-relay/package.json +++ b/packages/react-server-dom-relay/package.json @@ -8,7 +8,6 @@ "directory": "packages/react-server-dom-relay" }, "dependencies": { - "object-assign": "^4.1.1", "scheduler": "^0.11.0" }, "peerDependencies": { diff --git a/packages/react-server-dom-webpack/package.json b/packages/react-server-dom-webpack/package.json index af705d975db70..ababa89250e3c 100644 --- a/packages/react-server-dom-webpack/package.json +++ b/packages/react-server-dom-webpack/package.json @@ -54,8 +54,7 @@ "dependencies": { "acorn": "^6.2.1", "neo-async": "^2.6.1", - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" + "loose-envify": "^1.1.0" }, "browserify": { "transform": [ diff --git a/packages/react-server-native-relay/package.json b/packages/react-server-native-relay/package.json index 1c328bd2ba32f..241913f91e6a7 100644 --- a/packages/react-server-native-relay/package.json +++ b/packages/react-server-native-relay/package.json @@ -8,7 +8,6 @@ "directory": "packages/react-server-native-relay" }, "dependencies": { - "object-assign": "^4.1.1", "scheduler": "^0.11.0" }, "peerDependencies": { diff --git a/packages/react-server/package.json b/packages/react-server/package.json index 3e9252d2615d7..8c5c312a693ac 100644 --- a/packages/react-server/package.json +++ b/packages/react-server/package.json @@ -29,8 +29,7 @@ "react": "^17.0.0" }, "dependencies": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" + "loose-envify": "^1.1.0" }, "browserify": { "transform": [ diff --git a/packages/react-server/src/ReactFizzClassComponent.js b/packages/react-server/src/ReactFizzClassComponent.js index 2e085c3f1a711..12fbeb46f2ded 100644 --- a/packages/react-server/src/ReactFizzClassComponent.js +++ b/packages/react-server/src/ReactFizzClassComponent.js @@ -17,6 +17,7 @@ import { import {get as getInstance, set as setInstance} from 'shared/ReactInstanceMap'; import getComponentNameFromType from 'shared/getComponentNameFromType'; import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols'; +import assign from 'shared/assign'; import isArray from 'shared/isArray'; const didWarnAboutNoopUpdateForComponent = {}; @@ -161,7 +162,7 @@ function applyDerivedStateFromProps( const newState = partialState === null || partialState === undefined ? prevState - : Object.assign({}, prevState, partialState); + : assign({}, prevState, partialState); return newState; } @@ -602,9 +603,9 @@ function processUpdateQueue( if (partialState != null) { if (dontMutate) { dontMutate = false; - nextState = Object.assign({}, nextState, partialState); + nextState = assign({}, nextState, partialState); } else { - Object.assign(nextState, partialState); + assign(nextState, partialState); } } } diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js index bdf9b16a2458f..a7f5ad8346939 100644 --- a/packages/react-server/src/ReactFizzServer.js +++ b/packages/react-server/src/ReactFizzServer.js @@ -117,6 +117,7 @@ import { enableSuspenseAvoidThisFallbackFizz, } from 'shared/ReactFeatureFlags'; +import assign from 'shared/assign'; import getComponentNameFromType from 'shared/getComponentNameFromType'; import isArray from 'shared/isArray'; @@ -837,7 +838,7 @@ function validateFunctionComponentInDev(Component: any): void { function resolveDefaultProps(Component: any, baseProps: Object): Object { if (Component && Component.defaultProps) { // Resolve default props. Taken from ReactElement - const props = Object.assign({}, baseProps); + const props = assign({}, baseProps); const defaultProps = Component.defaultProps; for (const propName in defaultProps) { if (props[propName] === undefined) { diff --git a/packages/react-test-renderer/package.json b/packages/react-test-renderer/package.json index 26e0337641827..814e6b3d264bd 100644 --- a/packages/react-test-renderer/package.json +++ b/packages/react-test-renderer/package.json @@ -19,7 +19,6 @@ }, "homepage": "https://reactjs.org/", "dependencies": { - "object-assign": "^4.1.1", "react-is": "^17.0.3", "react-shallow-renderer": "^16.13.1", "scheduler": "^0.20.1" diff --git a/packages/react/package.json b/packages/react/package.json index 94703e2005102..9d19b02b3202b 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -38,8 +38,7 @@ "node": ">=0.10.0" }, "dependencies": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" + "loose-envify": "^1.1.0" }, "browserify": { "transform": [ diff --git a/packages/react/src/ReactBaseClasses.js b/packages/react/src/ReactBaseClasses.js index e2a004bcc0467..23b8f126146dc 100644 --- a/packages/react/src/ReactBaseClasses.js +++ b/packages/react/src/ReactBaseClasses.js @@ -6,6 +6,7 @@ */ import ReactNoopUpdateQueue from './ReactNoopUpdateQueue'; +import assign from 'shared/assign'; const emptyObject = {}; if (__DEV__) { @@ -139,7 +140,7 @@ function PureComponent(props, context, updater) { const pureComponentPrototype = (PureComponent.prototype = new ComponentDummy()); pureComponentPrototype.constructor = PureComponent; // Avoid an extra prototype jump for these methods. -Object.assign(pureComponentPrototype, Component.prototype); +assign(pureComponentPrototype, Component.prototype); pureComponentPrototype.isPureReactComponent = true; export {Component, PureComponent}; diff --git a/packages/react/src/ReactElement.js b/packages/react/src/ReactElement.js index e87f31d1ae36c..4af5a8bafb5ae 100644 --- a/packages/react/src/ReactElement.js +++ b/packages/react/src/ReactElement.js @@ -7,6 +7,7 @@ import getComponentNameFromType from 'shared/getComponentNameFromType'; import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols'; +import assign from 'shared/assign'; import hasOwnProperty from 'shared/hasOwnProperty'; import {checkKeyStringCoercion} from 'shared/CheckStringCoercion'; @@ -492,7 +493,7 @@ export function cloneElement(element, config, children) { let propName; // Original props are copied - const props = Object.assign({}, element.props); + const props = assign({}, element.props); // Reserved names are extracted let key = element.key; diff --git a/packages/react/src/ReactSharedInternals.js b/packages/react/src/ReactSharedInternals.js index 3443fb276633c..2874b03985b9e 100644 --- a/packages/react/src/ReactSharedInternals.js +++ b/packages/react/src/ReactSharedInternals.js @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import assign from 'object-assign'; import ReactCurrentDispatcher from './ReactCurrentDispatcher'; import ReactCurrentBatchConfig from './ReactCurrentBatchConfig'; import ReactCurrentActQueue from './ReactCurrentActQueue'; @@ -16,8 +15,6 @@ const ReactSharedInternals = { ReactCurrentDispatcher, ReactCurrentBatchConfig, ReactCurrentOwner, - // Used by renderers to avoid bundling object-assign twice in UMD bundles: - assign, }; if (__DEV__) { diff --git a/packages/react/src/forks/ReactSharedInternals.umd.js b/packages/react/src/forks/ReactSharedInternals.umd.js index 5dcfbb5650e63..04e8cb577f7ab 100644 --- a/packages/react/src/forks/ReactSharedInternals.umd.js +++ b/packages/react/src/forks/ReactSharedInternals.umd.js @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import assign from 'object-assign'; import * as Scheduler from 'scheduler'; import ReactCurrentDispatcher from '../ReactCurrentDispatcher'; import ReactCurrentActQueue from '../ReactCurrentActQueue'; @@ -17,8 +16,6 @@ const ReactSharedInternals = { ReactCurrentDispatcher, ReactCurrentOwner, ReactCurrentBatchConfig, - // Used by renderers to avoid bundling object-assign twice in UMD bundles: - assign, // Re-export the schedule API(s) for UMD bundles. // This avoids introducing a dependency on a new UMD global in a minor update, diff --git a/packages/scheduler/package.json b/packages/scheduler/package.json index 3f88ee56bbf72..50f5778a833f3 100644 --- a/packages/scheduler/package.json +++ b/packages/scheduler/package.json @@ -17,8 +17,7 @@ }, "homepage": "https://reactjs.org/", "dependencies": { - "loose-envify": "^1.1.0", - "object-assign": "^4.1.1" + "loose-envify": "^1.1.0" }, "files": [ "LICENSE", diff --git a/packages/shared/forks/object-assign.umd.js b/packages/shared/assign.js similarity index 57% rename from packages/shared/forks/object-assign.umd.js rename to packages/shared/assign.js index 3e5d03c3cca85..b9666e5e8b415 100644 --- a/packages/shared/forks/object-assign.umd.js +++ b/packages/shared/assign.js @@ -7,8 +7,6 @@ * @flow */ -import * as React from 'react'; +const assign = Object.assign; -const ReactInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; - -export default ReactInternals.assign; +export default assign; diff --git a/packages/shared/forks/object-assign.inline-umd.js b/packages/shared/forks/object-assign.inline-umd.js deleted file mode 100644 index 553fa44f6b0aa..0000000000000 --- a/packages/shared/forks/object-assign.inline-umd.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2004-present Facebook. All Rights Reserved. - */ - -const hasOwnProperty = Object.prototype.hasOwnProperty; - -const _assign = function(to, from) { - for (const key in from) { - if (hasOwnProperty.call(from, key)) { - to[key] = from[key]; - } - } -}; - -export default Object.assign || - function(target, sources) { - if (target == null) { - throw new TypeError('Object.assign target cannot be null or undefined'); - } - - const to = Object(target); - - for (let nextIndex = 1; nextIndex < arguments.length; nextIndex++) { - const nextSource = arguments[nextIndex]; - if (nextSource != null) { - _assign(to, Object(nextSource)); - } - } - - return to; - }; diff --git a/packages/use-subscription/package.json b/packages/use-subscription/package.json index 0c270f5e08ee1..cad898b5f646d 100644 --- a/packages/use-subscription/package.json +++ b/packages/use-subscription/package.json @@ -14,9 +14,6 @@ "cjs/" ], "license": "MIT", - "dependencies": { - "object-assign": "^4.1.1" - }, "peerDependencies": { "react": "^16.8.0 || ^17.0.0" }, diff --git a/scripts/babel/transform-object-assign-require.js b/scripts/babel/transform-object-assign.js similarity index 77% rename from scripts/babel/transform-object-assign-require.js rename to scripts/babel/transform-object-assign.js index 8b43be53e700c..9b20675f39f13 100644 --- a/scripts/babel/transform-object-assign-require.js +++ b/scripts/babel/transform-object-assign.js @@ -14,7 +14,7 @@ module.exports = function autoImporter(babel) { if (state.id) { return state.id; } - state.id = helperModuleImports.addDefault(path, 'object-assign', { + state.id = helperModuleImports.addDefault(path, 'shared/assign', { nameHint: 'assign', }); return state.id; @@ -28,8 +28,8 @@ module.exports = function autoImporter(babel) { visitor: { CallExpression: function(path, file) { - if (file.filename.indexOf('object-assign') !== -1) { - // Don't replace Object.assign if we're transforming object-assign + if (file.filename.indexOf('shared/assign') !== -1) { + // Don't replace Object.assign if we're transforming shared/assign return; } if (path.get('callee').matchesPattern('Object.assign')) { @@ -40,8 +40,8 @@ module.exports = function autoImporter(babel) { }, MemberExpression: function(path, file) { - if (file.filename.indexOf('object-assign') !== -1) { - // Don't replace Object.assign if we're transforming object-assign + if (file.filename.indexOf('shared/assign') !== -1) { + // Don't replace Object.assign if we're transforming shared/assign return; } if (path.matchesPattern('Object.assign')) { diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 48401c002b431..c42edbe7eea64 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -8,7 +8,6 @@ const prettier = require('rollup-plugin-prettier'); const replace = require('rollup-plugin-replace'); const stripBanner = require('rollup-plugin-strip-banner'); const chalk = require('chalk'); -const path = require('path'); const resolve = require('rollup-plugin-node-resolve'); const fs = require('fs'); const argv = require('minimist')(process.argv.slice(2)); @@ -127,6 +126,8 @@ const babelPlugins = [ '@babel/plugin-transform-parameters', // TODO: Remove array destructuring from the source. Requires runtime. ['@babel/plugin-transform-destructuring', {loose: true, useBuiltIns: true}], + // Transform Object spread to shared/assign + require('../babel/transform-object-assign'), ]; const babelToES5Plugins = [ @@ -177,22 +178,7 @@ function getBabelConfig( options.plugins.push(require('../error-codes/transform-error-messages')); } - switch (bundleType) { - case UMD_DEV: - case UMD_PROD: - case UMD_PROFILING: - case NODE_DEV: - case NODE_PROD: - case NODE_PROFILING: - return Object.assign({}, options, { - plugins: options.plugins.concat([ - // Use object-assign polyfill in open source - path.resolve('./scripts/babel/transform-object-assign-require'), - ]), - }); - default: - return options; - } + return options; } function getRollupOutputOptions( diff --git a/scripts/rollup/forks.js b/scripts/rollup/forks.js index eba8f0d684b69..d45c41a74268d 100644 --- a/scripts/rollup/forks.js +++ b/scripts/rollup/forks.js @@ -36,38 +36,6 @@ const __EXPERIMENTAL__ = // algorithm because 1) require.resolve doesn't work with ESM modules, and 2) // the behavior is easier to predict. const forks = Object.freeze({ - // Optimization: for UMDs, use a version that we can inline into the React bundle. - // Use that from all other bundles. - - // NOTE: This is hard-coded to the main entry point of the (third-party) - // object-assign package. - './node_modules/object-assign/index.js': ( - bundleType, - entry, - dependencies - ) => { - if ( - bundleType !== UMD_DEV && - bundleType !== UMD_PROD && - bundleType !== UMD_PROFILING - ) { - // It's only relevant for UMD bundles since that's where the duplication - // happens. Other bundles just require('object-assign') anyway. - return null; - } - if (entry === 'react' || entry === 'react/src/ReactSharedSubset.js') { - // Use the forked version that uses ES modules instead of CommonJS. - return './packages/shared/forks/object-assign.inline-umd.js'; - } - if (dependencies.indexOf('react') === -1) { - // We can only apply the optimizations to bundle that depend on React - // because we read assign() from an object exposed on React internals. - return null; - } - // We can use the fork that reads the secret export! - return './packages/shared/forks/object-assign.umd.js'; - }, - // NOTE: This is hard-coded to the main entry point of the (third-party) // react-shallow-renderer package. './node_modules/react-shallow-renderer/index.js': () => {