Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: use jsx-dev-runtime when development #6883

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-meals-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/jsx-runtime': minor
---

fix: use jsx-dev-runtime when development
19 changes: 15 additions & 4 deletions packages/jsx-runtime/src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
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,
isStaticChildren: boolean,
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 };
51 changes: 1 addition & 50 deletions packages/jsx-runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
28 changes: 27 additions & 1 deletion packages/jsx-runtime/src/prod.ts
Original file line number Diff line number Diff line change
@@ -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,
};
26 changes: 26 additions & 0 deletions packages/jsx-runtime/src/style.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions packages/jsx-runtime/tests/hijackElememt.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -19,4 +19,4 @@ describe('hijack element', () => {
},
});
});
});
});
Loading