Created
July 7, 2021 06:12
-
-
Save souporserious/c71ef3aa2c4dbd7002e2f4deab754f6a to your computer and use it in GitHub Desktop.
Example of web and native visitors in JSXUI.
This file contains hidden or 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
| module.exports = { | |
| plugins: [ | |
| ['@jsxui/babel-plugin', { | |
| visitor: process.env.PLATFORM === 'native' ? require('./native') : require('./web') | |
| }] | |
| ] | |
| } |
This file contains hidden or 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
| const buildStylesheet = template(` | |
| const styles = StyleSheet.create(STYLES) | |
| `) | |
| export const nativeVistitor = { | |
| Program(path) { | |
| path.pushContainer( | |
| 'body', | |
| buildStylesheet({ | |
| STYLES: t.objectExpression( | |
| Object.entries(this.styleAttributes).map(([id, attributes]) => | |
| t.objectProperty(t.identifier(id), t.objectExpression(attributes)) | |
| ) | |
| ), | |
| }) | |
| ) | |
| }, | |
| JSXOpeningElement(path) { | |
| const uid = path.node.attributes.find( | |
| (attribute) => attribute.name.name === 'uid' | |
| ) | |
| const attributes = path.node.attributes.filter( | |
| (attribute) => attribute.name.name !== 'uid' | |
| ) | |
| path.node.attributes = [ | |
| ...attributes, | |
| t.jsxAttribute( | |
| t.jsxIdentifier('style'), | |
| t.jSXExpressionContainer( | |
| t.memberExpression( | |
| t.identifier('styles'), | |
| t.identifier(uid.value.value) | |
| ) | |
| ) | |
| ), | |
| ] | |
| }, | |
| } |
This file contains hidden or 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
| export const webVisitor = { | |
| JSXOpeningElement(path) { | |
| const id = path.node.attributes.find( | |
| (attribute) => attribute.name.name === 'uid' | |
| ) | |
| const styleAttributes = id ? this.styleAttributes[id.value.value] : null | |
| if (styleAttributes) { | |
| path.node.attributes.push( | |
| t.jsxAttribute( | |
| t.jsxIdentifier('css'), | |
| t.jsxExpressionContainer(t.objectExpression(styleAttributes)) | |
| ) | |
| ) | |
| } | |
| }, | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment