question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transform-react-jsx with `runtime: automatic` should always using `jsx-runtime`

See original GitHub issue

Bug Report

@babel/plugin-transform-react-jsx” ^7.10.4

[ "@babel/plugin-transform-react-jsx", { "runtime": "automatic"  }]

Current behavior fallbacked classic runtime when jsx use spread with key exists

_createElement("span", {...obj, key: "key"})

Input Code

<span {...obj} key={"key"} />

Expected behavior

_jsx("span", obj, "key")

Environment

  • Babel version(s): 7.12.0
  • Node/npm version: 14.12.0
  • OS: [e.g. OSX 10.13.4, Windows 10]
  • Monorepo: yes
  • How you are using Babel: cli, register, loader

Possible Solution

may transform codes like

<span key={key} {...obj} /> => jsx("span", obj, key) <span {...obj} key={key} /> => jsx("span", {...obj, key}, key)

to support https://github.com/babel/babel/blob/master/packages/babel-helper-builder-react-jsx-experimental/src/index.js#L270

it will be friendly for custom jsx-runtime and some react-like lib

Additional context

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
morlaycommented, Oct 15, 2020

I got it by the comments.

  // We want to use React.createElement, even in the case of
  // jsx, for <div {...props} key={key} /> to distinguish it
  // from <div key={key} {...props} />. This is an intermediary
  // step while we deprecate key spread from props. Afterwards,
  // we will stop using createElement in the transform.

https://github.com/babel/babel/blob/master/packages/babel-helper-builder-react-jsx-experimental/src/index.js#L270

But it will be confused for adding custom jsx-runtime like https://github.com/emotion-js/emotion/pull/1970

have to expose as

import { createElement } from "@emotion/core"
import { jsx, jsxs, Fragment } from "@emotion/core/jsx-runtime"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing the New JSX Transform – React Blog
With the new transform, you can use JSX without importing React. ... JSX transform will automatically import the necessary react/jsx-runtime ...
Read more >
babel/preset-react
Decides which runtime to use. automatic auto imports the functions that JSX transpiles to. classic does not automatic import anything. development.
Read more >
New jsx compiler? - help - Meteor forums
Does anyone know how to use the new JSX compiler in Meteor? I actually don't know where the transform is specified in my...
Read more >
a-jsx-runtime - npm Package Health Analysis - Snyk
[ "@babel/plugin-transform-react-jsx", { "runtime": "automatic", // add the `import` etc automatically. "importSource": "a-jsx-runtime", // use ...
Read more >
You don't need React to use JSX. In this ... - Arnaud Dostes
import {jsx as _jsx} from 'react/jsx-runtime'; ... From the blog post, we know we need to do two things: use the new automatic...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found