-
Notifications
You must be signed in to change notification settings - Fork 624
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
feat: rax jsx runtime #2101
Open
7213
wants to merge
13
commits into
alibaba:master
Choose a base branch
from
7213:feat/rax-jsx-runtime
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: rax jsx runtime #2101
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b09bac3
feat: jsx-runtime
52c3268
feat: jsx-runtime
eaf2e27
feat: jsx-runtime
78cc660
feat: jsx-runtime
be9d1e4
feat: jsx-runtime
f895d08
feat: jsx-runtime
ef8bb58
feat: jsx-runtime
b85968f
feat: jsx-runtime
4979a61
feat: jsx-runtime
0c2c2b3
feat: jsx-runtime
2632b35
feat: jsx-runtime
b57b666
feat: jsx-runtime
22f1efa
feat: jsx-runtime
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Fragment, jsxDEV } from './index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Fragment, jsx, jsxs, jsxDEV } from './index'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Component from '../vdom/component'; | ||
import createElement from '../createElement'; | ||
import createRef from '../createRef'; | ||
import { jsx, jsxs, jsxDEV } from '../jsx-runtime'; | ||
import Fragment from '../fragment'; | ||
|
||
describe('Support JSX-Runtime', () => { | ||
it('should export all modules needed by importSource', () => { | ||
expect(typeof jsx).toBe('function'); | ||
expect(typeof jsxs).toBe('function'); | ||
expect(typeof jsxDEV).toBe('function'); | ||
expect(typeof Fragment).toBe('function'); | ||
}); | ||
|
||
it('should add key', () => { | ||
const vnode = jsx('div', null, 'foo'); | ||
expect(vnode.key).toBe('foo'); | ||
}); | ||
|
||
it('should keep ref in props', () => { | ||
let ref = () => null; | ||
let vnode = jsx('div', { ref }); | ||
expect(vnode.ref).toBe(ref); | ||
|
||
ref = 'fooRef'; | ||
vnode = jsx('div', { ref }); | ||
expect(vnode.ref).toBe('fooRef'); | ||
}); | ||
|
||
it('should apply defaultProps', () => { | ||
class Foo extends Component { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
Foo.defaultProps = { | ||
foo: 'bar', | ||
fake: 'bar1', | ||
}; | ||
|
||
let vnode = jsx(Foo, {}, null); | ||
expect(vnode.props.foo).toBe('bar'); | ||
|
||
vnode = jsx(Foo, {fake: 'bar2'}, null); | ||
expect(vnode.props.fake).toBe('bar2'); | ||
}); | ||
|
||
it('should keep props over defaultProps', () => { | ||
class Foo extends Component { | ||
render() { | ||
return <div />; | ||
} | ||
} | ||
|
||
Foo.defaultProps = { | ||
foo: 'bar' | ||
}; | ||
|
||
const vnode = jsx(Foo, { foo: 'baz' }, null); | ||
expect(vnode.props).toEqual({ | ||
foo: 'baz' | ||
}); | ||
}); | ||
|
||
it('should set __source and __self', () => { | ||
const vnode = jsxDEV('div', { class: 'foo' }, 'key', 'source', 'self'); | ||
expect(vnode.__source).toBe('source'); | ||
expect(vnode.__self).toBe('self'); | ||
}); | ||
|
||
it('should return a vnode like createElement', () => { | ||
const elementVNode = createElement('div', { | ||
class: 'foo', | ||
key: 'key' | ||
}); | ||
const jsxVNode = jsx('div', { class: 'foo' }, 'key'); | ||
expect(jsxVNode).toEqual(elementVNode); | ||
}); | ||
|
||
it('should remove ref from props', () => { | ||
const ref = createRef(); | ||
const vnode = jsx('div', { ref }, null); | ||
expect(vnode.props).toEqual({}); | ||
expect(vnode.ref).toBe(ref); | ||
}); | ||
|
||
it('should receive children props', () => { | ||
const fooProps = {children: []}; | ||
expect(Fragment(fooProps)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { jsxWithValidation, jsxWithValidationDynamic, jsxWithValidationStatic } from './vdom/jsxValidator'; | ||
import { jsxRuntime as jsxProd } from './vdom/jsx'; | ||
|
||
/** | ||
* This module is adapted to react's jsx-runtime module. | ||
* @see:[introducing-the-new-jsx-transform](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.htm) | ||
*/ | ||
const __DEV__ = process.env.NODE_ENV !== 'production'; | ||
|
||
const jsx = __DEV__ ? jsxWithValidationDynamic : jsxProd; | ||
const jsxs = __DEV__ ? jsxWithValidationStatic : jsxProd; | ||
const jsxDEV = __DEV__ ? jsxWithValidation : undefined; | ||
|
||
export { | ||
jsx, | ||
jsxs, | ||
jsxDEV | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Host from './host'; | ||
import Element from './element'; | ||
|
||
export function jsxRuntime(type, props, key, __source, __self) { | ||
let normalizedProps = {}; | ||
let propName; | ||
|
||
// The default value of key and ref of rax element is null | ||
let ref = props && props.ref || null; | ||
key = key || null; | ||
|
||
for (propName in props) { | ||
if (propName !== 'ref') { | ||
normalizedProps[propName] = props[propName]; | ||
} | ||
} | ||
|
||
let defaults; | ||
if (typeof type === 'function' && (defaults = type.defaultProps)) { | ||
for (propName in defaults) | ||
if (normalizedProps[propName] === undefined) { | ||
normalizedProps[propName] = defaults[propName]; | ||
} | ||
} | ||
|
||
return new Element( | ||
type, | ||
key, | ||
ref, | ||
normalizedProps, | ||
Host.owner, | ||
__source, | ||
__self | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { throwError, throwMinifiedWarn, warning } from '../error'; | ||
import { isArray, NOOP } from '../types'; | ||
import validateChildKeys from '../validateChildKeys'; | ||
import { jsxRuntime } from './jsx'; | ||
|
||
const __DEV__ = process.env.NODE_ENV !== 'production'; | ||
|
||
function _jsxWithValidation(type, props, key, isStaticChildren, __source, __self) { | ||
const elem = jsxRuntime(type, props, key, __source, __self); | ||
|
||
if (type == null) { | ||
if (__DEV__) { | ||
throwError(`Invalid element type, expected a string or a class/function component but got "${type}".`); | ||
} else { | ||
// A empty component replaced avoid break render in production | ||
type = NOOP; | ||
throwMinifiedWarn(0); | ||
} | ||
} | ||
|
||
const children = props && props.children; | ||
if (children !== undefined) { | ||
if (isStaticChildren) { | ||
if (isArray(children)) { | ||
for (let i = 0; i < children.length; i++) { | ||
validateChildKeys(children[i], type); | ||
} | ||
|
||
if (Object.freeze) { | ||
Object.freeze(children); | ||
} | ||
} else { | ||
warning( | ||
'Rax.jsx: Static children should always be an array. ' + | ||
'You are likely explicitly calling Rax.jsxs or Rax.jsxDEV. ' + | ||
'Use the Babel transform instead.', | ||
); | ||
} | ||
} else { | ||
validateChildKeys(children, type); | ||
} | ||
} | ||
|
||
return elem; | ||
} | ||
|
||
|
||
export function jsxWithValidationDynamic(type, props, key) { | ||
return _jsxWithValidation(type, props, key, false); | ||
} | ||
export function jsxWithValidationStatic(type, props, key) { | ||
return _jsxWithValidation(type, props, key, true); | ||
} | ||
export function jsxWithValidation(type, props, key, __source, __self) { | ||
return _jsxWithValidation(type, props, key, true, __source, __self); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里抽成一个 utils,避免浪费体积