revenge-bundle-next/lib/react/src/jsx-runtime/patches.ts

35 lines
1 KiB
TypeScript
Raw Normal View History

2025-07-12 21:05:27 +07:00
import { instead } from '@revenge-mod/patcher'
import { ReactJSXRuntime } from '..'
import { jPatches } from './_internal'
import type { ReactElement } from 'react'
import type { AnyJSXFactoryFunction } from '.'
const jsx = ReactJSXRuntime.jsx
const patch = (
args: Parameters<AnyJSXFactoryFunction>,
orig: AnyJSXFactoryFunction,
) => {
const [type] = args
const patches = jPatches.get(type)
if (!patches) return Reflect.apply(orig, ReactJSXRuntime, args)
const { before, after, instead } = patches
2025-07-12 21:05:27 +07:00
if (before) for (const cb of before) args = cb(args)
2025-09-17 16:56:10 +07:00
let el: ReactElement | null | undefined
2025-07-12 21:05:27 +07:00
2025-09-17 16:56:10 +07:00
// If there are instead patches, el will always be set by the instead patches
if (instead) for (const cb of instead) el = cb(args, jsx)
// If there aren't any instead patches, we compute el normally, and it will be set
else el = Reflect.apply(orig, ReactJSXRuntime, args)
2025-07-12 21:05:27 +07:00
2025-09-17 16:56:10 +07:00
if (after) for (const cb of after) el = cb(el!)
2025-07-12 21:05:27 +07:00
2025-09-17 16:56:10 +07:00
return el!
2025-07-12 21:05:27 +07:00
}
instead(ReactJSXRuntime, 'jsx', patch)
instead(ReactJSXRuntime, 'jsxs', patch)