Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/shared/forks/object-assign.inline-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*/

const hasOwnProperty = Object.prototype.hasOwnProperty;

const _assign = function(to, from) {
for (let 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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually care about this? Would we expect to ever happen with our usage?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if it's a bug. Meh. I mostly just took it as-is from FB.

}

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;
};
8 changes: 8 additions & 0 deletions scripts/babel/transform-object-assign-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ 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
return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're now transforming the file itself, I needed to ensure this bails out.

}
if (path.get('callee').matchesPattern('Object.assign')) {
// generate identifier and require if it hasn't been already
const id = getAssignIdent(path, file, this);
Expand All @@ -36,6 +40,10 @@ 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
return;
}
if (path.matchesPattern('Object.assign')) {
const id = getAssignIdent(path, file, this);
path.replaceWith(id);
Expand Down
10 changes: 7 additions & 3 deletions scripts/rollup/forks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const {RENDERER, RECONCILER} = moduleTypes;
// If you need to replace a file with another file for a specific environment,
// add it to this list with the logic for choosing the right replacement.
const forks = Object.freeze({
// Optimization: for UMDs, use object-assign polyfill that is already a part
// of the React package instead of bundling it again.
// Optimization: for UMDs, use a version that we can inline into the React bundle.
// Use that from all other bundles.
'object-assign': (bundleType, entry, dependencies) => {
if (
bundleType !== UMD_DEV &&
Expand All @@ -34,12 +34,16 @@ const forks = Object.freeze({
// happens. Other bundles just require('object-assign') anyway.
return null;
}
if (entry === 'react') {
// Use the forked version that uses ES modules instead of CommonJS.
return '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!
// We can use the fork that reads the secret export!
return 'shared/forks/object-assign.umd.js';
},

Expand Down