From 1af9e97421ed8f4b07cfdd5a0f1f92230945dd3c Mon Sep 17 00:00:00 2001 From: Ian Sutherland Date: Thu, 22 Oct 2020 17:42:14 -0600 Subject: [PATCH] Attempt to add back jsx transform detection --- packages/babel-preset-react-app/create.js | 10 ++++--- packages/eslint-config-react-app/base.js | 5 ---- .../react-scripts/config/webpack.config.js | 27 ++++++++++++++++++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/babel-preset-react-app/create.js b/packages/babel-preset-react-app/create.js index 699d5229161..df2ec31338f 100644 --- a/packages/babel-preset-react-app/create.js +++ b/packages/babel-preset-react-app/create.js @@ -8,8 +8,6 @@ const path = require('path'); -const useJsxTransform = process.env.DISABLE_NEW_JSX_TRANSFORM !== 'true'; - const validateBoolOption = (name, value, defaultValue) => { if (typeof value === 'undefined') { value = defaultValue; @@ -56,6 +54,10 @@ module.exports = function (api, opts, env) { ); } + var hasJsxRuntime = Boolean( + api.caller(caller => !!caller && caller.hasJsxRuntime) + ); + if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) { throw new Error( 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' + @@ -97,8 +99,8 @@ module.exports = function (api, opts, env) { development: isEnvDevelopment || isEnvTest, // Will use the native built-in instead of trying to polyfill // behavior for any plugins that require one. - ...(!useJsxTransform ? { useBuiltIns: true } : {}), - runtime: useJsxTransform ? 'automatic' : 'classic', + ...(!hasJsxRuntime ? { useBuiltIns: true } : {}), + runtime: hasJsxRuntime ? 'automatic' : 'classic', }, ], isTypeScriptEnabled && [require('@babel/preset-typescript').default], diff --git a/packages/eslint-config-react-app/base.js b/packages/eslint-config-react-app/base.js index 65229b150dd..4b6bcd6f8ef 100644 --- a/packages/eslint-config-react-app/base.js +++ b/packages/eslint-config-react-app/base.js @@ -11,8 +11,6 @@ // React App support, and is used as the `baseConfig` for `eslint-loader` // to ensure that user-provided configs don't need this boilerplate. -const useJsxTransform = process.env.DISABLE_NEW_JSX_TRANSFORM !== 'true'; - module.exports = { root: true, @@ -45,8 +43,5 @@ module.exports = { rules: { 'react/jsx-uses-vars': 'warn', 'react/jsx-uses-react': 'warn', - ...(!useJsxTransform && { - 'react/react-in-jsx-scope': 'error', - }), }, }; diff --git a/packages/react-scripts/config/webpack.config.js b/packages/react-scripts/config/webpack.config.js index a33a814b472..80c6ac27aca 100644 --- a/packages/react-scripts/config/webpack.config.js +++ b/packages/react-scripts/config/webpack.config.js @@ -71,6 +71,19 @@ const cssModuleRegex = /\.module\.css$/; const sassRegex = /\.(scss|sass)$/; const sassModuleRegex = /\.module\.(scss|sass)$/; +const hasJsxRuntime = (() => { + if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') { + return false; + } + + try { + require.resolve('react/jsx-runtime'); + return true; + } catch (e) { + return false; + } +})(); + // This is the production and development configuration. // It is focused on developer experience, fast rebuilds, and a minimal bundle. module.exports = function (webpackEnv) { @@ -396,7 +409,14 @@ module.exports = function (webpackEnv) { // @remove-on-eject-begin babelrc: false, configFile: false, - presets: [require.resolve('babel-preset-react-app')], + presets: [ + [ + require.resolve('babel-preset-react-app'), + { + runtime: hasJsxRuntime ? 'automatic' : 'classic', + }, + ], + ], // Make sure we have a unique cache identifier, erring on the // side of caution. // We remove this when the user ejects because the default @@ -737,6 +757,11 @@ module.exports = function (webpackEnv) { resolvePluginsRelativeTo: __dirname, baseConfig: { extends: [require.resolve('eslint-config-react-app/base')], + rules: { + ...(!hasJsxRuntime && { + 'react/react-in-jsx-scope': 'error', + }), + }, }, }), ].filter(Boolean),