Skip to content

Commit

Permalink
Fix using new jsx transform in tests (#501)
Browse files Browse the repository at this point in the history
The new jsx transform is not working in tests when run through react-app-rewired (the fail with "ReferenceError: React is not defined").  Ported over this change from create-react-app: facebook/create-react-app@2b1161b
  • Loading branch information
msbarry committed Dec 4, 2020
1 parent a4ee598 commit 2ca3055
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion scripts/utils/babelTransform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
const babelJest = require('babel-jest');

const hasJsxRuntime = (() => {
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
return false;
}

try {
require.resolve('react/jsx-runtime');
return true;
} catch (e) {
return false;
}
})();

module.exports = babelJest.createTransformer({
presets: [require.resolve('babel-preset-react-app')],
presets: [
[
require.resolve('babel-preset-react-app'),
{
runtime: hasJsxRuntime ? 'automatic' : 'classic',
},
]
],
plugins: [],
babelrc: true
});

4 comments on commit 2ca3055

@DallasCarraher
Copy link

Choose a reason for hiding this comment

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

I think this is a breaking change for folks stuck on babel 6. Can this be moved to a major version?
@timarney @msbarry

@msbarry
Copy link
Contributor Author

@msbarry msbarry commented on 2ca3055 Dec 11, 2020

Choose a reason for hiding this comment

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

@DallasCarraher what about this is breaking? @dawnmist tested the pull request against v3: #501 (comment)

@dawnmist
Copy link
Collaborator

Choose a reason for hiding this comment

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

react-scripts 3 uses babel 7...in fact, I think react-scripts 2 used babel 7 as well.

@DallasCarraher What version of react-scripts are you using?

@DallasCarraher
Copy link

Choose a reason for hiding this comment

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

Yeah, sorry I didn't quite do my homework here and didn't do a great job explaining our scenario.

We have a forked version of CRA from 2018 in many projects at my company. We did this I believe to override the webpack config and get .less files working with a loader.

We've been breaking away from this fork recently and upgrading projects to the latest CRA. Some projects are still lagging behind and are dependencies of projects that have successfully upgraded.

So in a nutshell:
• We have a project using CRA 3.4.4.
• A dependency package using forked CRA 2.x (with babel 6)
• This commit seems to break packages using babel 6.

Long story short, for the time being I just wanted to see if we can make it possible to still get some updates from rewire while we're getting rid of this fork in other projects and also replacing less with sass because eventually we'd even like to get off rewire unless necessary.

Please sign in to comment.