@tamagui/static 1.0.1-beta.99 → 1.0.1-rc.0.1

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