@tamagui/static 1.0.0-alpha.9 → 1.0.0-beta.0.209

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 (243) hide show
  1. package/README.md +3 -19
  2. package/dist/cjs/constants.js +44 -0
  3. package/dist/cjs/constants.js.map +7 -0
  4. package/dist/cjs/extractor/accessSafe.js +36 -0
  5. package/dist/cjs/extractor/accessSafe.js.map +7 -0
  6. package/dist/cjs/extractor/babelParse.js +56 -0
  7. package/dist/cjs/extractor/babelParse.js.map +7 -0
  8. package/dist/cjs/extractor/buildClassName.js +65 -0
  9. package/dist/cjs/extractor/buildClassName.js.map +7 -0
  10. package/dist/cjs/extractor/createEvaluator.js +78 -0
  11. package/dist/cjs/extractor/createEvaluator.js.map +7 -0
  12. package/dist/cjs/extractor/createExtractor.js +1221 -0
  13. package/dist/cjs/extractor/createExtractor.js.map +7 -0
  14. package/dist/cjs/extractor/ensureImportingConcat.js +49 -0
  15. package/dist/cjs/extractor/ensureImportingConcat.js.map +7 -0
  16. package/dist/cjs/extractor/evaluateAstNode.js +119 -0
  17. package/dist/cjs/extractor/evaluateAstNode.js.map +7 -0
  18. package/dist/cjs/extractor/extractHelpers.js +135 -0
  19. package/dist/cjs/extractor/extractHelpers.js.map +7 -0
  20. package/dist/cjs/extractor/extractMediaStyle.js +191 -0
  21. package/dist/cjs/extractor/extractMediaStyle.js.map +7 -0
  22. package/dist/cjs/extractor/extractToClassNames.js +333 -0
  23. package/dist/cjs/extractor/extractToClassNames.js.map +7 -0
  24. package/dist/cjs/extractor/findTopmostFunction.js +45 -0
  25. package/dist/cjs/extractor/findTopmostFunction.js.map +7 -0
  26. package/dist/cjs/extractor/generatedUid.js +53 -0
  27. package/dist/cjs/extractor/generatedUid.js.map +7 -0
  28. package/dist/cjs/extractor/getPrefixLogs.js +33 -0
  29. package/dist/cjs/extractor/getPrefixLogs.js.map +7 -0
  30. package/dist/cjs/extractor/getPropValueFromAttributes.js +74 -0
  31. package/dist/cjs/extractor/getPropValueFromAttributes.js.map +7 -0
  32. package/dist/cjs/extractor/getSourceModule.js +94 -0
  33. package/dist/cjs/extractor/getSourceModule.js.map +7 -0
  34. package/dist/cjs/extractor/getStaticBindingsForScope.js +153 -0
  35. package/dist/cjs/extractor/getStaticBindingsForScope.js.map +7 -0
  36. package/dist/cjs/extractor/hoistClassNames.js +69 -0
  37. package/dist/cjs/extractor/hoistClassNames.js.map +7 -0
  38. package/dist/cjs/extractor/literalToAst.js +119 -0
  39. package/dist/cjs/extractor/literalToAst.js.map +7 -0
  40. package/dist/cjs/extractor/loadTamagui.js +103 -0
  41. package/dist/cjs/extractor/loadTamagui.js.map +7 -0
  42. package/dist/cjs/extractor/logLines.js +44 -0
  43. package/dist/cjs/extractor/logLines.js.map +7 -0
  44. package/dist/cjs/extractor/normalizeTernaries.js +105 -0
  45. package/dist/cjs/extractor/normalizeTernaries.js.map +7 -0
  46. package/dist/cjs/extractor/removeUnusedHooks.js +104 -0
  47. package/dist/cjs/extractor/removeUnusedHooks.js.map +7 -0
  48. package/dist/cjs/extractor/timer.js +46 -0
  49. package/dist/cjs/extractor/timer.js.map +7 -0
  50. package/dist/cjs/extractor/validHTMLAttributes.js +75 -0
  51. package/dist/cjs/extractor/validHTMLAttributes.js.map +7 -0
  52. package/dist/cjs/index.js +40 -0
  53. package/dist/cjs/index.js.map +7 -0
  54. package/dist/cjs/patchReactNativeWeb.js +167 -0
  55. package/dist/cjs/patchReactNativeWeb.js.map +7 -0
  56. package/dist/cjs/types.js +16 -0
  57. package/dist/cjs/types.js.map +7 -0
  58. package/dist/{constants.js → esm/constants.js} +4 -0
  59. package/dist/{constants.js.map → esm/constants.js.map} +3 -3
  60. package/dist/{extractor → esm/extractor}/accessSafe.js +0 -0
  61. package/dist/{extractor → esm/extractor}/accessSafe.js.map +2 -2
  62. package/dist/{extractor → esm/extractor}/babelParse.js +0 -0
  63. package/dist/{extractor → esm/extractor}/babelParse.js.map +3 -3
  64. package/dist/{extractor → esm/extractor}/buildClassName.js +0 -0
  65. package/dist/{extractor → esm/extractor}/buildClassName.js.map +2 -2
  66. package/dist/{extractor → esm/extractor}/createEvaluator.js +3 -2
  67. package/dist/esm/extractor/createEvaluator.js.map +7 -0
  68. package/dist/esm/extractor/createExtractor.js +1169 -0
  69. package/dist/esm/extractor/createExtractor.js.map +7 -0
  70. package/dist/{extractor → esm/extractor}/ensureImportingConcat.js +1 -1
  71. package/dist/esm/extractor/ensureImportingConcat.js.map +7 -0
  72. package/dist/{extractor → esm/extractor}/evaluateAstNode.js +0 -0
  73. package/dist/esm/extractor/evaluateAstNode.js.map +7 -0
  74. package/dist/{extractor → esm/extractor}/extractHelpers.js +20 -11
  75. package/dist/esm/extractor/extractHelpers.js.map +7 -0
  76. package/dist/{extractor → esm/extractor}/extractMediaStyle.js +5 -4
  77. package/dist/esm/extractor/extractMediaStyle.js.map +7 -0
  78. package/dist/{extractor → esm/extractor}/extractToClassNames.js +106 -46
  79. package/dist/esm/extractor/extractToClassNames.js.map +7 -0
  80. package/dist/{extractor → esm/extractor}/findTopmostFunction.js +0 -0
  81. package/dist/{extractor → esm/extractor}/findTopmostFunction.js.map +2 -2
  82. package/dist/{extractor → esm/extractor}/generatedUid.js +0 -0
  83. package/dist/{extractor → esm/extractor}/generatedUid.js.map +2 -2
  84. package/dist/esm/extractor/getPrefixLogs.js +12 -0
  85. package/dist/esm/extractor/getPrefixLogs.js.map +7 -0
  86. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js +0 -0
  87. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js.map +2 -2
  88. package/dist/{extractor → esm/extractor}/getSourceModule.js +0 -0
  89. package/dist/{extractor → esm/extractor}/getSourceModule.js.map +2 -2
  90. package/dist/{extractor → esm/extractor}/getStaticBindingsForScope.js +0 -0
  91. package/dist/esm/extractor/getStaticBindingsForScope.js.map +7 -0
  92. package/dist/{extractor → esm/extractor}/hoistClassNames.js +0 -0
  93. package/dist/{extractor → esm/extractor}/hoistClassNames.js.map +2 -2
  94. package/dist/esm/extractor/literalToAst.js +80 -0
  95. package/dist/esm/extractor/literalToAst.js.map +7 -0
  96. package/dist/esm/extractor/loadTamagui.js +81 -0
  97. package/dist/esm/extractor/loadTamagui.js.map +7 -0
  98. package/dist/{extractor → esm/extractor}/logLines.js +8 -3
  99. package/dist/esm/extractor/logLines.js.map +7 -0
  100. package/dist/{extractor → esm/extractor}/normalizeTernaries.js +3 -2
  101. package/dist/esm/extractor/normalizeTernaries.js.map +7 -0
  102. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js +5 -3
  103. package/dist/esm/extractor/removeUnusedHooks.js.map +7 -0
  104. package/dist/esm/extractor/timer.js +24 -0
  105. package/dist/esm/extractor/timer.js.map +7 -0
  106. package/dist/esm/extractor/validHTMLAttributes.js +52 -0
  107. package/dist/esm/extractor/validHTMLAttributes.js.map +7 -0
  108. package/dist/{index.js → esm/index.js} +0 -0
  109. package/dist/{index.js.map → esm/index.js.map} +1 -1
  110. package/dist/{patchReactNativeWeb.js → esm/patchReactNativeWeb.js} +49 -14
  111. package/dist/esm/patchReactNativeWeb.js.map +7 -0
  112. package/dist/{types.js → esm/types.js} +0 -0
  113. package/dist/{types.js.map → esm/types.js.map} +0 -0
  114. package/dist/jsx/constants.js +13 -0
  115. package/dist/jsx/extractor/accessSafe.js +10 -0
  116. package/dist/jsx/extractor/babelParse.js +29 -0
  117. package/dist/jsx/extractor/buildClassName.js +39 -0
  118. package/dist/jsx/extractor/createEvaluator.js +51 -0
  119. package/dist/jsx/extractor/createExtractor.js +1168 -0
  120. package/dist/jsx/extractor/ensureImportingConcat.js +23 -0
  121. package/dist/jsx/extractor/evaluateAstNode.js +93 -0
  122. package/dist/jsx/extractor/extractHelpers.js +102 -0
  123. package/dist/jsx/extractor/extractMediaStyle.js +151 -0
  124. package/dist/jsx/extractor/extractToClassNames.js +292 -0
  125. package/dist/jsx/extractor/findTopmostFunction.js +22 -0
  126. package/dist/jsx/extractor/generatedUid.js +27 -0
  127. package/dist/jsx/extractor/getPrefixLogs.js +11 -0
  128. package/dist/jsx/extractor/getPropValueFromAttributes.js +48 -0
  129. package/dist/jsx/extractor/getSourceModule.js +68 -0
  130. package/dist/jsx/extractor/getStaticBindingsForScope.js +127 -0
  131. package/dist/jsx/extractor/hoistClassNames.js +43 -0
  132. package/dist/jsx/extractor/literalToAst.js +79 -0
  133. package/dist/jsx/extractor/loadTamagui.js +80 -0
  134. package/dist/jsx/extractor/logLines.js +21 -0
  135. package/dist/jsx/extractor/normalizeTernaries.js +52 -0
  136. package/dist/jsx/extractor/removeUnusedHooks.js +79 -0
  137. package/dist/jsx/extractor/timer.js +23 -0
  138. package/dist/jsx/extractor/validHTMLAttributes.js +51 -0
  139. package/dist/jsx/index.js +13 -0
  140. package/dist/jsx/patchReactNativeWeb.js +141 -0
  141. package/dist/jsx/types.js +0 -0
  142. package/package.json +40 -37
  143. package/types/constants.d.ts +6 -0
  144. package/types/constants.d.ts.map +1 -0
  145. package/types/extractor/accessSafe.d.ts +3 -0
  146. package/types/extractor/accessSafe.d.ts.map +1 -0
  147. package/types/extractor/babelParse.d.ts +5 -0
  148. package/types/extractor/babelParse.d.ts.map +1 -0
  149. package/types/extractor/buildClassName.d.ts +4 -0
  150. package/types/extractor/buildClassName.d.ts.map +1 -0
  151. package/types/extractor/createEvaluator.d.ts +12 -0
  152. package/types/extractor/createEvaluator.d.ts.map +1 -0
  153. package/types/extractor/createExtractor.d.ts +29 -0
  154. package/types/extractor/createExtractor.d.ts.map +1 -0
  155. package/types/extractor/ensureImportingConcat.d.ts +4 -0
  156. package/types/extractor/ensureImportingConcat.d.ts.map +1 -0
  157. package/types/extractor/evaluateAstNode.d.ts +3 -0
  158. package/types/extractor/evaluateAstNode.d.ts.map +1 -0
  159. package/types/extractor/extractHelpers.d.ts +12 -0
  160. package/types/extractor/extractHelpers.d.ts.map +1 -0
  161. package/types/extractor/extractMediaStyle.d.ts +10 -0
  162. package/types/extractor/extractMediaStyle.d.ts.map +1 -0
  163. package/types/extractor/extractToClassNames.d.ts +24 -0
  164. package/types/extractor/extractToClassNames.d.ts.map +1 -0
  165. package/types/extractor/findTopmostFunction.d.ts +4 -0
  166. package/types/extractor/findTopmostFunction.d.ts.map +1 -0
  167. package/types/extractor/generatedUid.d.ts +5 -0
  168. package/types/extractor/generatedUid.d.ts.map +1 -0
  169. package/types/extractor/getPrefixLogs.d.ts +3 -0
  170. package/types/extractor/getPrefixLogs.d.ts.map +1 -0
  171. package/types/extractor/getPropValueFromAttributes.d.ts +3 -0
  172. package/types/extractor/getPropValueFromAttributes.d.ts.map +1 -0
  173. package/types/extractor/getSourceModule.d.ts +16 -0
  174. package/types/extractor/getSourceModule.d.ts.map +1 -0
  175. package/types/extractor/getStaticBindingsForScope.d.ts +4 -0
  176. package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -0
  177. package/types/extractor/hoistClassNames.d.ts +6 -0
  178. package/types/extractor/hoistClassNames.d.ts.map +1 -0
  179. package/types/extractor/literalToAst.d.ts +4 -0
  180. package/types/extractor/literalToAst.d.ts.map +1 -0
  181. package/types/extractor/loadTamagui.d.ts +9 -0
  182. package/types/extractor/loadTamagui.d.ts.map +1 -0
  183. package/types/extractor/logLines.d.ts +2 -0
  184. package/types/extractor/logLines.d.ts.map +1 -0
  185. package/types/extractor/normalizeTernaries.d.ts +3 -0
  186. package/types/extractor/normalizeTernaries.d.ts.map +1 -0
  187. package/types/extractor/removeUnusedHooks.d.ts +3 -0
  188. package/types/extractor/removeUnusedHooks.d.ts.map +1 -0
  189. package/types/extractor/timer.d.ts +5 -0
  190. package/types/extractor/timer.d.ts.map +1 -0
  191. package/types/extractor/validHTMLAttributes.d.ts +49 -0
  192. package/types/extractor/validHTMLAttributes.d.ts.map +1 -0
  193. package/types/index.d.ts +8 -0
  194. package/types/index.d.ts.map +1 -0
  195. package/types/patchReactNativeWeb.d.ts +2 -0
  196. package/types/patchReactNativeWeb.d.ts.map +1 -0
  197. package/types/types.d.ts +85 -0
  198. package/types/types.d.ts.map +1 -0
  199. package/dist/extractor/createEvaluator.js.map +0 -7
  200. package/dist/extractor/createExtractor.js +0 -828
  201. package/dist/extractor/createExtractor.js.map +0 -7
  202. package/dist/extractor/ensureImportingConcat.js.map +0 -7
  203. package/dist/extractor/evaluateAstNode.js.map +0 -7
  204. package/dist/extractor/extractHelpers.js.map +0 -7
  205. package/dist/extractor/extractMediaStyle.js.map +0 -7
  206. package/dist/extractor/extractToClassNames.js.map +0 -7
  207. package/dist/extractor/getStaticBindingsForScope.js.map +0 -7
  208. package/dist/extractor/literalToAst.js +0 -34
  209. package/dist/extractor/literalToAst.js.map +0 -7
  210. package/dist/extractor/loadTamagui.js +0 -41
  211. package/dist/extractor/loadTamagui.js.map +0 -7
  212. package/dist/extractor/logLines.js.map +0 -7
  213. package/dist/extractor/normalizeTernaries.js.map +0 -7
  214. package/dist/extractor/removeUnusedHooks.js.map +0 -7
  215. package/dist/index.cjs +0 -2126
  216. package/dist/index.cjs.map +0 -7
  217. package/dist/patchReactNativeWeb.js.map +0 -7
  218. package/src/constants.ts +0 -10
  219. package/src/extractor/accessSafe.ts +0 -18
  220. package/src/extractor/babelParse.ts +0 -27
  221. package/src/extractor/buildClassName.ts +0 -61
  222. package/src/extractor/createEvaluator.ts +0 -68
  223. package/src/extractor/createExtractor.ts +0 -1138
  224. package/src/extractor/ensureImportingConcat.ts +0 -33
  225. package/src/extractor/evaluateAstNode.ts +0 -121
  226. package/src/extractor/extractHelpers.ts +0 -102
  227. package/src/extractor/extractMediaStyle.ts +0 -191
  228. package/src/extractor/extractToClassNames.ts +0 -323
  229. package/src/extractor/findTopmostFunction.ts +0 -22
  230. package/src/extractor/generatedUid.ts +0 -43
  231. package/src/extractor/getPropValueFromAttributes.ts +0 -92
  232. package/src/extractor/getSourceModule.ts +0 -101
  233. package/src/extractor/getStaticBindingsForScope.ts +0 -173
  234. package/src/extractor/hoistClassNames.ts +0 -45
  235. package/src/extractor/literalToAst.ts +0 -32
  236. package/src/extractor/loadTamagui.ts +0 -61
  237. package/src/extractor/logLines.ts +0 -11
  238. package/src/extractor/normalizeTernaries.ts +0 -60
  239. package/src/extractor/removeUnusedHooks.ts +0 -76
  240. package/src/index.ts +0 -9
  241. package/src/patchReactNativeWeb.ts +0 -123
  242. package/src/types.ts +0 -89
  243. package/types.d.ts +0 -219
