From ddb603211b5fbe3f720237cf8d03718a8d3b9f46 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Thu, 1 May 2025 12:11:33 +0700 Subject: [PATCH] chore: shim `react/jsx-runtime` to compile JSX modules --- lib/types/tsconfig.json | 1 + scripts/build.ts | 9 +++++++++ scripts/dev.ts | 2 +- shims/react~jsx-runtime.ts | 12 ++++++++++++ tsconfig.json | 2 +- 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 shims/react~jsx-runtime.ts diff --git a/lib/types/tsconfig.json b/lib/types/tsconfig.json index 3af944c..928d7f4 100644 --- a/lib/types/tsconfig.json +++ b/lib/types/tsconfig.json @@ -8,6 +8,7 @@ "moduleResolution": "bundler", "verbatimModuleSyntax": true, "noEmit": true, + "jsx": "preserve", // Type checking "strict": true, "skipLibCheck": true, diff --git a/scripts/build.ts b/scripts/build.ts index fe997ff..38d615c 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -2,6 +2,7 @@ import { transform } from '@swc/core' import { $, main } from 'bun' import chalk from 'chalk' import { type OutputChunk, type RolldownPlugin, rolldown } from 'rolldown' +import { aliasPlugin } from 'rolldown/experimental' import pkg from '../package.json' @@ -33,6 +34,14 @@ export default async function build(dev = false, log = true) { }, plugins: [ swcPlugin(), + aliasPlugin({ + entries: [ + { + find: 'react/jsx-runtime', + replacement: './shims/react~jsx-runtime.ts', + }, + ], + }), hermesCPlugin({ flags: [ dev ? '-Og' : '-O', diff --git a/scripts/dev.ts b/scripts/dev.ts index 961b77e..559c941 100644 --- a/scripts/dev.ts +++ b/scripts/dev.ts @@ -12,7 +12,7 @@ console.info(chalk.redBright(`\nRevenge ${chalk.white(`v${pkg.version}`)}\n`)) const cwdify = (path: string) => join(process.cwd(), path) const Sources = ['src', 'lib', 'plugins'].map(cwdify) -const ExitTriggers = ['scripts', 'package.json', 'bun.lock'].map(cwdify) +const ExitTriggers = ['scripts', 'shims', 'package.json', 'bun.lock'].map(cwdify) const debouncedBuildDev = debounce(() => ((needRebuild = false), build(true).catch(console.error)), 250) diff --git a/shims/react~jsx-runtime.ts b/shims/react~jsx-runtime.ts new file mode 100644 index 0000000..5e9a73e --- /dev/null +++ b/shims/react~jsx-runtime.ts @@ -0,0 +1,12 @@ +import { ReactJsxRuntime } from '@revenge-mod/react' +import { getProxyTarget, isProxy } from '@revenge-mod/utils/proxy' +import type { ElementType } from 'react' + +const { jsx: _jsx, jsxs: _jsxs, Fragment } = ReactJsxRuntime + +const jsx = ((type, ...args) => + _jsx(isProxy(type as object) ? (getProxyTarget(type as object) as ElementType) : type, ...args)) as typeof _jsx +const jsxs = ((type, ...args) => + _jsxs(isProxy(type as object) ? (getProxyTarget(type as object) as ElementType) : type, ...args)) as typeof _jsxs + +export { jsx, jsxs, Fragment } diff --git a/tsconfig.json b/tsconfig.json index 9292188..ce96146 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,5 +6,5 @@ "~plugins/*": ["./plugins/*"] } }, - "include": ["lib", "plugins", "src"] + "include": ["lib", "plugins", "src", "shims"] }