Skip to content

Commit 58eedbb

Browse files
authored
Check in a forked version of object-assign only for UMD builds (#18180)
* Check in a forked version of object-assign This one uses ES modules so that we can inline it into UMD builds. We could wait for object-assign to make an ESM export but we're going to remove this dependency and assume global polyfills in the next version anyway. However, we'd have to figure out how to keep the copyright header and it'll get counted in terms of byte size (even if other tooling removes it). A lot of headache when we have our own implementation anyway. So I'll just use that. Ours is not resilient to checking certain browser bugs but those browsers are mostly unused anyway. (Even FB breaks on them presumably.) We also don't need to be resilient to Symbols since the way React uses it we shouldn't need to copy symbols * Don't transpile Object.assign to object-assign in object-assign The polyfill needs to be able to feature detect Object.assign.
1 parent 053347e commit 58eedbb

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*/
4+
5+
const hasOwnProperty = Object.prototype.hasOwnProperty;
6+
7+
const _assign = function(to, from) {
8+
for (let key in from) {
9+
if (hasOwnProperty.call(from, key)) {
10+
to[key] = from[key];
11+
}
12+
}
13+
};
14+
15+
export default Object.assign ||
16+
function(target, sources) {
17+
if (target == null) {
18+
throw new TypeError('Object.assign target cannot be null or undefined');
19+
}
20+
21+
const to = Object(target);
22+
23+
for (let nextIndex = 1; nextIndex < arguments.length; nextIndex++) {
24+
const nextSource = arguments[nextIndex];
25+
if (nextSource != null) {
26+
_assign(to, Object(nextSource));
27+
}
28+
}
29+
30+
return to;
31+
};

scripts/babel/transform-object-assign-require.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports = function autoImporter(babel) {
2828

2929
visitor: {
3030
CallExpression: function(path, file) {
31+
if (file.filename.indexOf('object-assign') !== -1) {
32+
// Don't replace Object.assign if we're transforming object-assign
33+
return;
34+
}
3135
if (path.get('callee').matchesPattern('Object.assign')) {
3236
// generate identifier and require if it hasn't been already
3337
const id = getAssignIdent(path, file, this);
@@ -36,6 +40,10 @@ module.exports = function autoImporter(babel) {
3640
},
3741

3842
MemberExpression: function(path, file) {
43+
if (file.filename.indexOf('object-assign') !== -1) {
44+
// Don't replace Object.assign if we're transforming object-assign
45+
return;
46+
}
3947
if (path.matchesPattern('Object.assign')) {
4048
const id = getAssignIdent(path, file, this);
4149
path.replaceWith(id);

scripts/rollup/forks.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const {RENDERER, RECONCILER} = moduleTypes;
2222
// If you need to replace a file with another file for a specific environment,
2323
// add it to this list with the logic for choosing the right replacement.
2424
const forks = Object.freeze({
25-
// Optimization: for UMDs, use object-assign polyfill that is already a part
26-
// of the React package instead of bundling it again.
25+
// Optimization: for UMDs, use a version that we can inline into the React bundle.
26+
// Use that from all other bundles.
2727
'object-assign': (bundleType, entry, dependencies) => {
2828
if (
2929
bundleType !== UMD_DEV &&
@@ -34,12 +34,16 @@ const forks = Object.freeze({
3434
// happens. Other bundles just require('object-assign') anyway.
3535
return null;
3636
}
37+
if (entry === 'react') {
38+
// Use the forked version that uses ES modules instead of CommonJS.
39+
return 'shared/forks/object-assign.inline-umd.js';
40+
}
3741
if (dependencies.indexOf('react') === -1) {
3842
// We can only apply the optimizations to bundle that depend on React
3943
// because we read assign() from an object exposed on React internals.
4044
return null;
4145
}
42-
// We can use the fork!
46+
// We can use the fork that reads the secret export!
4347
return 'shared/forks/object-assign.umd.js';
4448
},
4549

0 commit comments

Comments
 (0)