@tamagui/static 1.0.1-beta.20 → 1.0.1-beta.201

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.
Files changed (260) hide show
  1. package/dist/{cjs/constants.js → constants.js} +9 -4
  2. package/dist/constants.js.map +7 -0
  3. package/dist/{cjs/extractor → extractor}/accessSafe.js +14 -4
  4. package/dist/{esm/extractor → extractor}/accessSafe.js.map +2 -2
  5. package/dist/{cjs/extractor → extractor}/babelParse.js +20 -17
  6. package/dist/extractor/babelParse.js.map +7 -0
  7. package/dist/{cjs/extractor → extractor}/buildClassName.js +40 -11
  8. package/dist/extractor/buildClassName.js.map +7 -0
  9. package/dist/extractor/bundle.js +117 -0
  10. package/dist/extractor/bundle.js.map +7 -0
  11. package/dist/{cjs/extractor → extractor}/createEvaluator.js +19 -14
  12. package/dist/extractor/createEvaluator.js.map +7 -0
  13. package/dist/extractor/createExtractor.js +1650 -0
  14. package/dist/extractor/createExtractor.js.map +7 -0
  15. package/dist/{cjs/extractor → extractor}/ensureImportingConcat.js +18 -8
  16. package/dist/extractor/ensureImportingConcat.js.map +7 -0
  17. package/dist/{cjs/extractor → extractor}/evaluateAstNode.js +14 -6
  18. package/dist/{esm/extractor → extractor}/evaluateAstNode.js.map +2 -2
  19. package/dist/{cjs/extractor → extractor}/extractHelpers.js +91 -27
  20. package/dist/extractor/extractHelpers.js.map +7 -0
  21. package/dist/{cjs/extractor → extractor}/extractMediaStyle.js +49 -50
  22. package/dist/extractor/extractMediaStyle.js.map +7 -0
  23. package/dist/{cjs/extractor → extractor}/extractToClassNames.js +161 -109
  24. package/dist/extractor/extractToClassNames.js.map +7 -0
  25. package/dist/{cjs/extractor → extractor}/findTopmostFunction.js +2 -3
  26. package/dist/{esm/extractor → extractor}/findTopmostFunction.js.map +2 -2
  27. package/dist/{cjs/extractor → extractor}/generatedUid.js +9 -4
  28. package/dist/{cjs/extractor → extractor}/generatedUid.js.map +3 -3
  29. package/dist/{cjs/extractor → extractor}/getPrefixLogs.js +1 -2
  30. package/dist/extractor/getPrefixLogs.js.map +7 -0
  31. package/dist/{cjs/extractor → extractor}/getPropValueFromAttributes.js +24 -15
  32. package/dist/extractor/getPropValueFromAttributes.js.map +7 -0
  33. package/dist/{cjs/extractor → extractor}/getSourceModule.js +5 -3
  34. package/dist/{esm/extractor → extractor}/getSourceModule.js.map +2 -2
  35. package/dist/{cjs/extractor → extractor}/getStaticBindingsForScope.js +88 -38
  36. package/dist/extractor/getStaticBindingsForScope.js.map +7 -0
  37. package/dist/{cjs/extractor → extractor}/hoistClassNames.js +5 -4
  38. package/dist/{esm/extractor → extractor}/hoistClassNames.js.map +3 -3
  39. package/dist/{cjs/extractor → extractor}/literalToAst.js +26 -34
  40. package/dist/{cjs/extractor → extractor}/literalToAst.js.map +2 -2
  41. package/dist/extractor/loadFile.js +21 -0
  42. package/dist/extractor/loadFile.js.map +7 -0
  43. package/dist/extractor/loadTamagui.js +421 -0
  44. package/dist/extractor/loadTamagui.js.map +7 -0
  45. package/dist/{cjs/extractor → extractor}/logLines.js +3 -3
  46. package/dist/{cjs/extractor → extractor}/logLines.js.map +2 -2
  47. package/dist/{cjs/extractor → extractor}/normalizeTernaries.js +13 -36
  48. package/dist/extractor/normalizeTernaries.js.map +7 -0
  49. package/dist/{cjs/extractor → extractor}/removeUnusedHooks.js +7 -5
  50. package/dist/{cjs/extractor → extractor}/removeUnusedHooks.js.map +2 -2
  51. package/dist/{cjs/extractor → extractor}/timer.js +11 -4
  52. package/dist/extractor/timer.js.map +7 -0
  53. package/dist/{cjs/extractor → extractor}/validHTMLAttributes.js +52 -0
  54. package/dist/extractor/validHTMLAttributes.js.map +7 -0
  55. package/dist/getPragmaOptions.js +68 -0
  56. package/dist/getPragmaOptions.js.map +7 -0
  57. package/dist/helpers/memoize.js +46 -0
  58. package/dist/helpers/memoize.js.map +7 -0
  59. package/dist/{cjs/index.js → index.js} +10 -7
  60. package/dist/index.js.map +7 -0
  61. package/dist/require.js +90 -0
  62. package/dist/require.js.map +7 -0
  63. package/dist/tamagui-node/tamagui.config.js +6781 -0
  64. package/dist/{jsx/types.js → tamagui-node/tamaguicore-components.config.js} +0 -0
  65. package/dist/tamagui-node/tamaguitestdesignsystem-components.config.js +31 -0
  66. package/dist/{cjs/types.js → types.js} +1 -0
  67. package/dist/types.js.map +7 -0
  68. package/dist/webpackPlugin.js +35 -0
  69. package/dist/webpackPlugin.js.map +7 -0
  70. package/package.json +47 -35
  71. package/src/constants.ts +14 -0
  72. package/src/extractor/accessSafe.ts +18 -0
  73. package/src/extractor/babelParse.ts +28 -0
  74. package/src/extractor/buildClassName.ts +77 -0
  75. package/src/extractor/bundle.ts +110 -0
  76. package/src/extractor/createEvaluator.ts +73 -0
  77. package/src/extractor/createExtractor.ts +2147 -0
  78. package/src/extractor/ensureImportingConcat.ts +32 -0
  79. package/src/extractor/evaluateAstNode.ts +121 -0
  80. package/src/extractor/extractHelpers.ts +201 -0
  81. package/src/extractor/extractMediaStyle.ts +199 -0
  82. package/src/extractor/extractToClassNames.ts +445 -0
  83. package/src/extractor/findTopmostFunction.ts +22 -0
  84. package/src/extractor/generatedUid.ts +43 -0
  85. package/src/extractor/getPrefixLogs.ts +6 -0
  86. package/src/extractor/getPropValueFromAttributes.ts +92 -0
  87. package/src/extractor/getSourceModule.ts +101 -0
  88. package/src/extractor/getStaticBindingsForScope.ts +227 -0
  89. package/src/extractor/hoistClassNames.ts +45 -0
  90. package/src/extractor/literalToAst.ts +84 -0
  91. package/src/extractor/loadFile.ts +17 -0
  92. package/src/extractor/loadTamagui.ts +478 -0
  93. package/src/extractor/logLines.ts +16 -0
  94. package/src/extractor/normalizeTernaries.ts +63 -0
  95. package/src/extractor/removeUnusedHooks.ts +76 -0
  96. package/src/extractor/timer.ts +29 -0
  97. package/src/extractor/validHTMLAttributes.ts +99 -0
  98. package/src/getPragmaOptions.ts +53 -0
  99. package/src/helpers/memoize.ts +21 -0
  100. package/src/index.ts +11 -0
  101. package/src/require.ts +107 -0
  102. package/src/types.ts +98 -0
  103. package/src/webpackPlugin.ts +9 -0
  104. package/tamagui.tsconfig.json +5 -0
  105. package/types/constants.d.ts +1 -1
  106. package/types/extractor/babelParse.d.ts +2 -1
  107. package/types/extractor/buildClassName.d.ts +5 -2
  108. package/types/extractor/bundle.d.ts +13 -0
  109. package/types/extractor/createEvaluator.d.ts +4 -4
  110. package/types/extractor/createExtractor.d.ts +26 -11
  111. package/types/extractor/extractHelpers.d.ts +16 -4
  112. package/types/extractor/extractMediaStyle.d.ts +3 -3
  113. package/types/extractor/extractToClassNames.d.ts +5 -9
  114. package/types/extractor/getPrefixLogs.d.ts +1 -1
  115. package/types/extractor/getPropValueFromAttributes.d.ts +16 -0
  116. package/types/extractor/getStaticBindingsForScope.d.ts +2 -1
  117. package/types/extractor/loadFile.d.ts +1 -0
  118. package/types/extractor/loadTamagui.d.ts +22 -6
  119. package/types/extractor/normalizeTernaries.d.ts +1 -1
  120. package/types/extractor/validHTMLAttributes.d.ts +49 -0
  121. package/types/getPragmaOptions.d.ts +9 -0
  122. package/types/helpers/memoize.d.ts +8 -0
  123. package/types/index.d.ts +9 -7
  124. package/types/require.d.ts +4 -0
  125. package/types/types.d.ts +32 -32
  126. package/types/webpackPlugin.d.ts +4 -0
  127. package/dist/cjs/constants.js.map +0 -7
  128. package/dist/cjs/extractor/accessSafe.js.map +0 -7
  129. package/dist/cjs/extractor/babelParse.js.map +0 -7
  130. package/dist/cjs/extractor/buildClassName.js.map +0 -7
  131. package/dist/cjs/extractor/createEvaluator.js.map +0 -7
  132. package/dist/cjs/extractor/createExtractor.js +0 -1221
  133. package/dist/cjs/extractor/createExtractor.js.map +0 -7
  134. package/dist/cjs/extractor/ensureImportingConcat.js.map +0 -7
  135. package/dist/cjs/extractor/evaluateAstNode.js.map +0 -7
  136. package/dist/cjs/extractor/extractHelpers.js.map +0 -7
  137. package/dist/cjs/extractor/extractMediaStyle.js.map +0 -7
  138. package/dist/cjs/extractor/extractToClassNames.js.map +0 -7
  139. package/dist/cjs/extractor/findTopmostFunction.js.map +0 -7
  140. package/dist/cjs/extractor/getPrefixLogs.js.map +0 -7
  141. package/dist/cjs/extractor/getPropValueFromAttributes.js.map +0 -7
  142. package/dist/cjs/extractor/getSourceModule.js.map +0 -7
  143. package/dist/cjs/extractor/getStaticBindingsForScope.js.map +0 -7
  144. package/dist/cjs/extractor/hoistClassNames.js.map +0 -7
  145. package/dist/cjs/extractor/loadTamagui.js +0 -106
  146. package/dist/cjs/extractor/loadTamagui.js.map +0 -7
  147. package/dist/cjs/extractor/normalizeTernaries.js.map +0 -7
  148. package/dist/cjs/extractor/timer.js.map +0 -7
  149. package/dist/cjs/extractor/validHTMLAttributes.js.map +0 -7
  150. package/dist/cjs/index.js.map +0 -7
  151. package/dist/cjs/patchReactNativeWeb.js +0 -167
  152. package/dist/cjs/patchReactNativeWeb.js.map +0 -7
  153. package/dist/cjs/types.js.map +0 -7
  154. package/dist/esm/constants.js +0 -14
  155. package/dist/esm/constants.js.map +0 -7
  156. package/dist/esm/extractor/accessSafe.js +0 -11
  157. package/dist/esm/extractor/babelParse.js +0 -30
  158. package/dist/esm/extractor/babelParse.js.map +0 -7
  159. package/dist/esm/extractor/buildClassName.js +0 -40
  160. package/dist/esm/extractor/buildClassName.js.map +0 -7
  161. package/dist/esm/extractor/createEvaluator.js +0 -52
  162. package/dist/esm/extractor/createEvaluator.js.map +0 -7
  163. package/dist/esm/extractor/createExtractor.js +0 -1169
  164. package/dist/esm/extractor/createExtractor.js.map +0 -7
  165. package/dist/esm/extractor/ensureImportingConcat.js +0 -24
  166. package/dist/esm/extractor/ensureImportingConcat.js.map +0 -7
  167. package/dist/esm/extractor/evaluateAstNode.js +0 -94
  168. package/dist/esm/extractor/extractHelpers.js +0 -103
  169. package/dist/esm/extractor/extractHelpers.js.map +0 -7
  170. package/dist/esm/extractor/extractMediaStyle.js +0 -152
  171. package/dist/esm/extractor/extractMediaStyle.js.map +0 -7
  172. package/dist/esm/extractor/extractToClassNames.js +0 -293
  173. package/dist/esm/extractor/extractToClassNames.js.map +0 -7
  174. package/dist/esm/extractor/findTopmostFunction.js +0 -23
  175. package/dist/esm/extractor/generatedUid.js +0 -28
  176. package/dist/esm/extractor/generatedUid.js.map +0 -7
  177. package/dist/esm/extractor/getPrefixLogs.js +0 -12
  178. package/dist/esm/extractor/getPrefixLogs.js.map +0 -7
  179. package/dist/esm/extractor/getPropValueFromAttributes.js +0 -49
  180. package/dist/esm/extractor/getPropValueFromAttributes.js.map +0 -7
  181. package/dist/esm/extractor/getSourceModule.js +0 -69
  182. package/dist/esm/extractor/getStaticBindingsForScope.js +0 -137
  183. package/dist/esm/extractor/getStaticBindingsForScope.js.map +0 -7
  184. package/dist/esm/extractor/hoistClassNames.js +0 -44
  185. package/dist/esm/extractor/literalToAst.js +0 -80
  186. package/dist/esm/extractor/literalToAst.js.map +0 -7
  187. package/dist/esm/extractor/loadTamagui.js +0 -84
  188. package/dist/esm/extractor/loadTamagui.js.map +0 -7
  189. package/dist/esm/extractor/logLines.js +0 -22
  190. package/dist/esm/extractor/logLines.js.map +0 -7
  191. package/dist/esm/extractor/normalizeTernaries.js +0 -53
  192. package/dist/esm/extractor/normalizeTernaries.js.map +0 -7
  193. package/dist/esm/extractor/removeUnusedHooks.js +0 -80
  194. package/dist/esm/extractor/removeUnusedHooks.js.map +0 -7
  195. package/dist/esm/extractor/timer.js +0 -24
  196. package/dist/esm/extractor/timer.js.map +0 -7
  197. package/dist/esm/extractor/validHTMLAttributes.js +0 -52
  198. package/dist/esm/extractor/validHTMLAttributes.js.map +0 -7
  199. package/dist/esm/index.js +0 -14
  200. package/dist/esm/index.js.map +0 -7
  201. package/dist/esm/patchReactNativeWeb.js +0 -142
  202. package/dist/esm/patchReactNativeWeb.js.map +0 -7
  203. package/dist/esm/types.js +0 -1
  204. package/dist/esm/types.js.map +0 -7
  205. package/dist/jsx/constants.js +0 -13
  206. package/dist/jsx/extractor/accessSafe.js +0 -10
  207. package/dist/jsx/extractor/babelParse.js +0 -29
  208. package/dist/jsx/extractor/buildClassName.js +0 -39
  209. package/dist/jsx/extractor/createEvaluator.js +0 -51
  210. package/dist/jsx/extractor/createExtractor.js +0 -1168
  211. package/dist/jsx/extractor/ensureImportingConcat.js +0 -23
  212. package/dist/jsx/extractor/evaluateAstNode.js +0 -93
  213. package/dist/jsx/extractor/extractHelpers.js +0 -102
  214. package/dist/jsx/extractor/extractMediaStyle.js +0 -151
  215. package/dist/jsx/extractor/extractToClassNames.js +0 -292
  216. package/dist/jsx/extractor/findTopmostFunction.js +0 -22
  217. package/dist/jsx/extractor/generatedUid.js +0 -27
  218. package/dist/jsx/extractor/getPrefixLogs.js +0 -11
  219. package/dist/jsx/extractor/getPropValueFromAttributes.js +0 -48
  220. package/dist/jsx/extractor/getSourceModule.js +0 -68
  221. package/dist/jsx/extractor/getStaticBindingsForScope.js +0 -136
  222. package/dist/jsx/extractor/hoistClassNames.js +0 -43
  223. package/dist/jsx/extractor/literalToAst.js +0 -79
  224. package/dist/jsx/extractor/loadTamagui.js +0 -83
  225. package/dist/jsx/extractor/logLines.js +0 -21
  226. package/dist/jsx/extractor/normalizeTernaries.js +0 -52
  227. package/dist/jsx/extractor/removeUnusedHooks.js +0 -79
  228. package/dist/jsx/extractor/timer.js +0 -23
  229. package/dist/jsx/extractor/validHTMLAttributes.js +0 -51
  230. package/dist/jsx/index.js +0 -13
  231. package/dist/jsx/patchReactNativeWeb.js +0 -141
  232. package/types/constants.d.ts.map +0 -1
  233. package/types/extractor/accessSafe.d.ts.map +0 -1
  234. package/types/extractor/babelParse.d.ts.map +0 -1
  235. package/types/extractor/buildClassName.d.ts.map +0 -1
  236. package/types/extractor/createEvaluator.d.ts.map +0 -1
  237. package/types/extractor/createExtractor.d.ts.map +0 -1
  238. package/types/extractor/ensureImportingConcat.d.ts.map +0 -1
  239. package/types/extractor/evaluateAstNode.d.ts.map +0 -1
  240. package/types/extractor/extractHelpers.d.ts.map +0 -1
  241. package/types/extractor/extractMediaStyle.d.ts.map +0 -1
  242. package/types/extractor/extractToClassNames.d.ts.map +0 -1
  243. package/types/extractor/findTopmostFunction.d.ts.map +0 -1
  244. package/types/extractor/generatedUid.d.ts.map +0 -1
  245. package/types/extractor/getPrefixLogs.d.ts.map +0 -1
  246. package/types/extractor/getPropValueFromAttributes.d.ts.map +0 -1
  247. package/types/extractor/getSourceModule.d.ts.map +0 -1
  248. package/types/extractor/getStaticBindingsForScope.d.ts.map +0 -1
  249. package/types/extractor/hoistClassNames.d.ts.map +0 -1
  250. package/types/extractor/literalToAst.d.ts.map +0 -1
  251. package/types/extractor/loadTamagui.d.ts.map +0 -1
  252. package/types/extractor/logLines.d.ts.map +0 -1
  253. package/types/extractor/normalizeTernaries.d.ts.map +0 -1
  254. package/types/extractor/removeUnusedHooks.d.ts.map +0 -1
  255. package/types/extractor/timer.d.ts.map +0 -1
  256. package/types/extractor/validHTMLAttributes.d.ts.map +0 -1
  257. package/types/index.d.ts.map +0 -1
  258. package/types/patchReactNativeWeb.d.ts +0 -2
  259. package/types/patchReactNativeWeb.d.ts.map +0 -1
  260. package/types/types.d.ts.map +0 -1
