Unverified Commit 415d7fd7 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Respect next/babel user provided runtime option (#19136)

parent 8ff52136
......@@ -67,9 +67,10 @@ module.exports = (
const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer)
const isModern = api.caller((caller: any) => !!caller && caller.isModern)
const hasJsxRuntime = Boolean(
api.caller((caller: any) => !!caller && caller.hasJsxRuntime)
)
const useJsxRuntime =
options['preset-react']?.runtime === 'automatic' ||
(Boolean(api.caller((caller: any) => !!caller && caller.hasJsxRuntime)) &&
options['preset-react']?.runtime !== 'classic')
const isLaxModern =
isModern ||
......@@ -122,7 +123,7 @@ module.exports = (
// This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development
development: isDevelopment || isTest,
...(hasJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }),
...(useJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }),
...options['preset-react'],
},
],
......@@ -132,7 +133,7 @@ module.exports = (
],
],
plugins: [
!hasJsxRuntime && [
!useJsxRuntime && [
require('./plugins/jsx-pragma'),
{
// This produces the following injected import for modules containing JSX:
......
......@@ -191,4 +191,32 @@ describe('next/babel', () => {
`)
})
})
describe('respect preset-react runtime', () => {
it('should allow forcing on automatic mode', () => {
const code = trim`const a = ()=><a href="/">home</a>`
const output = babel(code, true, {
'preset-react': {
runtime: 'automatic',
},
})
expect(output).toMatchInlineSnapshot(
`"import{jsx as _jsx}from\\"react/jsx-runtime\\";var a=function a(){return/*#__PURE__*/_jsx(\\"a\\",{href:\\"/\\",children:\\"home\\"});};"`
)
})
it('should allow forcing on classic mode', () => {
const code = trim`const a = ()=><a href="/">home</a>`
const output = babel(code, true, {
'preset-react': {
runtime: 'classic',
},
})
expect(output).toMatchInlineSnapshot(
`"import React from\\"react\\";var __jsx=React.createElement;var a=function a(){return __jsx(\\"a\\",{href:\\"/\\"},\\"home\\");};"`
)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment