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.

Using jsx in .jsx files not achievable -- Babel presets aren't used

See original GitHub issue

Current Behavior

With my tsconfig.json’s compilerOptions.jsx set to "react", when using .jsx files that contain any jsx markup, on tsdx build I get

(babel plugin) SyntaxError: /path/src/File.jsx: Unexpected token (3:11)
  1 | import React from 'react';
  2 | export function SomeComponent() {
> 3 |     return <div>Foo</div>;
    |            ^
  4 | }

Now, I’d expect this to work out of the box with .jsx files but I also tried to instead set the tsconfig.json’s compilerOptions.jsx: "preserve" and use @babel/preset-react via my .babelrc

{
  "plugins": [
    ...
  ],
  "presets": ["@babel/preset-react"]
}

Still getting the same error. While the plugins I use seem to work, preset-react seems to not do the job for some reason.

Expected behavior

.jsx files’ jsx code should be transpiled according to the tsconfig’s jsx option or there should be another way to achieve this.

Additional context

Using the .tsx file extension instead of .jsx for the same file, everything works as expected while using jsx: "react" but still can’t get it to work with jsx: "preserve" plus Babel.

Your environment

Software Version(s)
TSDX 0.12.3
TypeScript 3.7.3
Node 0.12
Operating System Mac, Linux

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
agilgur5commented, Jan 30, 2020

@agilgur5 According to https://www.typescriptlang.org/docs/handbook/tsconfig-json.html with compilerOptions.allowJs: true it shouldn’t be necessary to explicitly add .jsx to includes.

Yes, I also wrote “(or equivalent on the include part)” in my comment above as there’s many ways of doing it.

But wow! It seems changing my .babelrc to use

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

really does the trick!

Ok, think that confirms that that line I was looking at & referenced above is a bug. Thanks for checking that! I’ll get a PR out soon to fix that so you don’t need to add @babel/preset-env to get your other presets to work, but that should be an easy workaround for the time-being

1reaction
DanweDEcommented, Jan 30, 2020

@agilgur5 According to https://www.typescriptlang.org/docs/handbook/tsconfig-json.html with compilerOptions.allowJs: true it shouldn’t be necessary to explicitly add .jsx to includes.

But wow! It seems changing my .babelrc to use

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

really does the trick!

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel/preset-react
Replace the component used when compiling JSX fragments. useBuiltIns. boolean , defaults to false . Will use the native built-in instead of ......
Read more >
Babel-loader not transpiling JSX component. ModuleParseError
I'm using webpack to transpile all the CSS and JS/JSX files into JS. I'm able to build the package and pack it into...
Read more >
Support for the Experimental Syntax 'Jsx' Isn't Currently Enabled
Support for the experimental syntax 'jsx' isn't currently enabled is a Babel configuration error. Read this article, and we'll teach you how to...
Read more >
The always-recent guide to creating a development ...
Step-by-step guide to create a standalone full-stack JavaScript development environment for Nodejs and server-side rendered Reactjs.
Read more >
JavaScript - Parcel
@babel/preset-env and @babel/plugin-transform-runtime are not necessary, since transpilation for your browser targets is handled automatically by Parcel.
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