@@ -1,1169 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { relative } from "path";
4
- import traverse from "@babel/traverse";
5
- import * as t from "@babel/types";
6
- import {
7
- getSplitStyles,
8
- mediaQueryConfig,
9
- normalizeStyleObject,
10
- proxyThemeVariables,
11
- pseudos,
12
- rnw
13
- } from "@tamagui/core-node";
14
- import { difference, pick } from "lodash";
15
- import { FAILED_EVAL } from "../constants";
16
- import { createEvaluator, createSafeEvaluator } from "./createEvaluator";
17
- import { evaluateAstNode } from "./evaluateAstNode";
18
- import { attrStr, findComponentName, isInsideTamagui, isPresent, objToStr } from "./extractHelpers";
19
- import { findTopmostFunction } from "./findTopmostFunction";
20
- import { getStaticBindingsForScope } from "./getStaticBindingsForScope";
21
- import { literalToAst } from "./literalToAst";
22
- import { loadTamagui } from "./loadTamagui";
23
- import { logLines } from "./logLines";
24
- import { normalizeTernaries } from "./normalizeTernaries";
25
- import { removeUnusedHooks } from "./removeUnusedHooks";
26
- import { timer } from "./timer";
27
- import { validHTMLAttributes } from "./validHTMLAttributes";
28
- const UNTOUCHED_PROPS = {
29
- key: true,
30
- style: true,
31
- className: true
32
- };
33
- const INLINE_EXTRACTABLE = {
34
- ref: "ref",
35
- key: "key",
36
- onPress: "onClick",
37
- onHoverIn: "onMouseEnter",
38
- onHoverOut: "onMouseLeave",
39
- onPressIn: "onMouseDown",
40
- onPressOut: "onMouseUp"
41
- };
42
- const isAttr = /* @__PURE__ */ __name((x) => x.type === "attr", "isAttr");
43
- const validHooks = {
44
- useMedia: true,
45
- useTheme: true
46
- };
47
- const createTernary = /* @__PURE__ */ __name((x) => x, "createTernary");
48
- function createExtractor() {
49
- if (!process.env.TAMAGUI_TARGET) {
50
- console.log('\u26A0\uFE0F Please set process.env.TAMAGUI_TARGET to either "web" or "native"');
51
- process.exit(1);
52
- }
53
- const shouldAddDebugProp = !process.env.npm_package_dependencies_next && process.env.TAMAGUI_TARGET !== "native" && process.env.IDENTIFY_TAGS !== "false" && (process.env.NODE_ENV === "development" || process.env.DEBUG || process.env.IDENTIFY_TAGS);
54
- let loadedTamaguiConfig;
55
- let hasLogged = false;
56
- return {
57
- getTamagui() {
58
- return loadedTamaguiConfig;
59
- },
60
- parse: (fileOrPath, {
61
- config = "tamagui.config.ts",
62
- importsWhitelist = ["constants.js"],
63
- evaluateVars = true,
64
- shouldPrintDebug = false,
65
- sourcePath = "",
66
- onExtractTag,
67
- getFlattenedNode,
68
- disable,
69
- disableExtraction,
70
- disableExtractInlineMedia,
71
- disableExtractVariables,
72
- disableDebugAttr,
73
- prefixLogs,
74
- excludeProps,
75
- ...props
76
- }) => {
77
- if (disable) {
78
- return null;
79
- }
80
- if (sourcePath === "") {
81
- throw new Error(`Must provide a source file name`);
82
- }
83
- if (!Array.isArray(props.components)) {
84
- throw new Error(`Must provide components array with list of Tamagui component modules`);
85
- }
86
- const ogDebug = shouldPrintDebug;
87
- const tm = timer();
88
- const { components, tamaguiConfig } = loadTamagui({
89
- config,
90
- components: props.components || ["tamagui"]
91
- });
92
- if (shouldPrintDebug === "verbose") {
93
- console.log("tamagui.config.ts:", { components, config });
94
- }
95
- tm.mark("load-tamagui", shouldPrintDebug === "verbose");
96
- loadedTamaguiConfig = tamaguiConfig;
97
- const defaultTheme = proxyThemeVariables(tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]]);
98
- const body = fileOrPath.type === "Program" ? fileOrPath.get("body") : fileOrPath.program.body;
99
- const isInternalImport = /* @__PURE__ */ __name((importStr) => {
100
- return isInsideTamagui(sourcePath) && importStr[0] === ".";
101
- }, "isInternalImport");
102
- const validComponents = Object.keys(components).filter((key) => {
103
- var _a;
104
- return key[0].toUpperCase() === key[0] && !!((_a = components[key]) == null ? void 0 : _a.staticConfig);
105
- }).reduce((obj, name) => {
106
- obj[name] = components[name];
107
- return obj;
108
- }, {});
109
- if (shouldPrintDebug === "verbose") {
110
- console.log("validComponents", Object.keys(validComponents));
111
- }
112
- let doesUseValidImport = false;
113
- let hasImportedTheme = false;
114
- for (const bodyPath of body) {
115
- if (bodyPath.type !== "ImportDeclaration")
116
- continue;
117
- const node = "node" in bodyPath ? bodyPath.node : bodyPath;
118
- const from = node.source.value;
119
- const isValidImport = props.components.includes(from) || isInternalImport(from);
120
- if (isValidImport) {
121
- const isValidComponent = node.specifiers.some((specifier) => {
122
- const name = specifier.local.name;
123
- return validComponents[name] || validHooks[name];
124
- });
125
- if (shouldPrintDebug === "verbose") {
126
- console.log("import from", from, { isValidComponent });
127
- }
128
- if (isValidComponent) {
129
- doesUseValidImport = true;
130
- break;
131
- }
132
- }
133
- }
134
- if (shouldPrintDebug) {
135
- console.log(sourcePath, { doesUseValidImport });
136
- }
137
- if (!doesUseValidImport) {
138
- return null;
139
- }
140
- tm.mark("import-check", shouldPrintDebug === "verbose");
141
- let couldntParse = false;
142
- const modifiedComponents = /* @__PURE__ */ new Set();
143
- const bindingCache = {};
144
- const callTraverse = /* @__PURE__ */ __name((a) => {
145
- return fileOrPath.type === "File" ? traverse(fileOrPath, a) : fileOrPath.traverse(a);
146
- }, "callTraverse");
147
- let programPath;
148
- const res = {
149
- flattened: 0,
150
- optimized: 0,
151
- modified: 0,
152
- found: 0
153
- };
154
- callTraverse({
155
- Program: {
156
- enter(path) {
157
- programPath = path;
158
- }
159
- },
160
- JSXElement(traversePath) {
161
- var _a, _b, _c, _d, _e;
162
- tm.mark("jsx-element", shouldPrintDebug === "verbose");
163
- const node = traversePath.node.openingElement;
164
- const ogAttributes = node.attributes;
165
- const componentName = findComponentName(traversePath.scope);
166
- const closingElement = traversePath.node.closingElement;
167
- if (t.isJSXMemberExpression(closingElement == null ? void 0 : closingElement.name) || !t.isJSXIdentifier(node.name)) {
168
- return;
169
- }
170
- const binding = traversePath.scope.getBinding(node.name.name);
171
- if (binding) {
172
- if (!t.isImportDeclaration(binding.path.parent)) {
173
- return;
174
- }
175
- const source = binding.path.parent.source;
176
- if (!props.components.includes(source.value) && !isInternalImport(source.value)) {
177
- return;
178
- }
179
- if (!validComponents[binding.identifier.name]) {
180
- return;
181
- }
182
- }
183
- const component = validComponents[node.name.name];
184
- if (!component || !component.staticConfig) {
185
- return;
186
- }
187
- const originalNodeName = node.name.name;
188
- res.found++;
189
- const filePath = `./${relative(process.cwd(), sourcePath)}`;
190
- const lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "";
191
- const tagId = [componentName, `${node.name.name}`, `${filePath}:${lineNumbers}`].filter(Boolean);
192
- const debugPropValue = node.attributes.filter((n) => t.isJSXAttribute(n) && t.isJSXIdentifier(n.name) && n.name.name === "debug").map((n) => {
193
- if (n.value === null)
194
- return true;
195
- if (t.isStringLiteral(n.value))
196
- return n.value.value;
197
- return false;
198
- })[0];
199
- if (debugPropValue) {
200
- shouldPrintDebug = debugPropValue;
201
- }
202
- try {
203
- let isValidStyleKey = function(name) {
204
- var _a2;
205
- return !!(!!validStyles[name] || !!pseudos[name] || ((_a2 = staticConfig.variants) == null ? void 0 : _a2[name]) || tamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
206
- }, evaluateAttribute = function(path) {
207
- const attribute = path.node;
208
- const attr = { type: "attr", value: attribute };
209
- if (t.isJSXSpreadAttribute(attribute)) {
210
- const arg = attribute.argument;
211
- const conditional = t.isConditionalExpression(arg) ? [arg.test, arg.consequent, arg.alternate] : t.isLogicalExpression(arg) && arg.operator === "&&" ? [arg.left, arg.right, null] : null;
212
- if (conditional) {
213
- const [test, alt, cons] = conditional;
214
- if (!test)
215
- throw new Error(`no test`);
216
- if ([alt, cons].some((side) => side && !isExtractable(side))) {
217
- if (shouldPrintDebug) {
218
- console.log("not extractable", alt, cons);
219
- }
220
- return attr;
221
- }
222
- return [
223
- ...createTernariesFromObjectProperties(test, alt) || [],
224
- ...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
225
- ].map((ternary) => ({
226
- type: "ternary",
227
- value: ternary
228
- }));
229
- }
230
- }
231
- if (t.isJSXSpreadAttribute(attribute) || !attribute.name || typeof attribute.name.name !== "string") {
232
- if (shouldPrintDebug) {
233
- console.log(" ! inlining, spread attr");
234
- }
235
- inlined.set(`${Math.random()}`, "spread");
236
- return attr;
237
- }
238
- const name = attribute.name.name;
239
- if (excludeProps == null ? void 0 : excludeProps.has(name)) {
240
- if (shouldPrintDebug) {
241
- console.log(" excluding prop", name);
242
- }
243
- return null;
244
- }
245
- if (inlineProps.has(name)) {
246
- inlined.set(name, name);
247
- if (shouldPrintDebug) {
248
- console.log(" ! inlining, inline prop", name);
249
- }
250
- return attr;
251
- }
252
- if (deoptProps.has(name)) {
253
- shouldDeopt = true;
254
- inlined.set(name, name);
255
- if (shouldPrintDebug) {
256
- console.log(" ! inlining, deopted prop", name);
257
- }
258
- return attr;
259
- }
260
- if (UNTOUCHED_PROPS[name]) {
261
- return attr;
262
- }
263
- if (INLINE_EXTRACTABLE[name]) {
264
- inlined.set(name, INLINE_EXTRACTABLE[name]);
265
- return attr;
266
- }
267
- if (name.startsWith("data-")) {
268
- return attr;
269
- }
270
- if (name[0] === "$" && t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
271
- if (disableExtractInlineMedia) {
272
- return attr;
273
- }
274
- const shortname = name.slice(1);
275
- if (mediaQueryConfig[shortname]) {
276
- const expression = attribute.value.expression;
277
- if (!t.isJSXEmptyExpression(expression)) {
278
- const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(shortname), expression, {
279
- inlineMediaQuery: shortname
280
- });
281
- if (ternaries2) {
282
- return ternaries2.map((value2) => ({
283
- type: "ternary",
284
- value: value2
285
- }));
286
- }
287
- }
288
- }
289
- }
290
- const [value, valuePath] = (() => {
291
- if (t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
292
- return [attribute.value.expression, path.get("value")];
293
- } else {
294
- return [attribute.value, path.get("value")];
295
- }
296
- })();
297
- const remove = /* @__PURE__ */ __name(() => {
298
- Array.isArray(valuePath) ? valuePath.map((p) => p.remove()) : valuePath.remove();
299
- }, "remove");
300
- if (name === "ref") {
301
- if (shouldPrintDebug) {
302
- console.log(" ! inlining, ref", name);
303
- }
304
- inlined.set("ref", "ref");
305
- return attr;
306
- }
307
- if (name === "tag") {
308
- return {
309
- type: "attr",
310
- value: path.node
311
- };
312
- }
313
- if (disableExtractVariables) {
314
- if (value) {
315
- if (value.type === "StringLiteral" && value.value[0] === "$") {
316
- if (shouldPrintDebug) {
317
- console.log(` ! inlining, native disable extract: ${name} =`, value.value);
318
- }
319
- inlined.set(name, true);
320
- return attr;
321
- }
322
- }
323
- }
324
- if (name === "theme") {
325
- inlined.set("theme", attr.value);
326
- return attr;
327
- }
328
- const styleValue = attemptEvalSafe(value);
329
- if (!isValidStyleKey(name)) {
330
- let keys = [name];
331
- let out = null;
332
- if (staticConfig.propMapper) {
333
- out = staticConfig.propMapper(name, styleValue, defaultTheme, staticConfig.defaultProps, "auto");
334
- if (out) {
335
- out = rnw.createDOMProps(isTextView ? "span" : "div", out);
336
- delete out.className;
337
- keys = Object.keys(out);
338
- }
339
- }
340
- let didInline = false;
341
- const attributes = keys.map((key) => {
342
- const val = out[key];
343
- if (isValidStyleKey(key)) {
344
- return {
345
- type: "style",
346
- value: { [name]: styleValue },
347
- name,
348
- attr: path.node
349
- };
350
- }
351
- if (validHTMLAttributes[key]) {
352
- return attr;
353
- }
354
- if (shouldPrintDebug) {
355
- console.log(" ! inlining, non-static", key);
356
- }
357
- didInline = true;
358
- inlined.set(key, val);
359
- return val;
360
- });
361
- if (didInline) {
362
- if (shouldPrintDebug) {
363
- console.log(" bailing flattening due to attributes", attributes);
364
- }
365
- return attr;
366
- }
367
- return attributes;
368
- }
369
- if (styleValue !== FAILED_EVAL) {
370
- if (isValidStyleKey(name)) {
371
- if (shouldPrintDebug) {
372
- console.log(` style: ${name} =`, styleValue);
373
- }
374
- if (!(name in staticConfig.defaultProps)) {
375
- if (!hasSetOptimized) {
376
- res.optimized++;
377
- hasSetOptimized = true;
378
- }
379
- }
380
- return {
381
- type: "style",
382
- value: { [name]: styleValue },
383
- name,
384
- attr: path.node
385
- };
386
- } else {
387
- inlined.set(name, true);
388
- return attr;
389
- }
390
- }
391
- if (t.isBinaryExpression(value)) {
392
- if (shouldPrintDebug) {
393
- console.log(` binary expression ${name} = `, value);
394
- }
395
- const { operator, left, right } = value;
396
- const lVal = attemptEvalSafe(left);
397
- const rVal = attemptEvalSafe(right);
398
- if (shouldPrintDebug) {
399
- console.log(` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`);
400
- }
401
- if (lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
402
- const ternary = addBinaryConditional(operator, left, right);
403
- if (ternary)
404
- return ternary;
405
- }
406
- if (rVal !== FAILED_EVAL && t.isConditionalExpression(left)) {
407
- const ternary = addBinaryConditional(operator, right, left);
408
- if (ternary)
409
- return ternary;
410
- }
411
- if (shouldPrintDebug) {
412
- console.log(` evalBinaryExpression cant extract`);
413
- }
414
- inlined.set(name, true);
415
- return attr;
416
- }
417
- const staticConditional = getStaticConditional(value);
418
- if (staticConditional) {
419
- if (shouldPrintDebug === "verbose") {
420
- console.log(` static conditional ${name}`, value);
421
- }
422
- return { type: "ternary", value: staticConditional };
423
- }
424
- const staticLogical = getStaticLogical(value);
425
- if (staticLogical) {
426
- if (shouldPrintDebug === "verbose") {
427
- console.log(` static ternary ${name} = `, value);
428
- }
429
- return { type: "ternary", value: staticLogical };
430
- }
431
- inlined.set(name, true);
432
- if (shouldPrintDebug) {
433
- console.log(` ! inline no match ${name}`, value);
434
- }
435
- return attr;
436
- function addBinaryConditional(operator, staticExpr, cond) {
437
- if (getStaticConditional(cond)) {
438
- const alt = attemptEval(t.binaryExpression(operator, staticExpr, cond.alternate));
439
- const cons = attemptEval(t.binaryExpression(operator, staticExpr, cond.consequent));
440
- if (shouldPrintDebug) {
441
- console.log(" binaryConditional", cond.test, cons, alt);
442
- }
443
- return {
444
- type: "ternary",
445
- value: {
446
- test: cond.test,
447
- remove,
448
- alternate: { [name]: alt },
449
- consequent: { [name]: cons }
450
- }
451
- };
452
- }
453
- return null;
454
- }
455
- __name(addBinaryConditional, "addBinaryConditional");
456
- function getStaticConditional(value2) {
457
- if (t.isConditionalExpression(value2)) {
458
- try {
459
- const aVal = attemptEval(value2.alternate);
460
- const cVal = attemptEval(value2.consequent);
461
- if (shouldPrintDebug) {
462
- const type = value2.test.type;
463
- console.log(" static ternary", type, cVal, aVal);
464
- }
465
- return {
466
- test: value2.test,
467
- remove,
468
- consequent: { [name]: cVal },
469
- alternate: { [name]: aVal }
470
- };
471
- } catch (err) {
472
- if (shouldPrintDebug) {
473
- console.log(" cant eval ternary", err.message);
474
- }
475
- }
476
- }
477
- return null;
478
- }
479
- __name(getStaticConditional, "getStaticConditional");
480
- function getStaticLogical(value2) {
481
- if (t.isLogicalExpression(value2)) {
482
- if (value2.operator === "&&") {
483
- try {
484
- const val = attemptEval(value2.right);
485
- if (shouldPrintDebug) {
486
- console.log(" staticLogical", value2.left, name, val);
487
- }
488
- return {
489
- test: value2.left,
490
- remove,
491
- consequent: { [name]: val },
492
- alternate: null
493
- };
494
- } catch (err) {
495
- if (shouldPrintDebug) {
496
- console.log(" cant static eval logical", err);
497
- }
498
- }
499
- }
500
- }
501
- return null;
502
- }
503
- __name(getStaticLogical, "getStaticLogical");
504
- }, isExtractable = function(obj) {
505
- return t.isObjectExpression(obj) && obj.properties.every((prop) => {
506
- if (!t.isObjectProperty(prop)) {
507
- console.log("not object prop", prop);
508
- return false;
509
- }
510
- const propName = prop.key["name"];
511
- if (!isValidStyleKey(propName) && propName !== "tag") {
512
- if (shouldPrintDebug) {
513
- console.log(" not a valid style prop!", propName);
514
- }
515
- return false;
516
- }
517
- return true;
518
- });
519
- }, createTernariesFromObjectProperties = function(test, side, ternaryPartial = {}) {
520
- if (!side) {
521
- return null;
522
- }
523
- if (!isExtractable(side)) {
524
- throw new Error("not extractable");
525
- }
526
- return side.properties.flatMap((property) => {
527
- if (!t.isObjectProperty(property)) {
528
- throw new Error("expected object property");
529
- }
530
- if (t.isIdentifier(property.key)) {
531
- const key = property.key.name;
532
- const mediaQueryKey = key.slice(1);
533
- const isMediaQuery = key[0] === "$" && mediaQueryConfig[mediaQueryKey];
534
- if (isMediaQuery) {
535
- if (t.isExpression(property.value)) {
536
- const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(mediaQueryKey), property.value, {
537
- inlineMediaQuery: mediaQueryKey
538
- });
539
- if (ternaries2) {
540
- return ternaries2.map((value) => ({
541
- ...ternaryPartial,
542
- ...value,
543
- test: t.logicalExpression("&&", value.test, test)
544
- }));
545
- } else {
546
- console.log("\u26A0\uFE0F no ternaries?", property);
547
- }
548
- } else {
549
- console.log("\u26A0\uFE0F not expression", property);
550
- }
551
- }
552
- }
553
- if (t.isConditionalExpression(property.value)) {
554
- const [truthy, falsy] = [
555
- t.objectExpression([t.objectProperty(property.key, property.value.consequent)]),
556
- t.objectExpression([t.objectProperty(property.key, property.value.alternate)])
557
- ].map((x) => attemptEval(x));
558
- return [
559
- createTernary({
560
- remove() {
561
- },
562
- ...ternaryPartial,
563
- test: t.logicalExpression("&&", test, property.value.test),
564
- consequent: truthy,
565
- alternate: null
566
- }),
567
- createTernary({
568
- ...ternaryPartial,
569
- test: t.logicalExpression("&&", test, t.unaryExpression("!", property.value.test)),
570
- consequent: falsy,
571
- alternate: null,
572
- remove() {
573
- }
574
- })
575
- ];
576
- }
577
- const obj = t.objectExpression([t.objectProperty(property.key, property.value)]);
578
- const consequent = attemptEval(obj);
579
- return createTernary({
580
- remove() {
581
- },
582
- ...ternaryPartial,
583
- test,
584
- consequent,
585
- alternate: null
586
- });
587
- });
588
- }, mergeStyles = function(prev2, next) {
589
- normalizeStyleObject(next);
590
- for (const key in next) {
591
- if (pseudos[key]) {
592
- prev2[key] = prev2[key] || {};
593
- if (shouldPrintDebug) {
594
- if (!next[key] || !prev2[key]) {
595
- console.log("warn: missing", key, prev2, next);
596
- }
597
- }
598
- Object.assign(prev2[key], next[key]);
599
- } else {
600
- prev2[key] = next[key];
601
- }
602
- }
603
- };
604
- __name(isValidStyleKey, "isValidStyleKey");
605
- __name(evaluateAttribute, "evaluateAttribute");
606
- __name(isExtractable, "isExtractable");
607
- __name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
608
- __name(mergeStyles, "mergeStyles");
609
- node.attributes.find((n) => t.isJSXAttribute(n) && t.isJSXIdentifier(n.name) && n.name.name === "debug" && n.value === null);
610
- if (shouldPrintDebug) {
611
- console.log("\n");
612
- console.log("\x1B[33m%s\x1B[0m", `${tagId[0]} | ${tagId[2]} -------------------`);
613
- console.log("\x1B[1m", "\x1B[32m", `<${originalNodeName} />`);
614
- }
615
- if (shouldAddDebugProp && !disableDebugAttr) {
616
- res.modified++;
617
- node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(tagId.join(" "))));
618
- }
619
- const shouldLog = !hasLogged;
620
- if (shouldLog) {
621
- const prefix = " |";
622
- console.log(prefixLogs || prefix, " total \xB7 optimized \xB7 flattened ");
623
- hasLogged = true;
624
- }
625
- if (disableExtraction) {
626
- return;
627
- }
628
- const { staticConfig } = component;
629
- const isTextView = staticConfig.isText || false;
630
- const validStyles = (_a = staticConfig == null ? void 0 : staticConfig.validStyles) != null ? _a : {};
631
- let tagName = (_c = (_b = staticConfig.defaultProps) == null ? void 0 : _b.tag) != null ? _c : isTextView ? "span" : "div";
632
- traversePath.get("openingElement").get("attributes").forEach((path) => {
633
- const attr = path.node;
634
- if (t.isJSXSpreadAttribute(attr))
635
- return;
636
- if (attr.name.name !== "tag")
637
- return;
638
- const val = attr.value;
639
- if (!t.isStringLiteral(val))
640
- return;
641
- tagName = val.value;
642
- });
643
- const flatNode = getFlattenedNode({ isTextView, tag: tagName });
644
- const inlineProps = /* @__PURE__ */ new Set([
645
- ...props.inlineProps || [],
646
- ...staticConfig.inlineProps || []
647
- ]);
648
- const deoptProps = /* @__PURE__ */ new Set([
649
- "animation",
650
- ...props.deoptProps || [],
651
- ...staticConfig.deoptProps || []
652
- ]);
653
- const staticNamespace = getStaticBindingsForScope(traversePath.scope, importsWhitelist, sourcePath, bindingCache, shouldPrintDebug);
654
- const attemptEval = !evaluateVars ? evaluateAstNode : createEvaluator({
655
- tamaguiConfig,
656
- staticNamespace,
657
- sourcePath,
658
- traversePath,
659
- shouldPrintDebug
660
- });
661
- const attemptEvalSafe = createSafeEvaluator(attemptEval);
662
- if (shouldPrintDebug) {
663
- console.log(" staticNamespace", Object.keys(staticNamespace).join(", "));
664
- }
665
- const flattenedAttrs = [];
666
- traversePath.get("openingElement").get("attributes").forEach((path) => {
667
- const attr = path.node;
668
- if (!t.isJSXSpreadAttribute(attr)) {
669
- flattenedAttrs.push(attr);
670
- return;
671
- }
672
- let arg;
673
- try {
674
- arg = attemptEval(attr.argument);
675
- } catch (e) {
676
- if (shouldPrintDebug) {
677
- console.log(" couldnt parse spread", e.message);
678
- }
679
- flattenedAttrs.push(attr);
680
- return;
681
- }
682
- if (arg !== void 0) {
683
- try {
684
- if (typeof arg !== "object" || arg == null) {
685
- if (shouldPrintDebug) {
686
- console.log(" non object or null arg", arg);
687
- }
688
- flattenedAttrs.push(attr);
689
- } else {
690
- for (const k in arg) {
691
- const value = arg[k];
692
- if (!value && typeof value === "object") {
693
- console.log("shouldnt we handle this?", k, value, arg);
694
- continue;
695
- }
696
- flattenedAttrs.push(t.jsxAttribute(t.jsxIdentifier(k), t.jsxExpressionContainer(literalToAst(value))));
697
- }
698
- }
699
- } catch (err) {
700
- console.warn("cant parse spread, caught err", err);
701
- couldntParse = true;
702
- }
703
- }
704
- });
705
- if (couldntParse) {
706
- return;
707
- }
708
- tm.mark("jsx-element-flattened", shouldPrintDebug === "verbose");
709
- node.attributes = flattenedAttrs;
710
- if (staticConfig.defaultProps) {
711
- for (const key in staticConfig.defaultProps) {
712
- if (isValidStyleKey(key)) {
713
- continue;
714
- }
715
- const serialize = require("babel-literal-to-ast");
716
- const val = staticConfig.defaultProps[key];
717
- try {
718
- const serializedDefaultProp = serialize(val);
719
- node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(key), typeof val === "string" ? t.stringLiteral(val) : t.jsxExpressionContainer(serializedDefaultProp)));
720
- } catch (err) {
721
- console.warn(`\u26A0\uFE0F Error evaluating default prop for component ${node.name.name}, prop ${key}
722
- error: ${err}
723
- value:`, val, "\n defaultProps:", staticConfig.defaultProps);
724
- }
725
- }
726
- }
727
- let attrs = [];
728
- let shouldDeopt = false;
729
- const inlined = /* @__PURE__ */ new Map();
730
- let hasSetOptimized = false;
731
- attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
732
- try {
733
- const res2 = evaluateAttribute(path);
734
- tm.mark("jsx-element-evaluate-attr", shouldPrintDebug === "verbose");
735
- if (!res2) {
736
- path.remove();
737
- }
738
- return res2;
739
- } catch (err) {
740
- if (shouldPrintDebug) {
741
- console.log("Error extracting attribute", err.message, err.stack);
742
- console.log("node", path.node);
743
- }
744
- return {
745
- type: "attr",
746
- value: path.node
747
- };
748
- }
749
- }).flat(4).filter(isPresent);
750
- if (shouldPrintDebug) {
751
- console.log(" - attrs (before):\n", logLines(attrs.map(attrStr).join(", ")));
752
- }
753
- node.attributes = attrs.filter(isAttr).map((x) => x.value);
754
- if (couldntParse || shouldDeopt) {
755
- if (shouldPrintDebug) {
756
- console.log(` avoid optimizing:`, { couldntParse, shouldDeopt });
757
- }
758
- node.attributes = ogAttributes;
759
- return;
760
- }
761
- const parentFn = findTopmostFunction(traversePath);
762
- if (parentFn) {
763
- modifiedComponents.add(parentFn);
764
- }
765
- let ternaries = [];
766
- attrs = attrs.reduce((out, cur) => {
767
- const next = attrs[attrs.indexOf(cur) + 1];
768
- if (cur.type === "ternary") {
769
- ternaries.push(cur.value);
770
- }
771
- if ((!next || next.type !== "ternary") && ternaries.length) {
772
- const normalized = normalizeTernaries(ternaries).map(({ alternate, consequent, ...rest }) => {
773
- return {
774
- type: "ternary",
775
- value: {
776
- ...rest,
777
- alternate: alternate || null,
778
- consequent: consequent || null
779
- }
780
- };
781
- });
782
- try {
783
- return [...out, ...normalized];
784
- } finally {
785
- if (shouldPrintDebug) {
786
- console.log(` normalizeTernaries (${ternaries.length} => ${normalized.length})`);
787
- }
788
- ternaries = [];
789
- }
790
- }
791
- if (cur.type === "ternary") {
792
- return out;
793
- }
794
- out.push(cur);
795
- return out;
796
- }, []).flat();
797
- const hasSpread = node.attributes.some((x) => t.isJSXSpreadAttribute(x));
798
- const hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText"));
799
- let themeVal = inlined.get("theme");
800
- inlined.delete("theme");
801
- const allOtherPropsExtractable = [...inlined].every(([k, v]) => INLINE_EXTRACTABLE[k]);
802
- const shouldWrapThme = allOtherPropsExtractable && !!themeVal;
803
- const canFlattenProps = inlined.size === 0 || shouldWrapThme || allOtherPropsExtractable;
804
- let shouldFlatten = !shouldDeopt && canFlattenProps && !hasSpread && staticConfig.neverFlatten !== true && (staticConfig.neverFlatten === "jsx" ? hasOnlyStringChildren : true);
805
- if (shouldPrintDebug) {
806
- console.log(" - flatten?", objToStr({ hasSpread, shouldDeopt, shouldFlatten, canFlattenProps, shouldWrapThme, allOtherPropsExtractable, hasOnlyStringChildren }));
807
- }
808
- if (shouldFlatten && shouldWrapThme) {
809
- if (shouldPrintDebug) {
810
- console.log(" - wrapping theme", allOtherPropsExtractable, themeVal);
811
- }
812
- attrs = attrs.filter((x) => x.type === "attr" && t.isJSXAttribute(x.value) && x.value.name.name === "theme" ? false : true);
813
- if (!hasImportedTheme) {
814
- hasImportedTheme = true;
815
- programPath.node.body.push(t.importDeclaration([t.importSpecifier(t.identifier("_TamaguiTheme"), t.identifier("Theme"))], t.stringLiteral("@tamagui/core")));
816
- }
817
- traversePath.replaceWith(t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("_TamaguiTheme"), [
818
- t.jsxAttribute(t.jsxIdentifier("name"), themeVal.value)
819
- ]), t.jsxClosingElement(t.jsxIdentifier("_TamaguiTheme")), [traversePath.node]));
820
- }
821
- if (shouldFlatten && staticConfig.defaultProps) {
822
- const defaultStyleAttrs = Object.keys(staticConfig.defaultProps).flatMap((key) => {
823
- if (!isValidStyleKey(key)) {
824
- return [];
825
- }
826
- const value = staticConfig.defaultProps[key];
827
- const name = tamaguiConfig.shorthands[key] || key;
828
- if (value === void 0) {
829
- console.warn(`\u26A0\uFE0F Error evaluating default style for component, prop ${key} ${value}`);
830
- shouldDeopt = true;
831
- return;
832
- }
833
- const attr = {
834
- type: "style",
835
- name,
836
- value: { [name]: value }
837
- };
838
- return attr;
839
- });
840
- if (defaultStyleAttrs.length) {
841
- attrs = [...defaultStyleAttrs, ...attrs];
842
- }
843
- }
844
- if (shouldDeopt) {
845
- node.attributes = ogAttributes;
846
- return;
847
- }
848
- const ensureOverridden = {};
849
- if (!shouldFlatten) {
850
- for (const cur of attrs) {
851
- if (cur.type === "style") {
852
- for (const key in cur.value) {
853
- const shouldEnsureOverridden = !!((_d = staticConfig.ensureOverriddenProp) == null ? void 0 : _d[key]);
854
- const isSetInAttrsAlready = attrs.some((x) => x.type === "attr" && x.value.type === "JSXAttribute" && x.value.name.name === key);
855
- if (!isSetInAttrsAlready) {
856
- const isVariant = !!((_e = staticConfig.variants) == null ? void 0 : _e[cur.name || ""]);
857
- if (isVariant || shouldEnsureOverridden) {
858
- ensureOverridden[key] = true;
859
- }
860
- }
861
- }
862
- }
863
- }
864
- }
865
- if (shouldPrintDebug) {
866
- console.log(" - attrs (flattened): \n", logLines(attrs.map(attrStr).join(", ")));
867
- console.log(" - ensureOverriden:", Object.keys(ensureOverridden).join(", "));
868
- }
869
- attrs = attrs.reduce((acc, cur) => {
870
- if (!cur) {
871
- return acc;
872
- }
873
- if (cur.type === "attr" && !t.isJSXSpreadAttribute(cur.value)) {
874
- if (shouldFlatten) {
875
- if (cur.value.name.name === "tag") {
876
- return acc;
877
- }
878
- }
879
- }
880
- if (cur.type !== "style") {
881
- acc.push(cur);
882
- return acc;
883
- }
884
- let key = Object.keys(cur.value)[0];
885
- const value = cur.value[key];
886
- const fullKey = tamaguiConfig.shorthands[key];
887
- if (fullKey) {
888
- cur.value = { [fullKey]: value };
889
- key = fullKey;
890
- }
891
- if (disableExtractVariables) {
892
- if (value[0] === "$") {
893
- if (shouldPrintDebug) {
894
- console.log(` keeping variable inline: ${key} =`, value);
895
- }
896
- acc.push({
897
- type: "attr",
898
- value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.stringLiteral(value)))
899
- });
900
- return acc;
901
- }
902
- }
903
- acc.push(cur);
904
- return acc;
905
- }, []);
906
- tm.mark("jsx-element-expanded", shouldPrintDebug === "verbose");
907
- if (shouldPrintDebug) {
908
- console.log(" - attrs (expanded): \n", logLines(attrs.map(attrStr).join(", ")));
909
- }
910
- let prev = null;
911
- attrs = attrs.reduce((acc, cur) => {
912
- if (cur.type === "style") {
913
- const key = Object.keys(cur.value)[0];
914
- const value = cur.value[key];
915
- const shouldKeepOriginalAttr = !shouldFlatten && !validStyles[key] && !pseudos[key] && !key.startsWith("data-");
916
- if (shouldKeepOriginalAttr) {
917
- if (shouldPrintDebug) {
918
- console.log(" - keeping as non-style", key);
919
- }
920
- prev = cur;
921
- acc.push({
922
- type: "attr",
923
- value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(typeof value === "string" ? t.stringLiteral(value) : literalToAst(value)))
924
- });
925
- acc.push(cur);
926
- return acc;
927
- }
928
- if (ensureOverridden[key]) {
929
- acc.push({
930
- type: "attr",
931
- value: cur.attr || t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.nullLiteral()))
932
- });
933
- }
934
- if ((prev == null ? void 0 : prev.type) === "style") {
935
- mergeStyles(prev.value, cur.value);
936
- return acc;
937
- }
938
- }
939
- prev = cur;
940
- acc.push(cur);
941
- return acc;
942
- }, []);
943
- const state = {
944
- noClassNames: true,
945
- focus: false,
946
- hover: false,
947
- mounted: true,
948
- press: false,
949
- pressIn: false
950
- };
951
- const completeStaticProps = Object.keys(attrs).reduce((acc, index) => {
952
- const cur = attrs[index];
953
- if (cur.type === "style") {
954
- normalizeStyleObject(cur.value);
955
- Object.assign(acc, cur.value);
956
- }
957
- if (cur.type === "attr") {
958
- if (t.isJSXSpreadAttribute(cur.value)) {
959
- return acc;
960
- }
961
- if (!t.isJSXIdentifier(cur.value.name)) {
962
- return acc;
963
- }
964
- const key = cur.value.name.name;
965
- const value = attemptEvalSafe(cur.value.value || t.booleanLiteral(true));
966
- if (value === FAILED_EVAL) {
967
- return acc;
968
- }
969
- acc[key] = value;
970
- }
971
- return acc;
972
- }, {});
973
- const completeProps = {
974
- ...staticConfig.defaultProps,
975
- ...completeStaticProps
976
- };
977
- if (shouldPrintDebug) {
978
- console.log(" - attrs (combined \u{1F500}): \n", logLines(attrs.map(attrStr).join(", ")));
979
- console.log(" - defaultProps: \n", logLines(objToStr(staticConfig.defaultProps)));
980
- console.log(" - completeStaticProps: \n", logLines(objToStr(completeStaticProps)));
981
- }
982
- const getStyles = /* @__PURE__ */ __name((props2, debugName = "") => {
983
- if (!props2 || !Object.keys(props2).length) {
984
- if (shouldPrintDebug)
985
- console.log(" getStyles() no props");
986
- return {};
987
- }
988
- if (excludeProps && !!excludeProps.size) {
989
- for (const key in props2) {
990
- if (excludeProps.has(key)) {
991
- if (shouldPrintDebug)
992
- console.log(" delete excluded", key);
993
- delete props2[key];
994
- }
995
- }
996
- }
997
- try {
998
- const out = getSplitStyles(props2, staticConfig, defaultTheme, {
999
- ...state,
1000
- fallbackProps: completeProps
1001
- });
1002
- const outStyle = {
1003
- ...out.style,
1004
- ...out.pseudos
1005
- };
1006
- if (staticConfig.validStyles) {
1007
- for (const key in outStyle) {
1008
- if (!staticConfig.validStyles[key] && !pseudos[key] && !/(hover|focus|press)Style$/.test(key)) {
1009
- if (shouldPrintDebug)
1010
- console.log(" delete invalid style", key);
1011
- delete outStyle[key];
1012
- }
1013
- }
1014
- }
1015
- if (shouldPrintDebug) {
1016
- console.log(` getStyles ${debugName} (props):
1017
- `, logLines(objToStr(props2)));
1018
- console.log(` getStyles ${debugName} (out.viewProps):
1019
- `, logLines(objToStr(out.viewProps)));
1020
- console.log(` getStyles ${debugName} (out.style):
1021
- `, logLines(objToStr(outStyle || {}), true));
1022
- }
1023
- return outStyle;
1024
- } catch (err) {
1025
- console.log("error", err.message, err.stack);
1026
- return {};
1027
- }
1028
- }, "getStyles");
1029
- const completeStylesProcessed = getStyles(completeProps, "completeStylesProcessed");
1030
- if (!completeStylesProcessed) {
1031
- throw new Error(`Impossible, no styles`);
1032
- }
1033
- const stylesToAddToInitialGroup = shouldFlatten ? difference(Object.keys(completeStylesProcessed), Object.keys(completeStaticProps)) : [];
1034
- if (stylesToAddToInitialGroup.length) {
1035
- const toAdd = pick(completeStylesProcessed, ...stylesToAddToInitialGroup);
1036
- const firstGroup = attrs.find((x) => x.type === "style");
1037
- if (shouldPrintDebug) {
1038
- console.log(" toAdd", objToStr(toAdd));
1039
- }
1040
- if (!firstGroup) {
1041
- attrs.unshift({ type: "style", value: toAdd });
1042
- } else {
1043
- Object.assign(firstGroup.value, toAdd);
1044
- }
1045
- }
1046
- if (shouldPrintDebug) {
1047
- console.log(" -- stylesToAddToInitialGroup", stylesToAddToInitialGroup.join(", "), { shouldFlatten });
1048
- console.log(" -- completeStaticProps:\n", logLines(objToStr(completeStaticProps)));
1049
- console.log(" -- completeStylesProcessed:\n", logLines(objToStr(completeStylesProcessed)));
1050
- }
1051
- let getStyleError = null;
1052
- for (const attr of attrs) {
1053
- try {
1054
- if (shouldPrintDebug)
1055
- console.log(" *", attrStr(attr));
1056
- switch (attr.type) {
1057
- case "ternary":
1058
- const a = getStyles(attr.value.alternate, "ternary.alternate");
1059
- const c = getStyles(attr.value.consequent, "ternary.consequent");
1060
- if (a)
1061
- attr.value.alternate = a;
1062
- if (c)
1063
- attr.value.consequent = c;
1064
- if (shouldPrintDebug)
1065
- console.log(" => tern ", attrStr(attr));
1066
- continue;
1067
- case "style":
1068
- if (shouldPrintDebug)
1069
- console.log(" * styles in", logLines(objToStr(attr.value)));
1070
- const styles = getStyles(attr.value, "style");
1071
- if (shouldPrintDebug)
1072
- console.log(" * styles out", logLines(objToStr(styles)));
1073
- if (styles) {
1074
- attr.value = Object.fromEntries(Object.keys(styles).map((k) => [k, completeStylesProcessed[k]]));
1075
- }
1076
- continue;
1077
- }
1078
- } catch (err) {
1079
- getStyleError = err;
1080
- }
1081
- }
1082
- tm.mark("jsx-element-styles", shouldPrintDebug === "verbose");
1083
- if (getStyleError) {
1084
- console.log(" \u26A0\uFE0F postprocessing error, deopt", getStyleError);
1085
- node.attributes = ogAttributes;
1086
- return node;
1087
- }
1088
- const existingStyleKeys = /* @__PURE__ */ new Set();
1089
- for (let i = attrs.length - 1; i >= 0; i--) {
1090
- const attr = attrs[i];
1091
- if (shouldFlatten && canFlattenProps) {
1092
- if (attr.type === "attr") {
1093
- if (t.isJSXAttribute(attr.value)) {
1094
- if (t.isJSXIdentifier(attr.value.name)) {
1095
- const name = attr.value.name.name;
1096
- if (INLINE_EXTRACTABLE[name]) {
1097
- attr.value.name.name = INLINE_EXTRACTABLE[name];
1098
- }
1099
- }
1100
- }
1101
- }
1102
- }
1103
- if (attr.type === "style") {
1104
- for (const key in attr.value) {
1105
- if (existingStyleKeys.has(key)) {
1106
- if (shouldPrintDebug) {
1107
- console.log(" >> delete existing", key);
1108
- }
1109
- delete attr.value[key];
1110
- } else {
1111
- existingStyleKeys.add(key);
1112
- }
1113
- }
1114
- }
1115
- }
1116
- if (shouldFlatten) {
1117
- if (shouldPrintDebug) {
1118
- console.log(" [\u2705] flattening", originalNodeName, flatNode);
1119
- }
1120
- node.name.name = flatNode;
1121
- res.flattened++;
1122
- if (closingElement) {
1123
- closingElement.name.name = flatNode;
1124
- }
1125
- }
1126
- if (shouldPrintDebug) {
1127
- console.log(` \u274A\u274A inline props (${inlined.size}):`, shouldDeopt ? " deopted" : "", hasSpread ? " has spread" : "", staticConfig.neverFlatten ? "neverFlatten" : "");
1128
- console.log(" - attrs (end):\n", logLines(attrs.map(attrStr).join(", ")));
1129
- }
1130
- onExtractTag({
1131
- attrs,
1132
- node,
1133
- lineNumbers,
1134
- filePath,
1135
- attemptEval,
1136
- jsxPath: traversePath,
1137
- originalNodeName,
1138
- isFlattened: shouldFlatten,
1139
- programPath
1140
- });
1141
- } catch (err) {
1142
- throw err;
1143
- } finally {
1144
- if (debugPropValue) {
1145
- shouldPrintDebug = ogDebug;
1146
- }
1147
- }
1148
- }
1149
- });
1150
- tm.mark("jsx-done", shouldPrintDebug === "verbose");
1151
- if (modifiedComponents.size) {
1152
- const all = Array.from(modifiedComponents);
1153
- if (shouldPrintDebug) {
1154
- console.log(" [\u{1FA9D}] hook check", all.length);
1155
- }
1156
- for (const comp of all) {
1157
- removeUnusedHooks(comp, shouldPrintDebug);
1158
- }
1159
- }
1160
- tm.done(shouldPrintDebug === "verbose");
1161
- return res;
1162
- }
1163
- };
1164
- }
1165
- __name(createExtractor, "createExtractor");
1166
- export {
1167
- createExtractor
1168
- };
1169
- //# sourceMappingURL=createExtractor.js.map