From 3ce96e34833ca39ee7b4f238213de09a4d4eab2e Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Tue, 7 May 2024 17:34:18 +0800 Subject: [PATCH 1/2] fix: use jsx-dev-runtime when development --- .changeset/few-meals-listen.md | 5 +++ packages/jsx-runtime/src/dev.ts | 19 +++++++++--- packages/jsx-runtime/src/index.ts | 51 +------------------------------ packages/jsx-runtime/src/prod.ts | 28 ++++++++++++++++- packages/jsx-runtime/src/style.ts | 26 ++++++++++++++++ 5 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 .changeset/few-meals-listen.md create mode 100644 packages/jsx-runtime/src/style.ts diff --git a/.changeset/few-meals-listen.md b/.changeset/few-meals-listen.md new file mode 100644 index 0000000000..b36d11b155 --- /dev/null +++ b/.changeset/few-meals-listen.md @@ -0,0 +1,5 @@ +--- +'@ice/jsx-runtime': minor +--- + +fix: use jsx-dev-runtime when development diff --git a/packages/jsx-runtime/src/dev.ts b/packages/jsx-runtime/src/dev.ts index 833a599a54..890af0a9e6 100644 --- a/packages/jsx-runtime/src/dev.ts +++ b/packages/jsx-runtime/src/dev.ts @@ -1,7 +1,16 @@ -import { jsx, Fragment, jsxs } from './index.js'; +// @ts-ignore +import { jsxDEV as _jsxDEV, Fragment } from 'react/jsx-dev-runtime'; +import { hijackElementProps } from './style.js'; -export { Fragment }; -export function jsxDEV( +/** + * @param {*} type + * @param {object} props + * @param {string} key + * @param {boolean} isStaticChildren + * @param {object} source + * @param {any} self + */ +function jsxDEV( type: any, props: object, key: string, @@ -9,5 +18,7 @@ export function jsxDEV( source: object, self: any, ) { - return isStaticChildren ? jsxs(type, props, key, source, self) : jsx(type, props, key, source, self); + return _jsxDEV(type, hijackElementProps(props), key, isStaticChildren, source, self); } + +export { jsxDEV, Fragment }; diff --git a/packages/jsx-runtime/src/index.ts b/packages/jsx-runtime/src/index.ts index d75eac8ec8..ef3801e929 100644 --- a/packages/jsx-runtime/src/index.ts +++ b/packages/jsx-runtime/src/index.ts @@ -1,50 +1 @@ -// @ts-ignore -import { jsx as _jsx, jsxs as _jsxs, Fragment } from 'react/jsx-runtime'; -// @ts-ignore -import { convertUnit } from 'style-unit'; - -const STYLE = 'style'; - -/** - * https://github.com/reactjs/rfcs/pull/107 - * @param {*} type - * @param {object} props - * @param {string} maybeKey - * @param {object} source - * @param {any} self - */ -export function jsx(type: any, props: object, maybeKey: string, source: object, self: any) { - return _jsx(type, hijackElementProps(props), maybeKey, source, self); -} - -// Same as jsx method, special case jsxs internally to take advantage of static children. -// // for now we can ship identical prod functions. -export function jsxs(type: any, props: object, maybeKey: string, source: object, self: any) { - return _jsxs(type, hijackElementProps(props), maybeKey, source, self); -} - -function isObject(obj: any): obj is object { - return typeof obj === 'object'; -} - -// Support rpx unit. -export function hijackElementProps(props: { style?: object } | object): object { - if (props && STYLE in props) { - const { style } = props; - if (isObject(style)) { - const result = Object.assign({}, props); - const convertedStyle = {}; - for (const prop in style) { - // @ts-ignore - convertedStyle[prop] = typeof style[prop] === 'string' ? convertUnit(style[prop]) : style[prop]; - } - result['style'] = convertedStyle; - return result; - } - } - return props; -} - -export { - Fragment, -}; +export * from './prod.js'; diff --git a/packages/jsx-runtime/src/prod.ts b/packages/jsx-runtime/src/prod.ts index 3ed9a9ecc5..fd420ef6ab 100644 --- a/packages/jsx-runtime/src/prod.ts +++ b/packages/jsx-runtime/src/prod.ts @@ -1 +1,27 @@ -export { jsx, jsxs, Fragment } from './index.js'; +// @ts-ignore +import { jsx as _jsx, jsxs as _jsxs, Fragment } from 'react/jsx-runtime'; +import { hijackElementProps } from './style.js'; + +/** + * https://github.com/reactjs/rfcs/pull/107 + * @param {*} type + * @param {object} props + * @param {string} maybeKey + * @param {object} source + * @param {any} self + */ +function jsx(type: any, props: object, maybeKey: string, source: object, self: any) { + return _jsx(type, hijackElementProps(props), maybeKey, source, self); +} + +// Same as jsx method, special case jsxs internally to take advantage of static children. +// // for now we can ship identical prod functions. +function jsxs(type: any, props: object, maybeKey: string, source: object, self: any) { + return _jsxs(type, hijackElementProps(props), maybeKey, source, self); +} + +export { + Fragment, + jsx, + jsxs, +}; diff --git a/packages/jsx-runtime/src/style.ts b/packages/jsx-runtime/src/style.ts new file mode 100644 index 0000000000..eac2603fab --- /dev/null +++ b/packages/jsx-runtime/src/style.ts @@ -0,0 +1,26 @@ +// @ts-ignore +import { convertUnit } from 'style-unit'; + +const STYLE = 'style'; + +function isObject(obj: any): obj is object { + return typeof obj === 'object'; +} + +// Support rpx unit. +export function hijackElementProps(props: { style?: object } | object): object { + if (props && STYLE in props) { + const { style } = props; + if (isObject(style)) { + const result = Object.assign({}, props); + const convertedStyle = {}; + for (const prop in style) { + // @ts-ignore + convertedStyle[prop] = typeof style[prop] === 'string' ? convertUnit(style[prop]) : style[prop]; + } + result['style'] = convertedStyle; + return result; + } + } + return props; +} From da0b445a9b88a96089507f06f9db4af516164370 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Tue, 7 May 2024 17:49:29 +0800 Subject: [PATCH 2/2] chore: import path --- packages/jsx-runtime/tests/hijackElememt.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jsx-runtime/tests/hijackElememt.test.ts b/packages/jsx-runtime/tests/hijackElememt.test.ts index 7fdeff8e43..c675f43865 100644 --- a/packages/jsx-runtime/tests/hijackElememt.test.ts +++ b/packages/jsx-runtime/tests/hijackElememt.test.ts @@ -1,5 +1,5 @@ import { expect, it, describe } from 'vitest'; -import { hijackElementProps } from '../src/'; +import { hijackElementProps } from '../src/style'; describe('hijack element', () => { it('hijackElementProps basic', () => { @@ -19,4 +19,4 @@ describe('hijack element', () => { }, }); }); -}); \ No newline at end of file +});