Skip to content

Commit 3251212

Browse files
committed
feat: allow custom jsxImportSource
- configured through tsconfig - env var for js or override
1 parent 9722ef1 commit 3251212

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

packages/babel-preset-react-app/create.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ module.exports = function (api, opts, env) {
9393
// Adds component stack to warning messages
9494
// Adds __self attribute to JSX which React will use for some warnings
9595
development: isEnvDevelopment || isEnvTest,
96+
runtime: opts.runtime || 'classic',
9697
// Will use the native built-in instead of trying to polyfill
9798
// behavior for any plugins that require one.
9899
...(opts.runtime !== 'automatic' ? { useBuiltIns: true } : {}),
99-
runtime: opts.runtime || 'classic',
100+
...(opts.runtime === 'automatic' && opts.importSource != null
101+
? { importSource: opts.importSource }
102+
: {}),
100103
},
101104
],
102105
isTypeScriptEnabled && [require('@babel/preset-typescript').default],

packages/react-scripts/config/jest/babelTransform.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ const hasJsxRuntime = (() => {
2222
return false;
2323
}
2424
})();
25+
const jsxImportSource = process.env.JSX_IMPORT_SOURCE;
2526

2627
module.exports = babelJest.createTransformer({
2728
presets: [
2829
[
2930
require.resolve('babel-preset-react-app'),
3031
{
3132
runtime: hasJsxRuntime ? 'automatic' : 'classic',
33+
...(jsxImportSource ? { importSource: jsxImportSource } : {}),
3234
},
3335
],
3436
],

packages/react-scripts/config/webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const hasJsxRuntime = (() => {
8686
return false;
8787
}
8888
})();
89+
const jsxImportSource = process.env.JSX_IMPORT_SOURCE;
8990

9091
// This is the production and development configuration.
9192
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
@@ -414,6 +415,9 @@ module.exports = function (webpackEnv) {
414415
require.resolve('babel-preset-react-app'),
415416
{
416417
runtime: hasJsxRuntime ? 'automatic' : 'classic',
418+
...(jsxImportSource
419+
? { importSource: jsxImportSource }
420+
: {}),
417421
},
418422
],
419423
],

packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ function verifyTypeScriptSetup() {
250250
}
251251
}
252252

253+
if (parsedCompilerOptions.jsxImportSource != null) {
254+
if (hasJsxRuntime && semver.gte(ts.version, '4.1.0-beta')) {
255+
if (process.env.JSX_IMPORT_SOURCE == null) {
256+
process.env.JSX_IMPORT_SOURCE = parsedCompilerOptions.jsxImportSource;
257+
}
258+
}
259+
}
260+
253261
// tsconfig will have the merged "include" and "exclude" by this point
254262
if (parsedTsConfig.include == null) {
255263
appTsConfig = immer(appTsConfig, config => {

0 commit comments

Comments
 (0)