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.

How to use the Babel plugin?

See original GitHub issue

I couldn’t find any instructions on how to use the plugin in the documentation. I tried the following:

// .babelrc.json
{
  "plugins": [
    "babel-plugin-jsx-dom-expressions"
  ]
}

And I run it with @babel/cli:

npx babel index.js -o compiled.js

But I keep getting a syntax error because of the JSX:

TypeError: /home/me/library/index.js: Property left of AssignmentExpression expected node to be of a type [“LVal”] but instead got “ArrowFunctionExpression”

How do I use this with Babel?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ryansolidcommented, Apr 25, 2020

I actually can change this. I was worried about this case:

function MyComp(prop) {
  return <div ref={props.ref} />
}

<MyComp />

If props.ref isn’t passed in I can’t tell if the intention is to assign it or simply it’s missing. But I realized since props are readonly, even if it gets assigned who cares. It just doesn’t update. Right now that scenario is compiled to:

props.ref && props.ref(el)

I can change it to:

typeof props.ref === "function" ? props.ref($el) : props.ref = $el;

While it is meaningless in this case if isn’t passed in. This will also work for a simple ref:

typeof el1 === "function" ? el1($el) : el1 = $el;

Overall it’s more code but it only runs once on creation and I don’t think it makes that much of a difference on size.

0reactions
elclanrscommented, Apr 24, 2020

Thank you, I must’ve missed that part in the docs and just made the assumption. It’d be ideal if it takes both vars and callbacks, but I understand that may be a limitation currently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plugins - Babel.js
Using a Plugin. If the plugin is on npm, you can pass in the name of the plugin and Babel will check that...
Read more >
Step-by-step guide for writing a custom babel transformation
Babel is a JavaScript compiler that is mainly used to convert ECMAScript 2015+ code into backward compatible version of JavaScript in current ...
Read more >
Introduction to custom Babel Plugins | by Kashyap Mukkamala
Babel is a JavaScript compiler which allows us to use the latest and greatest version of JavaScript by converting it into code that...
Read more >
BabelJS - Babel Plugins - Tutorialspoint
BabelJS - Babel Plugins, BabelJS is a javascript compiler that changes the syntax of the code given based on presets and plugins available....
Read more >
What are Babel "plugins" and "presets"? (And how to use them)
In Babel, a preset is a set of plugins used to support particular language features. The two presets Babel uses by default: ......
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