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

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 (233) hide show
  1. package/README.md +3 -19
  2. package/dist/cjs/constants.js +36 -0
  3. package/dist/cjs/constants.js.map +7 -0
  4. package/dist/cjs/extractor/accessSafe.js +32 -0
  5. package/dist/cjs/extractor/accessSafe.js.map +7 -0
  6. package/dist/cjs/extractor/babelParse.js +51 -0
  7. package/dist/cjs/extractor/babelParse.js.map +7 -0
  8. package/dist/cjs/extractor/buildClassName.js +61 -0
  9. package/dist/cjs/extractor/buildClassName.js.map +7 -0
  10. package/dist/cjs/extractor/createEvaluator.js +73 -0
  11. package/dist/cjs/extractor/createEvaluator.js.map +7 -0
  12. package/dist/cjs/extractor/createExtractor.js +1100 -0
  13. package/dist/cjs/extractor/createExtractor.js.map +7 -0
  14. package/dist/cjs/extractor/ensureImportingConcat.js +45 -0
  15. package/dist/cjs/extractor/ensureImportingConcat.js.map +7 -0
  16. package/dist/cjs/extractor/evaluateAstNode.js +115 -0
  17. package/dist/cjs/extractor/evaluateAstNode.js.map +7 -0
  18. package/dist/cjs/extractor/extractHelpers.js +124 -0
  19. package/dist/cjs/extractor/extractHelpers.js.map +7 -0
  20. package/dist/cjs/extractor/extractMediaStyle.js +186 -0
  21. package/dist/cjs/extractor/extractMediaStyle.js.map +7 -0
  22. package/dist/cjs/extractor/extractToClassNames.js +324 -0
  23. package/dist/cjs/extractor/extractToClassNames.js.map +7 -0
  24. package/dist/cjs/extractor/findTopmostFunction.js +41 -0
  25. package/dist/cjs/extractor/findTopmostFunction.js.map +7 -0
  26. package/dist/cjs/extractor/generatedUid.js +49 -0
  27. package/dist/cjs/extractor/generatedUid.js.map +7 -0
  28. package/dist/cjs/extractor/getPrefixLogs.js +29 -0
  29. package/dist/cjs/extractor/getPrefixLogs.js.map +7 -0
  30. package/dist/cjs/extractor/getPropValueFromAttributes.js +70 -0
  31. package/dist/cjs/extractor/getPropValueFromAttributes.js.map +7 -0
  32. package/dist/cjs/extractor/getSourceModule.js +90 -0
  33. package/dist/cjs/extractor/getSourceModule.js.map +7 -0
  34. package/dist/cjs/extractor/getStaticBindingsForScope.js +149 -0
  35. package/dist/cjs/extractor/getStaticBindingsForScope.js.map +7 -0
  36. package/dist/cjs/extractor/hoistClassNames.js +65 -0
  37. package/dist/cjs/extractor/hoistClassNames.js.map +7 -0
  38. package/dist/cjs/extractor/literalToAst.js +114 -0
  39. package/dist/cjs/extractor/literalToAst.js.map +7 -0
  40. package/dist/cjs/extractor/loadTamagui.js +83 -0
  41. package/dist/cjs/extractor/loadTamagui.js.map +7 -0
  42. package/dist/cjs/extractor/logLines.js +40 -0
  43. package/dist/cjs/extractor/logLines.js.map +7 -0
  44. package/dist/cjs/extractor/normalizeTernaries.js +101 -0
  45. package/dist/cjs/extractor/normalizeTernaries.js.map +7 -0
  46. package/dist/cjs/extractor/removeUnusedHooks.js +100 -0
  47. package/dist/cjs/extractor/removeUnusedHooks.js.map +7 -0
  48. package/dist/cjs/extractor/validHTMLAttributes.js +71 -0
  49. package/dist/cjs/extractor/validHTMLAttributes.js.map +7 -0
  50. package/dist/cjs/index.js +34 -0
  51. package/dist/cjs/index.js.map +7 -0
  52. package/dist/cjs/patchReactNativeWeb.js +151 -0
  53. package/dist/cjs/patchReactNativeWeb.js.map +7 -0
  54. package/dist/cjs/types.js +16 -0
  55. package/dist/cjs/types.js.map +7 -0
  56. package/dist/{constants.js → esm/constants.js} +4 -0
  57. package/dist/{constants.js.map → esm/constants.js.map} +3 -3
  58. package/dist/{extractor → esm/extractor}/accessSafe.js +0 -0
  59. package/dist/{extractor → esm/extractor}/accessSafe.js.map +2 -2
  60. package/dist/{extractor → esm/extractor}/babelParse.js +0 -0
  61. package/dist/{extractor → esm/extractor}/babelParse.js.map +2 -2
  62. package/dist/{extractor → esm/extractor}/buildClassName.js +0 -0
  63. package/dist/{extractor → esm/extractor}/buildClassName.js.map +2 -2
  64. package/dist/{extractor → esm/extractor}/createEvaluator.js +3 -2
  65. package/dist/esm/extractor/createEvaluator.js.map +7 -0
  66. package/dist/{extractor → esm/extractor}/createExtractor.js +412 -189
  67. package/dist/esm/extractor/createExtractor.js.map +7 -0
  68. package/dist/{extractor → esm/extractor}/ensureImportingConcat.js +1 -1
  69. package/dist/esm/extractor/ensureImportingConcat.js.map +7 -0
  70. package/dist/{extractor → esm/extractor}/evaluateAstNode.js +0 -0
  71. package/dist/esm/extractor/evaluateAstNode.js.map +7 -0
  72. package/dist/{extractor → esm/extractor}/extractHelpers.js +20 -11
  73. package/dist/esm/extractor/extractHelpers.js.map +7 -0
  74. package/dist/{extractor → esm/extractor}/extractMediaStyle.js +3 -2
  75. package/dist/esm/extractor/extractMediaStyle.js.map +7 -0
  76. package/dist/{extractor → esm/extractor}/extractToClassNames.js +99 -44
  77. package/dist/esm/extractor/extractToClassNames.js.map +7 -0
  78. package/dist/{extractor → esm/extractor}/findTopmostFunction.js +0 -0
  79. package/dist/{extractor → esm/extractor}/findTopmostFunction.js.map +2 -2
  80. package/dist/{extractor → esm/extractor}/generatedUid.js +0 -0
  81. package/dist/{extractor → esm/extractor}/generatedUid.js.map +2 -2
  82. package/dist/esm/extractor/getPrefixLogs.js +12 -0
  83. package/dist/esm/extractor/getPrefixLogs.js.map +7 -0
  84. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js +0 -0
  85. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js.map +2 -2
  86. package/dist/{extractor → esm/extractor}/getSourceModule.js +0 -0
  87. package/dist/{extractor → esm/extractor}/getSourceModule.js.map +2 -2
  88. package/dist/{extractor → esm/extractor}/getStaticBindingsForScope.js +0 -0
  89. package/dist/esm/extractor/getStaticBindingsForScope.js.map +7 -0
  90. package/dist/{extractor → esm/extractor}/hoistClassNames.js +0 -0
  91. package/dist/{extractor → esm/extractor}/hoistClassNames.js.map +2 -2
  92. package/dist/esm/extractor/literalToAst.js +80 -0
  93. package/dist/esm/extractor/literalToAst.js.map +7 -0
  94. package/dist/{extractor → esm/extractor}/loadTamagui.js +27 -3
  95. package/dist/esm/extractor/loadTamagui.js.map +7 -0
  96. package/dist/{extractor → esm/extractor}/logLines.js +8 -3
  97. package/dist/esm/extractor/logLines.js.map +7 -0
  98. package/dist/{extractor → esm/extractor}/normalizeTernaries.js +3 -2
  99. package/dist/esm/extractor/normalizeTernaries.js.map +7 -0
  100. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js +5 -3
  101. package/dist/esm/extractor/removeUnusedHooks.js.map +7 -0
  102. package/dist/esm/extractor/validHTMLAttributes.js +52 -0
  103. package/dist/esm/extractor/validHTMLAttributes.js.map +7 -0
  104. package/dist/{index.js → esm/index.js} +0 -0
  105. package/dist/{index.js.map → esm/index.js.map} +1 -1
  106. package/dist/{patchReactNativeWeb.js → esm/patchReactNativeWeb.js} +37 -14
  107. package/dist/esm/patchReactNativeWeb.js.map +7 -0
  108. package/dist/{types.js → esm/types.js} +0 -0
  109. package/dist/{types.js.map → esm/types.js.map} +0 -0
  110. package/dist/jsx/constants.js +13 -0
  111. package/dist/jsx/extractor/accessSafe.js +10 -0
  112. package/dist/jsx/extractor/babelParse.js +29 -0
  113. package/dist/jsx/extractor/buildClassName.js +39 -0
  114. package/dist/jsx/extractor/createEvaluator.js +51 -0
  115. package/dist/jsx/extractor/createExtractor.js +1050 -0
  116. package/dist/jsx/extractor/ensureImportingConcat.js +23 -0
  117. package/dist/jsx/extractor/evaluateAstNode.js +93 -0
  118. package/dist/jsx/extractor/extractHelpers.js +102 -0
  119. package/dist/jsx/extractor/extractMediaStyle.js +151 -0
  120. package/dist/jsx/extractor/extractToClassNames.js +287 -0
  121. package/dist/jsx/extractor/findTopmostFunction.js +22 -0
  122. package/dist/jsx/extractor/generatedUid.js +27 -0
  123. package/dist/jsx/extractor/getPrefixLogs.js +11 -0
  124. package/dist/jsx/extractor/getPropValueFromAttributes.js +48 -0
  125. package/dist/jsx/extractor/getSourceModule.js +68 -0
  126. package/dist/jsx/extractor/getStaticBindingsForScope.js +127 -0
  127. package/dist/jsx/extractor/hoistClassNames.js +43 -0
  128. package/dist/jsx/extractor/literalToAst.js +79 -0
  129. package/dist/jsx/extractor/loadTamagui.js +64 -0
  130. package/dist/jsx/extractor/logLines.js +21 -0
  131. package/dist/jsx/extractor/normalizeTernaries.js +52 -0
  132. package/dist/jsx/extractor/removeUnusedHooks.js +79 -0
  133. package/dist/jsx/extractor/validHTMLAttributes.js +51 -0
  134. package/dist/jsx/index.js +13 -0
  135. package/{src/patchReactNativeWeb.ts → dist/jsx/patchReactNativeWeb.js} +65 -59
  136. package/dist/jsx/types.js +0 -0
  137. package/package.json +26 -23
  138. package/types/constants.d.ts +6 -0
  139. package/types/constants.d.ts.map +1 -0
  140. package/types/extractor/accessSafe.d.ts +3 -0
  141. package/types/extractor/accessSafe.d.ts.map +1 -0
  142. package/types/extractor/babelParse.d.ts +5 -0
  143. package/types/extractor/babelParse.d.ts.map +1 -0
  144. package/types/extractor/buildClassName.d.ts +4 -0
  145. package/types/extractor/buildClassName.d.ts.map +1 -0
  146. package/types/extractor/createEvaluator.d.ts +12 -0
  147. package/types/extractor/createEvaluator.d.ts.map +1 -0
  148. package/types/extractor/createExtractor.d.ts +27 -0
  149. package/types/extractor/createExtractor.d.ts.map +1 -0
  150. package/types/extractor/ensureImportingConcat.d.ts +4 -0
  151. package/types/extractor/ensureImportingConcat.d.ts.map +1 -0
  152. package/types/extractor/evaluateAstNode.d.ts +3 -0
  153. package/types/extractor/evaluateAstNode.d.ts.map +1 -0
  154. package/types/extractor/extractHelpers.d.ts +12 -0
  155. package/types/extractor/extractHelpers.d.ts.map +1 -0
  156. package/types/extractor/extractMediaStyle.d.ts +10 -0
  157. package/types/extractor/extractMediaStyle.d.ts.map +1 -0
  158. package/types/extractor/extractToClassNames.d.ts +23 -0
  159. package/types/extractor/extractToClassNames.d.ts.map +1 -0
  160. package/types/extractor/findTopmostFunction.d.ts +4 -0
  161. package/types/extractor/findTopmostFunction.d.ts.map +1 -0
  162. package/types/extractor/generatedUid.d.ts +5 -0
  163. package/types/extractor/generatedUid.d.ts.map +1 -0
  164. package/types/extractor/getPrefixLogs.d.ts +3 -0
  165. package/types/extractor/getPrefixLogs.d.ts.map +1 -0
  166. package/types/extractor/getPropValueFromAttributes.d.ts +3 -0
  167. package/types/extractor/getPropValueFromAttributes.d.ts.map +1 -0
  168. package/types/extractor/getSourceModule.d.ts +16 -0
  169. package/types/extractor/getSourceModule.d.ts.map +1 -0
  170. package/types/extractor/getStaticBindingsForScope.d.ts +4 -0
  171. package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -0
  172. package/types/extractor/hoistClassNames.d.ts +6 -0
  173. package/types/extractor/hoistClassNames.d.ts.map +1 -0
  174. package/types/extractor/literalToAst.d.ts +4 -0
  175. package/types/extractor/literalToAst.d.ts.map +1 -0
  176. package/types/extractor/loadTamagui.d.ts +9 -0
  177. package/types/extractor/loadTamagui.d.ts.map +1 -0
  178. package/types/extractor/logLines.d.ts +2 -0
  179. package/types/extractor/logLines.d.ts.map +1 -0
  180. package/types/extractor/normalizeTernaries.d.ts +3 -0
  181. package/types/extractor/normalizeTernaries.d.ts.map +1 -0
  182. package/types/extractor/removeUnusedHooks.d.ts +3 -0
  183. package/types/extractor/removeUnusedHooks.d.ts.map +1 -0
  184. package/types/extractor/validHTMLAttributes.d.ts +49 -0
  185. package/types/extractor/validHTMLAttributes.d.ts.map +1 -0
  186. package/types/index.d.ts +8 -0
  187. package/types/index.d.ts.map +1 -0
  188. package/types/patchReactNativeWeb.d.ts +2 -0
  189. package/types/patchReactNativeWeb.d.ts.map +1 -0
  190. package/types/types.d.ts +85 -0
  191. package/types/types.d.ts.map +1 -0
  192. package/dist/extractor/createEvaluator.js.map +0 -7
  193. package/dist/extractor/createExtractor.js.map +0 -7
  194. package/dist/extractor/ensureImportingConcat.js.map +0 -7
  195. package/dist/extractor/evaluateAstNode.js.map +0 -7
  196. package/dist/extractor/extractHelpers.js.map +0 -7
  197. package/dist/extractor/extractMediaStyle.js.map +0 -7
  198. package/dist/extractor/extractToClassNames.js.map +0 -7
  199. package/dist/extractor/getStaticBindingsForScope.js.map +0 -7
  200. package/dist/extractor/literalToAst.js +0 -34
  201. package/dist/extractor/literalToAst.js.map +0 -7
  202. package/dist/extractor/loadTamagui.js.map +0 -7
  203. package/dist/extractor/logLines.js.map +0 -7
  204. package/dist/extractor/normalizeTernaries.js.map +0 -7
  205. package/dist/extractor/removeUnusedHooks.js.map +0 -7
  206. package/dist/index.cjs +0 -2126
  207. package/dist/index.cjs.map +0 -7
  208. package/dist/patchReactNativeWeb.js.map +0 -7
  209. package/src/constants.ts +0 -10
  210. package/src/extractor/accessSafe.ts +0 -18
  211. package/src/extractor/babelParse.ts +0 -27
  212. package/src/extractor/buildClassName.ts +0 -61
  213. package/src/extractor/createEvaluator.ts +0 -68
  214. package/src/extractor/createExtractor.ts +0 -1138
  215. package/src/extractor/ensureImportingConcat.ts +0 -33
  216. package/src/extractor/evaluateAstNode.ts +0 -121
  217. package/src/extractor/extractHelpers.ts +0 -102
  218. package/src/extractor/extractMediaStyle.ts +0 -191
  219. package/src/extractor/extractToClassNames.ts +0 -323
  220. package/src/extractor/findTopmostFunction.ts +0 -22
  221. package/src/extractor/generatedUid.ts +0 -43
  222. package/src/extractor/getPropValueFromAttributes.ts +0 -92
  223. package/src/extractor/getSourceModule.ts +0 -101
  224. package/src/extractor/getStaticBindingsForScope.ts +0 -173
  225. package/src/extractor/hoistClassNames.ts +0 -45
  226. package/src/extractor/literalToAst.ts +0 -32
  227. package/src/extractor/loadTamagui.ts +0 -61
  228. package/src/extractor/logLines.ts +0 -11
  229. package/src/extractor/normalizeTernaries.ts +0 -60
  230. package/src/extractor/removeUnusedHooks.ts +0 -76
  231. package/src/index.ts +0 -9
  232. package/src/types.ts +0 -89
  233. 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'
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
- }