@vitejs/plugin-react 1.3.2 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
5
+ * @license GNU General Public License v3.0
6
+ */
7
+ function babelRestoreJsx({ types: t }) {
8
+ function getJSXNode(node) {
9
+ if (!isReactCreateElement(node)) {
10
+ return null;
11
+ }
12
+ const [nameNode, propsNode, ...childNodes] = node.arguments;
13
+ const name = getJSXName(nameNode);
14
+ if (name == null) {
15
+ return null;
16
+ }
17
+ const props = getJSXProps(propsNode);
18
+ if (props == null) {
19
+ return null;
20
+ }
21
+ const children = getJSXChildren(childNodes);
22
+ if (children == null) {
23
+ return null;
24
+ }
25
+ if (t.isJSXMemberExpression(name) && t.isJSXIdentifier(name.object) && name.object.name === "React" && name.property.name === "Fragment") {
26
+ return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), children);
27
+ }
28
+ const selfClosing = children.length === 0;
29
+ const startTag = t.jsxOpeningElement(name, props, selfClosing);
30
+ startTag.loc = node.loc;
31
+ const endTag = selfClosing ? null : t.jsxClosingElement(name);
32
+ return t.jsxElement(startTag, endTag, children, selfClosing);
33
+ }
34
+ function getJSXName(node) {
35
+ if (node == null) {
36
+ return null;
37
+ }
38
+ const name = getJSXIdentifier(node, true);
39
+ if (name != null) {
40
+ return name;
41
+ }
42
+ if (!t.isMemberExpression(node)) {
43
+ return null;
44
+ }
45
+ const object = getJSXName(node.object);
46
+ const property = getJSXName(node.property);
47
+ if (object == null || property == null) {
48
+ return null;
49
+ }
50
+ return t.jsxMemberExpression(object, property);
51
+ }
52
+ function getJSXProps(node) {
53
+ if (node == null || isNullLikeNode(node)) {
54
+ return [];
55
+ }
56
+ if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "_extends" })) {
57
+ const props = node.arguments.map(getJSXProps);
58
+ if (props.every((prop) => prop != null)) {
59
+ return [].concat(...props);
60
+ }
61
+ }
62
+ if (!t.isObjectExpression(node) && t.isExpression(node))
63
+ return [t.jsxSpreadAttribute(node)];
64
+ if (!isPlainObjectExpression(node)) {
65
+ return null;
66
+ }
67
+ return node.properties.map((prop) => t.isObjectProperty(prop) ? t.jsxAttribute(getJSXIdentifier(prop.key), getJSXAttributeValue(prop.value)) : t.jsxSpreadAttribute(prop.argument));
68
+ }
69
+ function getJSXChild(node) {
70
+ if (t.isStringLiteral(node)) {
71
+ return t.jsxText(node.value);
72
+ }
73
+ if (isReactCreateElement(node)) {
74
+ return getJSXNode(node);
75
+ }
76
+ if (t.isExpression(node)) {
77
+ return t.jsxExpressionContainer(node);
78
+ }
79
+ return null;
80
+ }
81
+ function getJSXChildren(nodes) {
82
+ const children = nodes.filter((node) => !isNullLikeNode(node)).map(getJSXChild);
83
+ if (children.some((child) => child == null)) {
84
+ return null;
85
+ }
86
+ return children;
87
+ }
88
+ function getJSXIdentifier(node, tag = false) {
89
+ if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
90
+ return t.jsxIdentifier(node.name);
91
+ }
92
+ if (t.isStringLiteral(node)) {
93
+ return t.jsxIdentifier(node.value);
94
+ }
95
+ return null;
96
+ }
97
+ function getJSXAttributeValue(node) {
98
+ if (t.isStringLiteral(node)) {
99
+ return node;
100
+ }
101
+ if (t.isJSXElement(node)) {
102
+ return node;
103
+ }
104
+ if (t.isExpression(node)) {
105
+ return t.jsxExpressionContainer(node);
106
+ }
107
+ return null;
108
+ }
109
+ const isReactCreateElement = (node) => t.isCallExpression(node) && t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, { name: "React" }) && t.isIdentifier(node.callee.property, { name: "createElement" }) && !node.callee.computed;
110
+ const isNullLikeNode = (node) => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
111
+ const isPlainObjectExpression = (node) => t.isObjectExpression(node) && node.properties.every((property) => t.isSpreadElement(property) || t.isObjectProperty(property, { computed: false }) && getJSXIdentifier(property.key) != null && getJSXAttributeValue(property.value) != null);
112
+ return {
113
+ visitor: {
114
+ CallExpression(path) {
115
+ const node = getJSXNode(path.node);
116
+ if (node == null) {
117
+ return null;
118
+ }
119
+ path.replaceWith(node);
120
+ }
121
+ }
122
+ };
123
+ }
124
+
125
+ exports["default"] = babelRestoreJsx;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
3
+ * @license GNU General Public License v3.0
4
+ */
5
+ function babelRestoreJsx({ types: t }) {
6
+ function getJSXNode(node) {
7
+ if (!isReactCreateElement(node)) {
8
+ return null;
9
+ }
10
+ const [nameNode, propsNode, ...childNodes] = node.arguments;
11
+ const name = getJSXName(nameNode);
12
+ if (name == null) {
13
+ return null;
14
+ }
15
+ const props = getJSXProps(propsNode);
16
+ if (props == null) {
17
+ return null;
18
+ }
19
+ const children = getJSXChildren(childNodes);
20
+ if (children == null) {
21
+ return null;
22
+ }
23
+ if (t.isJSXMemberExpression(name) && t.isJSXIdentifier(name.object) && name.object.name === "React" && name.property.name === "Fragment") {
24
+ return t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), children);
25
+ }
26
+ const selfClosing = children.length === 0;
27
+ const startTag = t.jsxOpeningElement(name, props, selfClosing);
28
+ startTag.loc = node.loc;
29
+ const endTag = selfClosing ? null : t.jsxClosingElement(name);
30
+ return t.jsxElement(startTag, endTag, children, selfClosing);
31
+ }
32
+ function getJSXName(node) {
33
+ if (node == null) {
34
+ return null;
35
+ }
36
+ const name = getJSXIdentifier(node, true);
37
+ if (name != null) {
38
+ return name;
39
+ }
40
+ if (!t.isMemberExpression(node)) {
41
+ return null;
42
+ }
43
+ const object = getJSXName(node.object);
44
+ const property = getJSXName(node.property);
45
+ if (object == null || property == null) {
46
+ return null;
47
+ }
48
+ return t.jsxMemberExpression(object, property);
49
+ }
50
+ function getJSXProps(node) {
51
+ if (node == null || isNullLikeNode(node)) {
52
+ return [];
53
+ }
54
+ if (t.isCallExpression(node) && t.isIdentifier(node.callee, { name: "_extends" })) {
55
+ const props = node.arguments.map(getJSXProps);
56
+ if (props.every((prop) => prop != null)) {
57
+ return [].concat(...props);
58
+ }
59
+ }
60
+ if (!t.isObjectExpression(node) && t.isExpression(node))
61
+ return [t.jsxSpreadAttribute(node)];
62
+ if (!isPlainObjectExpression(node)) {
63
+ return null;
64
+ }
65
+ return node.properties.map((prop) => t.isObjectProperty(prop) ? t.jsxAttribute(getJSXIdentifier(prop.key), getJSXAttributeValue(prop.value)) : t.jsxSpreadAttribute(prop.argument));
66
+ }
67
+ function getJSXChild(node) {
68
+ if (t.isStringLiteral(node)) {
69
+ return t.jsxText(node.value);
70
+ }
71
+ if (isReactCreateElement(node)) {
72
+ return getJSXNode(node);
73
+ }
74
+ if (t.isExpression(node)) {
75
+ return t.jsxExpressionContainer(node);
76
+ }
77
+ return null;
78
+ }
79
+ function getJSXChildren(nodes) {
80
+ const children = nodes.filter((node) => !isNullLikeNode(node)).map(getJSXChild);
81
+ if (children.some((child) => child == null)) {
82
+ return null;
83
+ }
84
+ return children;
85
+ }
86
+ function getJSXIdentifier(node, tag = false) {
87
+ if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
88
+ return t.jsxIdentifier(node.name);
89
+ }
90
+ if (t.isStringLiteral(node)) {
91
+ return t.jsxIdentifier(node.value);
92
+ }
93
+ return null;
94
+ }
95
+ function getJSXAttributeValue(node) {
96
+ if (t.isStringLiteral(node)) {
97
+ return node;
98
+ }
99
+ if (t.isJSXElement(node)) {
100
+ return node;
101
+ }
102
+ if (t.isExpression(node)) {
103
+ return t.jsxExpressionContainer(node);
104
+ }
105
+ return null;
106
+ }
107
+ const isReactCreateElement = (node) => t.isCallExpression(node) && t.isMemberExpression(node.callee) && t.isIdentifier(node.callee.object, { name: "React" }) && t.isIdentifier(node.callee.property, { name: "createElement" }) && !node.callee.computed;
108
+ const isNullLikeNode = (node) => t.isNullLiteral(node) || t.isIdentifier(node, { name: "undefined" });
109
+ const isPlainObjectExpression = (node) => t.isObjectExpression(node) && node.properties.every((property) => t.isSpreadElement(property) || t.isObjectProperty(property, { computed: false }) && getJSXIdentifier(property.key) != null && getJSXAttributeValue(property.value) != null);
110
+ return {
111
+ visitor: {
112
+ CallExpression(path) {
113
+ const node = getJSXNode(path.node);
114
+ if (node == null) {
115
+ return null;
116
+ }
117
+ path.replaceWith(node);
118
+ }
119
+ }
120
+ };
121
+ }
122
+
123
+ export { babelRestoreJsx as default };