@samitouri / QOS-React-2 / commits / 58eedbb024

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.

Sebastian Markbåge committed Feb 28, 2020 at 11:14 UTC 58eedbb024d188081e20bee1514d794d731873ad
3 files changed +46 -3
packages/shared/forks/object-assign.inline-umd.js new
+31
@@ -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
+8
@@ -28,6 +28,10 @@ module.exports = function autoImporter(babel) {
28
29 visitor: {
30 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 + }
35 if (path.get('callee').matchesPattern('Object.assign')) {
36 // generate identifier and require if it hasn't been already
37 const id = getAssignIdent(path, file, this);
@@ -36,6 +40,10 @@ module.exports = function autoImporter(babel) {
40 },
41
42 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 + }
47 if (path.matchesPattern('Object.assign')) {
48 const id = getAssignIdent(path, file, this);
49 path.replaceWith(id);
scripts/rollup/forks.js
+7 -3
@@ -22,8 +22,8 @@ const {RENDERER, RECONCILER} = moduleTypes;
22 // If you need to replace a file with another file for a specific environment,
23 // add it to this list with the logic for choosing the right replacement.
24 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.
27 'object-assign': (bundleType, entry, dependencies) => {
28 if (
29 bundleType !== UMD_DEV &&
@@ -34,12 +34,16 @@ const forks = Object.freeze({
34 // happens. Other bundles just require('object-assign') anyway.
35 return null;
36 }
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 + }
41 if (dependencies.indexOf('react') === -1) {
42 // We can only apply the optimizations to bundle that depend on React
43 // because we read assign() from an object exposed on React internals.
44 return null;
45 }
42 - // We can use the fork!
46 + // We can use the fork that reads the secret export!
47 return 'shared/forks/object-assign.umd.js';
48 },
49