Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Object.assign() for JSX spread #4

Merged
merged 4 commits into from
Nov 19, 2019
Merged

Use Object.assign() for JSX spread #4

merged 4 commits into from
Nov 19, 2019

Conversation

developit
Copy link
Member

@developit developit commented Nov 18, 2019

Edge doesn't support Object spread properties. The JSX plugin here generates them, which means we're producing code that breaks in Edge.

Most users have @babel/proposal-object-rest-spread configured, and would see JSX spread transpiled to Babel's full object spread output with helpers. If they've configured that plugin with useBuiltIns:true, the result is clean Object.assign calls, though this is inconsistent with the spec behavior of spread properties (but not JSX Spread).

Instead, this PR changes preset-modules to transpile JSX Spread Attributes into Object.assign() calls, which are natively supported in all ES Modules-supporting browsers. The old behavior is still available, but is now gated behind a new useSpread option in the plugin (not the preset).

before:

// input:
const X = <div {...a} b />

// preset-modules output:
const X = <div {...{ ...a, b: true }} />;

// final output (after JSX transform):
function ownKeys(object, enumerableOnly) { /* snip */ }
function _objectSpread(target) { /* snip */ }
function _defineProperty(obj, key, value) { /* snip */ }
const X = h("div", _objectSpread({}, a, { b: true }));

// final output (after *loose* JSX transform):
var _assign = Object.assign || function(){ /* snip */ }
const X = h("div", _assign({}, a, { b: true }));

Size (uncompressed): 1.1kB

after:

// input:
const X = <div {...a} b />

// preset-modules output:
const X = <div {...Object.assign({}, a, { b: true })} />;

// final output (after JSX transform):
const X = h("div", Object.assign({}, a, { b: true }));

Size (uncompressed): 80 bytes


h/t @devongovett who noticed this!

Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
@developit developit merged commit d325cac into master Nov 19, 2019
@developit developit deleted the jsx-object-assign branch November 19, 2019 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants