Skip to content

Commit 7c88193

Browse files
committed
feat: add option for using new jsx runtime for react 17
1 parent 0249b7f commit 7c88193

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

packages/babel-preset-base/src/preset.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface PresetOptions {
77
noJsonStrings?: boolean;
88
noRuntime?: boolean;
99
hasReact?: boolean;
10+
useReactJsxRuntime?: boolean;
1011
presetEnv?: {};
1112
presetReact?: {};
1213
[x: string]: any;
@@ -41,6 +42,7 @@ export const preset = (opts: PresetOptions | null = {}) => {
4142
presetEnv = {},
4243
presetReact = {},
4344
hasReact = true,
45+
useReactJsxRuntime = false,
4446
// Put everything else inside noPlugins, which we will use later
4547
// to put or cancel out stage-3 plugins.
4648
// noDynamicImport = false,
@@ -69,6 +71,7 @@ export const preset = (opts: PresetOptions | null = {}) => {
6971
// Will use the native built-in instead of trying to polyfill
7072
// behavior for any plugins that require one.
7173
useBuiltIns: true,
74+
runtime: useReactJsxRuntime ? 'automatic' : 'classic',
7275
// But spread later, so that user can override it
7376
...presetReact,
7477
},

packages/scripts/src/config/CreateWebpackConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export class CreateWebpackConfig {
171171
jsBabelPresetOptions,
172172
tsBabelOverride,
173173
tsBabelPresetOptions,
174+
useReactJsxRuntime,
174175
} = this.projectConfig;
175176
const { host, port } = this.serverConfig;
176177
const helper: WebpackConfigHelper = new WebpackConfigHelper(
@@ -199,6 +200,7 @@ export class CreateWebpackConfig {
199200
jsBabelPresetOptions,
200201
tsBabelOverride,
201202
tsBabelPresetOptions,
203+
useReactJsxRuntime,
202204
},
203205
this.cwd,
204206
this.isDev

packages/scripts/src/config/WebpackConfigHelper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface WebpackConfigHelperConfig {
4343
outputPath: ProjectConfig['outputPath'];
4444
useBabelConfig: ProjectConfig['useBabelConfig'];
4545
hasReact: ProjectConfig['hasReact'];
46+
useReactJsxRuntime: ProjectConfig['useReactJsxRuntime'];
4647
disableReactRefresh: ProjectConfig['disableReactRefresh'];
4748
hasSass: ProjectConfig['hasSass'];
4849
hasFlow: ProjectConfig['hasFlow'];
@@ -395,6 +396,10 @@ ${bannerConfig.copyrightText}${bannerConfig.credit ? creditNote : ''}`,
395396
const { hasReact, hasSass, hasFlow, hasLess } = this.config;
396397
const wpackioBabelOptions: PresetOptions = {
397398
hasReact,
399+
// use react jsx runtime only if enabled explicitly
400+
// and not optimizing for gutenberg
401+
useReactJsxRuntime:
402+
this.config.useReactJsxRuntime && !this.file.optimizeForGutenberg,
398403
};
399404

400405
// check if babel.config.js is present

packages/scripts/src/config/project.config.default.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface ProjectConfig {
7979
hasReact: boolean;
8080
disableReactRefresh?: boolean;
8181
hasSass: boolean;
82+
useReactJsxRuntime?: boolean;
8283
hasLess?: boolean;
8384
hasFlow: boolean;
8485
// Use babel.config.js instead of built-in options
@@ -142,6 +143,10 @@ export const projectConfigDefault: ProjectConfig = {
142143
useBabelConfig: false,
143144
// Needs react?
144145
hasReact: true,
146+
// Whether or not to use the new jsx runtime introduced in React 17
147+
// this is opt-in
148+
// @see {https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html}
149+
useReactJsxRuntime: false,
145150
// Disable react refresh
146151
disableReactRefresh: false,
147152
// Needs sass?

packages/scripts/templates/wpackio.project.js.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ module.exports = {
5050
// Project specific config
5151
// Needs react(jsx)?
5252
hasReact: {{hasReact}},
53+
// Whether or not to use the new jsx runtime introduced in React 17
54+
// this is opt-in
55+
// @see {https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html}
56+
useReactJsxRuntime: false,
5357
// Disable react refresh
5458
disableReactRefresh: false,
5559
// Needs sass?

0 commit comments

Comments
 (0)