@@ -1,173 +0,0 @@
1
- import { dirname, extname, resolve } from 'path'
2
-
3
- import { Binding, NodePath } from '@babel/traverse'
4
- import * as t from '@babel/types'
5
- import { existsSync } from 'fs-extra'
6
-
7
- import { evaluateAstNode } from './evaluateAstNode'
8
- import { getSourceModule } from './getSourceModule'
9
-
10
- const isLocalImport = (path: string) => path.startsWith('.') || path.startsWith('/')
11
-
12
- function resolveImportPath(sourcePath: string, path: string) {
13
- const sourceDir = dirname(sourcePath)
14
- if (isLocalImport(path)) {
15
- if (extname(path) === '') {
16
- path += '.js'
17
- }
18
- return resolve(sourceDir, path)
19
- }
20
- return path
21
- }
22
-
23
- function importModule(path: string) {
24
- const filenames = [path.replace('.js', '.tsx'), path.replace('.js', '.ts'), path]
25
- for (const file of filenames) {
26
- if (existsSync(file)) {
27
- // TODO we can clear this when we see updates on it later on
28
- return require(file)
29
- }
30
- }
31
- return null
32
- }
33
-
34
- export function getStaticBindingsForScope(
35
- scope: NodePath<t.JSXElement>['scope'],
36
- whitelist: string[] = [],
37
- sourcePath: string,
38
- bindingCache: Record<string, string | null>,
39
- shouldPrintDebug: boolean
40
- ): Record<string, any> {
41
- const bindings: Record<string, Binding> = scope.getAllBindings() as any
42
- const ret: Record<string, any> = {}
43
-
44
- if (shouldPrintDebug) {
45
- // prettier-ignore
46
- console.log(' ', Object.keys(bindings).length, 'variables in scope')
47
- // .map(x => bindings[x].identifier?.name).join(', ')
48
- }
49
-
50
- // on react native at least it doesnt find some bindings? not sure why
51
- // lets add in whitelisted imports if they exist
52
- const program = scope.getProgramParent().block as t.Program
53
- for (const node of program.body) {
54
- if (t.isImportDeclaration(node)) {
55
- const importPath = node.source.value
56
- if (!node.specifiers.length) continue
57
- if (!isLocalImport(importPath)) {
58
- continue
59
- }
60
- const moduleName = resolveImportPath(sourcePath, importPath)
61
- const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))
62
- if (!isOnWhitelist) continue
63
- const src = importModule(moduleName)
64
- if (!src) continue
65
- for (const specifier of node.specifiers) {
66
- if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) {
67
- if (typeof src[specifier.imported.name] !== 'undefined') {
68
- const val = src[specifier.local.name]
69
- ret[specifier.local.name] = val
70
- }
71
- }
72
- }
73
- }
74
- }
75
-
76
- if (!bindingCache) {
77
- throw new Error('bindingCache is a required param')
78
- }
79
-
80
- for (const k in bindings) {
81
- const binding = bindings[k]
82
-
83
- // check to see if the item is a module
84
- const sourceModule = getSourceModule(k, binding)
85
- if (sourceModule) {
86
- if (!sourceModule.sourceModule) {
87
- continue
88
- }
89
-
90
- const moduleName = resolveImportPath(sourcePath, sourceModule.sourceModule)
91
- const isOnWhitelist = whitelist.some((test) => moduleName.endsWith(test))
92
-
93
- // TODO we could cache this at the file level.. and check if its been touched since
94
-
95
- if (isOnWhitelist) {
96
- const src = importModule(moduleName)
97
- if (!src) {
98
- console.log(
99
- `⚠️ missing file ${moduleName} via ${sourcePath} import ${sourceModule.sourceModule}?`
100
- )
101
- return {}
102
- }
103
- if (sourceModule.destructured) {
104
- if (sourceModule.imported) {
105
- ret[k] = src[sourceModule.imported]
106
- }
107
- } else {
108
- // crude esmodule check
109
- // TODO: make sure this actually works
110
- // if (src && src.__esModule) {
111
- // ret[k] = src.default
112
- // } else {
113
- ret[k] = src
114
- // }
115
- }
116
- }
117
- continue
118
- }
119
-
120
- const { parent, parentPath } = binding.path
121
-
122
- if (!t.isVariableDeclaration(parent) || parent.kind !== 'const') {
123
- continue
124
- }
125
-
126
- // pick out the right variable declarator
127
- const dec = parent.declarations.find((d) => t.isIdentifier(d.id) && d.id.name === k)
128
-
129
- // if init is not set, there's nothing to evaluate
130
- // TODO: handle spread syntax
131
- if (!dec || !dec.init) {
132
- continue
133
- }
134
-
135
- // missing start/end will break caching
136
- if (typeof dec.id.start !== 'number' || typeof dec.id.end !== 'number') {
137
- console.error('dec.id.start/end is not a number')
138
- continue
139
- }
140
-
141
- if (!t.isIdentifier(dec.id)) {
142
- console.error('dec is not an identifier')
143
- continue
144
- }
145
-
146
- const cacheKey = `${dec.id.name}_${dec.id.start}-${dec.id.end}`
147
-
148
- // retrieve value from cache
149
- if (bindingCache.hasOwnProperty(cacheKey)) {
150
- ret[k] = bindingCache[cacheKey]
151
- continue
152
- }
153
- // retrieve value from cache
154
- if (bindingCache.hasOwnProperty(cacheKey)) {
155
- ret[k] = bindingCache[cacheKey]
156
- continue
157
- }
158
-
159
- // evaluate
160
- try {
161
- ret[k] = evaluateAstNode(dec.init, undefined, shouldPrintDebug)
162
- bindingCache[cacheKey] = ret[k]
163
- continue
164
- } catch (e) {
165
- // skip
166
- // if (shouldPrintDebug) {
167
- // console.error('[🐇] cant eval, skipping', cacheKey) //, e.message)
168
- // }
169
- }
170
- }
171
-
172
- return ret
173
- }
@@ -1,45 +0,0 @@
1
- import { NodePath } from '@babel/traverse'
2
- import * as t from '@babel/types'
3
-
4
- export function hoistClassNames(
5
- path: NodePath<t.JSXElement>,
6
- existing: { [key: string]: t.Identifier },
7
- expr: t.Expression
8
- ) {
9
- const hoist = hoistClassNames.bind(null, path, existing)
10
- if (t.isStringLiteral(expr)) {
11
- if (expr.value.trim() === '') {
12
- return expr
13
- }
14
- if (existing[expr.value]) {
15
- return existing[expr.value]
16
- }
17
- const identifier = replaceStringWithVariable(expr)
18
- existing[expr.value] = identifier
19
- return identifier
20
- }
21
- if (t.isBinaryExpression(expr)) {
22
- return t.binaryExpression(expr.operator, hoist(expr.left), hoist(expr.right))
23
- }
24
- if (t.isLogicalExpression(expr)) {
25
- return t.logicalExpression(expr.operator, hoist(expr.left), hoist(expr.right))
26
- }
27
- if (t.isConditionalExpression(expr)) {
28
- return t.conditionalExpression(expr.test, hoist(expr.consequent), hoist(expr.alternate))
29
- }
30
- return expr
31
-
32
- function replaceStringWithVariable(str: t.StringLiteral): t.Identifier {
33
- // hoist outside fn!
34
- const uid = path.scope.generateUidIdentifier('cn')
35
- const parent = path.findParent((path) => path.isProgram())
36
- if (!parent) throw new Error(`no program?`)
37
- const variable = t.variableDeclaration('const', [
38
- // adding a space for extra safety
39
- t.variableDeclarator(uid, t.stringLiteral(` ${str.value}`)),
40
- ])
41
- // @ts-ignore
42
- parent.unshiftContainer('body', variable)
43
- return uid
44
- }
45
- }
@@ -1,32 +0,0 @@
1
- import * as t from '@babel/types'
2
-
3
- export function literalToAst(literal: any): t.Expression {
4
- if (literal === null) {
5
- return t.nullLiteral()
6
- }
7
- switch (typeof literal) {
8
- case 'function':
9
- throw new Error('Unsupported')
10
- case 'number':
11
- return t.numericLiteral(literal)
12
- case 'string':
13
- return t.stringLiteral(literal)
14
- case 'boolean':
15
- return t.booleanLiteral(literal)
16
- case 'undefined':
17
- return t.unaryExpression('void', t.numericLiteral(0), true)
18
- default:
19
- if (Array.isArray(literal)) {
20
- return t.arrayExpression(literal.map(literalToAst))
21
- }
22
- return t.objectExpression(
23
- Object.keys(literal)
24
- .filter((k) => {
25
- return typeof literal[k] !== 'undefined'
26
- })
27
- .map((k) => {
28
- return t.objectProperty(t.stringLiteral(k), literalToAst(literal[k]))
29
- })
30
- )
31
- }
32
- }
@@ -1,61 +0,0 @@
1
- import { join } from 'path'
2
-
3
- import type { StaticComponent, TamaguiInternalConfig } from '@tamagui/core'
4
- import { createTamagui } from '@tamagui/core-node'
5
-
6
- let loadedTamagui: any = null
7
-
8
- export function loadTamagui(props: { components: string[]; config: string }): {
9
- components: Record<string, StaticComponent>
10
- tamaguiConfig: TamaguiInternalConfig
11
- } {
12
- if (loadedTamagui) {
13
- return loadedTamagui
14
- }
15
-
16
- // lets shim require and avoid importing react-native + react-native-web
17
- // we just need to read the config around them
18
- process.env.IS_STATIC = 'is_static'
19
- const proxyWorm = require('@tamagui/fake-react-native')
20
- const Mod = require('module')
21
- const og = Mod.prototype.require
22
- Mod.prototype.require = function (path: string) {
23
- if (
24
- path.startsWith('react-native') &&
25
- // allow our rnw.tsx imports through
26
- !path.startsWith('react-native-web/dist/cjs/exports')
27
- ) {
28
- return proxyWorm
29
- }
30
- return og.apply(this, arguments)
31
- }
32
-
33
- // import config
34
- const configPath = join(process.cwd(), props.config)
35
- const tamaguiConfigExport = require(configPath)
36
- // TODO validate its the right config
37
- const tamaguiConfig = (tamaguiConfigExport['default'] ||
38
- tamaguiConfigExport) as TamaguiInternalConfig
39
-
40
- // import components
41
- const components = {}
42
- for (const module of props.components) {
43
- const exported = require(module)
44
- // TODO check if overwriting and warn
45
- Object.assign(components, exported)
46
- }
47
-
48
- // undo shims
49
- process.env.IS_STATIC = undefined
50
- Mod.prototype.require = og
51
-
52
- // set up core-node
53
- createTamagui(tamaguiConfig)
54
-
55
- loadedTamagui = {
56
- components,
57
- tamaguiConfig,
58
- }
59
-
60
- return loadedTamagui
61
- }
@@ -1,11 +0,0 @@
1
- export const logLines = (str: string) => {
2
- let lines: string[] = ['']
3
- const items = str.split(' ')
4
- for (const item of items) {
5
- if (item.length + lines[lines.length - 1].length > 100) {
6
- lines.push('')
7
- }
8
- lines[lines.length - 1] += item + ' '
9
- }
10
- return lines.map((line, i) => ' ' + (i == 0 ? '' : ' ') + line.trim()).join('\n')
11
- }
@@ -1,60 +0,0 @@
1
- import generate from '@babel/generator'
2
- import * as t from '@babel/types'
3
- import invariant from 'invariant'
4
-
5
- import { Ternary } from '../types'
6
-
7
- export function normalizeTernaries(ternaries: Ternary[]) {
8
- invariant(Array.isArray(ternaries), 'extractStaticTernaries expects param 1 to be an array of ternaries')
9
-
10
- if (ternaries.length === 0) {
11
- return []
12
- }
13
-
14
- const ternariesByKey: { [key: string]: Ternary } = {}
15
-
16
- for (let idx = -1, len = ternaries.length; ++idx < len; ) {
17
- const { test, consequent, alternate, remove, ...rest } = ternaries[idx]
18
-
19
- let ternaryTest = test
20
-
21
- // strip parens
22
- if (t.isExpressionStatement(test)) {
23
- ternaryTest = (test as any).expression
24
- }
25
-
26
- // convert `!thing` to `thing` with swapped consequent and alternate
27
- let shouldSwap = false
28
- if (t.isUnaryExpression(test) && test.operator === '!') {
29
- ternaryTest = test.argument
30
- shouldSwap = true
31
- } else if (t.isBinaryExpression(test)) {
32
- if (test.operator === '!==' || test.operator === '!=') {
33
- ternaryTest = t.binaryExpression(test.operator, test.left, test.right)
34
- shouldSwap = true
35
- }
36
- }
37
-
38
- const key = generate(ternaryTest).code
39
-
40
- if (!ternariesByKey[key]) {
41
- ternariesByKey[key] = {
42
- ...rest,
43
- alternate: {},
44
- consequent: {},
45
- test: ternaryTest,
46
- remove,
47
- }
48
- }
49
- const altStyle = (shouldSwap ? consequent : alternate) ?? {}
50
- const consStyle = (shouldSwap ? alternate : consequent) ?? {}
51
- Object.assign(ternariesByKey[key].alternate, altStyle)
52
- Object.assign(ternariesByKey[key].consequent, consStyle)
53
- }
54
-
55
- const ternaryExpression = Object.keys(ternariesByKey).map((key) => {
56
- return ternariesByKey[key]
57
- })
58
-
59
- return ternaryExpression
60
- }
@@ -1,76 +0,0 @@
1
- import { NodePath } from '@babel/traverse'
2
- import * as t from '@babel/types'
3
-
4
- const hooks = {
5
- useMedia: true,
6
- useTheme: true,
7
- }
8
-
9
- export function removeUnusedHooks(compFn: NodePath<any>, shouldPrintDebug: boolean) {
10
- compFn.scope.crawl()
11
- // check the top level statements
12
- let bodyStatements = compFn?.get('body')
13
- if (!bodyStatements) {
14
- console.log('no body statemnts?', compFn)
15
- return
16
- }
17
- if (!Array.isArray(bodyStatements)) {
18
- if (bodyStatements.isFunctionExpression()) {
19
- bodyStatements = bodyStatements.scope.path.get('body')
20
- } else {
21
- bodyStatements = bodyStatements.get('body')
22
- }
23
- }
24
- if (!bodyStatements || !Array.isArray(bodyStatements)) {
25
- return
26
- }
27
- const statements = bodyStatements as NodePath<any>[]
28
- for (const statement of statements) {
29
- if (!statement.isVariableDeclaration()) {
30
- continue
31
- }
32
- const declarations = statement.get('declarations')
33
- if (!Array.isArray(declarations)) {
34
- continue
35
- }
36
- const isBindingReferenced = (name: string) => {
37
- return !!statement.scope.getBinding(name)?.referenced
38
- }
39
- for (const declarator of declarations) {
40
- const id = declarator.get('id')
41
- const init = declarator.node.init
42
- if (Array.isArray(id) || Array.isArray(init)) {
43
- continue
44
- }
45
- const shouldRemove = (() => {
46
- const isHook =
47
- t.isCallExpression(init) && t.isIdentifier(init.callee) && hooks[init.callee.name]
48
- if (!isHook) {
49
- return false
50
- }
51
- if (t.isIdentifier(id.node)) {
52
- // remove "const media = useMedia()"
53
- const name = id.node.name
54
- return !isBindingReferenced(name)
55
- } else if (t.isObjectPattern(id.node)) {
56
- // remove "const { sm } = useMedia()"
57
- const propPaths = id.get('properties') as NodePath<any>[]
58
- return propPaths.every((prop) => {
59
- if (!prop.isObjectProperty()) return false
60
- const value = prop.get('value')
61
- if (Array.isArray(value) || !value.isIdentifier()) return false
62
- const name = value.node.name
63
- return !isBindingReferenced(name)
64
- })
65
- }
66
- return false
67
- })()
68
- if (shouldRemove) {
69
- declarator.remove()
70
- if (shouldPrintDebug) {
71
- console.log(` [🪝] removed ${id.node['name'] ?? ''}`)
72
- }
73
- }
74
- }
75
- }
76
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- process.env.TAMAGUI_COMPILE_PROCESS = '1'
2
-
3
- export { TamaguiOptions } from './types'
4
- export { createExtractor } from './extractor/createExtractor'
5
- export { literalToAst } from './extractor/literalToAst'
6
- export * from './constants'
7
- export * from './extractor/extractToClassNames'
8
- export * from './extractor/extractHelpers'
9
- export * from './patchReactNativeWeb'
@@ -1,123 +0,0 @@
1
- import * as fs from 'fs'
2
- import path from 'path'
3
-
4
- // were patching react-native-web so we can use some internal methods
5
- // we do it this way because we need to rely on webpack or bundler config to determine cjs vs esm
6
- // so we can't just require it all directly
7
- // would be nice in the future to be able to eject from react-native-web entirely optionally
8
-
9
- // keep it sync
10
- export function patchReactNativeWeb() {
11
- const rootDir = require.resolve('react-native-web').replace(/\/dist.*/, '')
12
- const modulePath = path.join(rootDir, 'dist', 'tamagui-exports.js')
13
- const cjsPath = path.join(rootDir, 'dist', 'cjs', 'tamagui-exports.js')
14
-
15
- const isEqual = (() => {
16
- let res = false
17
- try {
18
- res =
19
- fs.readFileSync(modulePath, 'utf-8') === moduleExports &&
20
- fs.readFileSync(cjsPath, 'utf-8') == cjsExports
21
- } catch {
22
- res = false
23
- }
24
- return res
25
- })()
26
-
27
- if (!isEqual) {
28
- console.log('🥚 Tamagui patching react-native-web to share atomic styling primitives')
29
- console.log(' > adding', modulePath)
30
- console.log(' > adding', cjsPath)
31
- fs.writeFileSync(modulePath, moduleExports)
32
- fs.writeFileSync(cjsPath, cjsExports)
33
- }
34
-
35
- // if entry files not patched, patch them:
36
- const moduleEntry = path.join(rootDir, 'dist', 'index.js')
37
- const moduleEntrySrc = fs.readFileSync(moduleEntry, 'utf-8')
38
- if (!moduleEntrySrc.includes('// tamagui-patch-v4')) {
39
- fs.writeFileSync(
40
- moduleEntry,
41
- `${removePatch(moduleEntrySrc)}
42
-
43
- // tamagui-patch-v4
44
- import * as TExports from './tamagui-exports'
45
- export const TamaguiExports = TExports
46
- // tamagui-patch-end
47
- `
48
- )
49
- }
50
- const cjsEntry = path.join(rootDir, 'dist', 'cjs', 'index.js')
51
- const cjsEntrySrc = fs.readFileSync(cjsEntry, 'utf-8')
52
- if (!cjsEntrySrc.includes('// tamagui-patch-v4')) {
53
- fs.writeFileSync(
54
- cjsEntry,
55
- `${removePatch(cjsEntrySrc)}
56
-
57
- // tamagui-patch-v4
58
- exports.TamaguiExports = _interopRequireDefault(require("./tamagui-exports"));
59
- // tamagui-patch-end
60
- `
61
- )
62
- }
63
- }
64
-
65
- function removePatch(source: string) {
66
- return source.replace(/\/\/ tamagui-patch([.\s\S]*)/g, '')
67
- }
68
-
69
- // view exports/View.ts
70
- const forwardedPropsObj = `{
71
- ...fwdProps.defaultProps,
72
- ...fwdProps.accessibilityProps,
73
- ...fwdProps.clickProps,
74
- ...fwdProps.focusProps,
75
- ...fwdProps.keyboardProps,
76
- ...fwdProps.mouseProps,
77
- ...fwdProps.touchProps,
78
- ...fwdProps.styleProps,
79
- href: true,
80
- lang: true,
81
- onScroll: true,
82
- onWheel: true,
83
- pointerEvents: true
84
- }
85
- `
86
-
87
- const moduleExports = `
88
- export { atomic } from './exports/StyleSheet/compile'
89
- export { default as createCompileableStyle } from './exports/StyleSheet/createCompileableStyle'
90
- export { default as createReactDOMStyle } from './exports/StyleSheet/createReactDOMStyle'
91
- export { default as i18Style } from './exports/StyleSheet/i18nStyle'
92
- export { default as createDOMProps } from './modules/createDOMProps'
93
- export { default as AccessibilityUtil } from './modules/AccessibilityUtil'
94
- export { default as createElement } from './exports/createElement'
95
- export { default as css } from './exports/StyleSheet/css'
96
- export { default as TextAncestorContext } from './exports/Text/TextAncestorContext'
97
- export { default as pick } from './modules/pick'
98
- export { default as useElementLayout } from './modules/useElementLayout'
99
- export { default as useMergeRefs } from './modules/useMergeRefs'
100
- export { default as usePlatformMethods } from './modules/usePlatformMethods'
101
- export { default as useResponderEvents } from './modules/useResponderEvents'
102
- import * as fwdProps from './modules/forwardedProps'
103
- export const forwardedProps = ${forwardedPropsObj}
104
- `
105
-
106
- const cjsExports = `
107
- exports.atomic = require('./exports/StyleSheet/compile').atomic
108
- exports.createCompileableStyle = require('./exports/StyleSheet/createCompileableStyle')
109
- exports.createReactDOMStyle = require('./exports/StyleSheet/createReactDOMStyle')
110
- exports.i18Style = require('./exports/StyleSheet/i18nStyle')
111
- exports.createDOMProps = require('./modules/createDOMProps')
112
- exports.AccessibilityUtil = require('./modules/AccessibilityUtil')
113
- exports.createElement = require('./exports/createElement')
114
- exports.css = require('./exports/StyleSheet/css')
115
- exports.TextAncestorContext = require('./exports/Text/TextAncestorContext')
116
- exports.pick = require('./modules/pick')
117
- exports.useElementLayout = require('./modules/useElementLayout')
118
- exports.useMergeRefs = require('./modules/useMergeRefs')
119
- exports.usePlatformMethods = require('./modules/usePlatformMethods')
120
- exports.useResponderEvents = require('./modules/useResponderEvents')
121
- const fwdProps = require('./modules/forwardedProps')
122
- exports.forwardedProps = ${forwardedPropsObj}
123
- `
package/src/types.ts DELETED
@@ -1,89 +0,0 @@
1
- import { NodePath } from '@babel/traverse'
2
- import * as t from '@babel/types'
3
-
4
- export type ClassNameObject = t.StringLiteral | t.Expression
5
-
6
- export interface CacheObject {
7
- [key: string]: any
8
- }
9
-
10
- export interface TamaguiOptions {
11
- // module paths you want to compile with tamagui (for example ['tamagui'])
12
- components: string[]
13
- // your tamagui.config.ts
14
- config?: string
15
- evaluateVars?: boolean
16
- importsWhitelist?: string[]
17
- disableExtraction?: boolean
18
- disableDebugAttr?: boolean
19
- disableExtractInlineMedia?: boolean
20
- exclude?: RegExp
21
- logTimings?: boolean
22
-
23
- // probably non user options
24
- cssPath?: string
25
- cssData?: any
26
- deoptProps?: Set<string>
27
- excludeProps?: string[]
28
- }
29
-
30
- export type ExtractedAttrAttr = {
31
- type: 'attr'
32
- value: t.JSXAttribute | t.JSXSpreadAttribute
33
- }
34
-
35
- export type ExtractedAttrStyle = {
36
- type: 'style'
37
- value: Object
38
- attr?: t.JSXAttribute | t.JSXSpreadAttribute
39
- name?: string
40
- }
41
-
42
- export type ExtractedAttr =
43
- | ExtractedAttrAttr
44
- | { type: 'ternary'; value: Ternary }
45
- | ExtractedAttrStyle
46
-
47
- export type ExtractTagProps = {
48
- attrs: ExtractedAttr[]
49
- node: t.JSXOpeningElement
50
- attemptEval: (exprNode: t.Node, evalFn?: ((node: t.Node) => any) | undefined) => any
51
- jsxPath: NodePath<t.JSXElement>
52
- programPath: NodePath<t.Program>
53
- originalNodeName: string
54
- lineNumbers: string
55
- filePath: string
56
- isFlattened: boolean
57
- }
58
-
59
- export type ExtractorParseProps = TamaguiOptions & {
60
- sourcePath?: string
61
- shouldPrintDebug?: boolean
62
- onExtractTag: (props: ExtractTagProps) => void
63
- getFlattenedNode: (props: { isTextView: boolean; tag: string }) => string
64
- }
65
-
66
- export interface Ternary {
67
- test: t.Expression
68
- // shorthand props that don't use hooks
69
- inlineMediaQuery?: string
70
- remove: Function
71
- consequent: Object | null
72
- alternate: Object | null
73
- }
74
-
75
- export type StyleObject = {
76
- property: string
77
- value: string
78
- className: string
79
- identifier: string
80
- rules: string[]
81
- }
82
-
83
- export type ClassNameToStyleObj = {
84
- [key: string]: StyleObject
85
- }
86
-
87
- export interface PluginContext {
88
- write: (path: string, rules: { [key: string]: string }) => any
89
- }