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 = ( ...@@ -67,9 +67,10 @@ module.exports = (
const supportsESM = api.caller(supportsStaticESM) const supportsESM = api.caller(supportsStaticESM)
const isServer = api.caller((caller: any) => !!caller && caller.isServer) const isServer = api.caller((caller: any) => !!caller && caller.isServer)
const isModern = api.caller((caller: any) => !!caller && caller.isModern) const isModern = api.caller((caller: any) => !!caller && caller.isModern)
const hasJsxRuntime = Boolean( const useJsxRuntime =
api.caller((caller: any) => !!caller && caller.hasJsxRuntime) options['preset-react']?.runtime === 'automatic' ||
) (Boolean(api.caller((caller: any) => !!caller && caller.hasJsxRuntime)) &&
options['preset-react']?.runtime !== 'classic')
const isLaxModern = const isLaxModern =
isModern || isModern ||
...@@ -122,7 +123,7 @@ module.exports = ( ...@@ -122,7 +123,7 @@ module.exports = (
// This adds @babel/plugin-transform-react-jsx-source and // This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development // @babel/plugin-transform-react-jsx-self automatically in development
development: isDevelopment || isTest, development: isDevelopment || isTest,
...(hasJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }), ...(useJsxRuntime ? { runtime: 'automatic' } : { pragma: '__jsx' }),
...options['preset-react'], ...options['preset-react'],
}, },
], ],
...@@ -132,7 +133,7 @@ module.exports = ( ...@@ -132,7 +133,7 @@ module.exports = (
], ],
], ],
plugins: [ plugins: [
!hasJsxRuntime && [ !useJsxRuntime && [
require('./plugins/jsx-pragma'), require('./plugins/jsx-pragma'),
{ {
// This produces the following injected import for modules containing JSX: // This produces the following injected import for modules containing JSX:
......
...@@ -191,4 +191,32 @@ describe('next/babel', () => { ...@@ -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