@tamagui/static 1.0.0-alpha.5 → 1.0.0-alpha.53

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 (209) hide show
  1. package/dist/cjs/constants.js +43 -0
  2. package/dist/cjs/constants.js.map +7 -0
  3. package/dist/cjs/extractor/accessSafe.js +39 -0
  4. package/dist/cjs/extractor/accessSafe.js.map +7 -0
  5. package/dist/cjs/extractor/babelParse.js +58 -0
  6. package/dist/cjs/extractor/babelParse.js.map +7 -0
  7. package/dist/cjs/extractor/buildClassName.js +68 -0
  8. package/dist/cjs/extractor/buildClassName.js.map +7 -0
  9. package/dist/cjs/extractor/createEvaluator.js +79 -0
  10. package/dist/cjs/extractor/createEvaluator.js.map +7 -0
  11. package/dist/cjs/extractor/createExtractor.js +919 -0
  12. package/dist/cjs/extractor/createExtractor.js.map +7 -0
  13. package/dist/cjs/extractor/ensureImportingConcat.js +52 -0
  14. package/dist/cjs/extractor/ensureImportingConcat.js.map +7 -0
  15. package/dist/cjs/extractor/evaluateAstNode.js +122 -0
  16. package/dist/cjs/extractor/evaluateAstNode.js.map +7 -0
  17. package/dist/cjs/extractor/extractHelpers.js +124 -0
  18. package/dist/cjs/extractor/extractHelpers.js.map +7 -0
  19. package/dist/cjs/extractor/extractMediaStyle.js +193 -0
  20. package/dist/cjs/extractor/extractMediaStyle.js.map +7 -0
  21. package/dist/cjs/extractor/extractToClassNames.js +289 -0
  22. package/dist/cjs/extractor/extractToClassNames.js.map +7 -0
  23. package/dist/cjs/extractor/findTopmostFunction.js +46 -0
  24. package/dist/cjs/extractor/findTopmostFunction.js.map +7 -0
  25. package/dist/cjs/extractor/generatedUid.js +56 -0
  26. package/dist/cjs/extractor/generatedUid.js.map +7 -0
  27. package/dist/cjs/extractor/getPropValueFromAttributes.js +77 -0
  28. package/dist/cjs/extractor/getPropValueFromAttributes.js.map +7 -0
  29. package/dist/cjs/extractor/getSourceModule.js +97 -0
  30. package/dist/cjs/extractor/getSourceModule.js.map +7 -0
  31. package/dist/cjs/extractor/getStaticBindingsForScope.js +156 -0
  32. package/dist/cjs/extractor/getStaticBindingsForScope.js.map +7 -0
  33. package/dist/cjs/extractor/hoistClassNames.js +72 -0
  34. package/dist/cjs/extractor/hoistClassNames.js.map +7 -0
  35. package/dist/cjs/extractor/literalToAst.js +62 -0
  36. package/dist/cjs/extractor/literalToAst.js.map +7 -0
  37. package/dist/cjs/extractor/loadTamagui.js +67 -0
  38. package/dist/cjs/extractor/loadTamagui.js.map +7 -0
  39. package/dist/cjs/extractor/logLines.js +40 -0
  40. package/dist/cjs/extractor/logLines.js.map +7 -0
  41. package/dist/cjs/extractor/normalizeTernaries.js +108 -0
  42. package/dist/cjs/extractor/normalizeTernaries.js.map +7 -0
  43. package/dist/cjs/extractor/removeUnusedHooks.js +107 -0
  44. package/dist/cjs/extractor/removeUnusedHooks.js.map +7 -0
  45. package/dist/cjs/index.js +38 -0
  46. package/dist/cjs/index.js.map +7 -0
  47. package/dist/cjs/patchReactNativeWeb.js +157 -0
  48. package/dist/cjs/patchReactNativeWeb.js.map +7 -0
  49. package/dist/cjs/types.js +21 -0
  50. package/dist/cjs/types.js.map +7 -0
  51. package/dist/{constants.js → esm/constants.js} +4 -0
  52. package/dist/{constants.js.map → esm/constants.js.map} +3 -3
  53. package/dist/{extractor → esm/extractor}/accessSafe.js +0 -0
  54. package/dist/{extractor → esm/extractor}/accessSafe.js.map +1 -1
  55. package/dist/{extractor → esm/extractor}/babelParse.js +0 -0
  56. package/dist/{extractor → esm/extractor}/babelParse.js.map +1 -1
  57. package/dist/{extractor → esm/extractor}/buildClassName.js +0 -0
  58. package/dist/{extractor → esm/extractor}/buildClassName.js.map +1 -1
  59. package/dist/{extractor → esm/extractor}/createEvaluator.js +1 -1
  60. package/dist/esm/extractor/createEvaluator.js.map +7 -0
  61. package/dist/{extractor → esm/extractor}/createExtractor.js +128 -76
  62. package/dist/esm/extractor/createExtractor.js.map +7 -0
  63. package/dist/{extractor → esm/extractor}/ensureImportingConcat.js +1 -1
  64. package/dist/esm/extractor/ensureImportingConcat.js.map +7 -0
  65. package/dist/{extractor → esm/extractor}/evaluateAstNode.js +0 -0
  66. package/dist/{extractor → esm/extractor}/evaluateAstNode.js.map +1 -1
  67. package/dist/{extractor → esm/extractor}/extractHelpers.js +6 -4
  68. package/dist/{extractor → esm/extractor}/extractHelpers.js.map +3 -3
  69. package/dist/{extractor → esm/extractor}/extractMediaStyle.js +3 -2
  70. package/dist/{extractor → esm/extractor}/extractMediaStyle.js.map +2 -2
  71. package/dist/{extractor → esm/extractor}/extractToClassNames.js +38 -35
  72. package/dist/esm/extractor/extractToClassNames.js.map +7 -0
  73. package/dist/{extractor → esm/extractor}/findTopmostFunction.js +0 -0
  74. package/dist/{extractor → esm/extractor}/findTopmostFunction.js.map +1 -1
  75. package/dist/{extractor → esm/extractor}/generatedUid.js +0 -0
  76. package/dist/{extractor → esm/extractor}/generatedUid.js.map +1 -1
  77. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js +0 -0
  78. package/dist/{extractor → esm/extractor}/getPropValueFromAttributes.js.map +1 -1
  79. package/dist/{extractor → esm/extractor}/getSourceModule.js +0 -0
  80. package/dist/{extractor → esm/extractor}/getSourceModule.js.map +1 -1
  81. package/dist/{extractor → esm/extractor}/getStaticBindingsForScope.js +0 -0
  82. package/dist/{extractor → esm/extractor}/getStaticBindingsForScope.js.map +1 -1
  83. package/dist/{extractor → esm/extractor}/hoistClassNames.js +0 -0
  84. package/dist/{extractor → esm/extractor}/hoistClassNames.js.map +1 -1
  85. package/dist/{extractor → esm/extractor}/literalToAst.js +0 -0
  86. package/dist/{extractor → esm/extractor}/literalToAst.js.map +1 -1
  87. package/dist/{extractor → esm/extractor}/loadTamagui.js +5 -2
  88. package/dist/esm/extractor/loadTamagui.js.map +7 -0
  89. package/dist/esm/extractor/logLines.js +17 -0
  90. package/dist/esm/extractor/logLines.js.map +7 -0
  91. package/dist/{extractor → esm/extractor}/normalizeTernaries.js +3 -2
  92. package/dist/{extractor → esm/extractor}/normalizeTernaries.js.map +2 -2
  93. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js +5 -3
  94. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js.map +2 -2
  95. package/dist/{index.js → esm/index.js} +0 -0
  96. package/dist/{index.js.map → esm/index.js.map} +1 -1
  97. package/dist/{patchReactNativeWeb.js → esm/patchReactNativeWeb.js} +40 -3
  98. package/dist/esm/patchReactNativeWeb.js.map +7 -0
  99. package/dist/{types.js → esm/types.js} +0 -0
  100. package/dist/{types.js.map → esm/types.js.map} +0 -0
  101. package/dist/jsx/constants.js +13 -0
  102. package/dist/jsx/extractor/accessSafe.js +10 -0
  103. package/dist/jsx/extractor/babelParse.js +29 -0
  104. package/dist/jsx/extractor/buildClassName.js +39 -0
  105. package/dist/jsx/extractor/createEvaluator.js +50 -0
  106. package/dist/jsx/extractor/createExtractor.js +856 -0
  107. package/dist/jsx/extractor/ensureImportingConcat.js +23 -0
  108. package/dist/jsx/extractor/evaluateAstNode.js +93 -0
  109. package/dist/jsx/extractor/extractHelpers.js +95 -0
  110. package/dist/jsx/extractor/extractMediaStyle.js +151 -0
  111. package/dist/jsx/extractor/extractToClassNames.js +245 -0
  112. package/dist/jsx/extractor/findTopmostFunction.js +22 -0
  113. package/dist/jsx/extractor/generatedUid.js +27 -0
  114. package/dist/jsx/extractor/getPropValueFromAttributes.js +48 -0
  115. package/dist/jsx/extractor/getSourceModule.js +68 -0
  116. package/dist/jsx/extractor/getStaticBindingsForScope.js +127 -0
  117. package/dist/jsx/extractor/hoistClassNames.js +43 -0
  118. package/dist/jsx/extractor/literalToAst.js +33 -0
  119. package/dist/jsx/extractor/loadTamagui.js +43 -0
  120. package/dist/jsx/extractor/logLines.js +16 -0
  121. package/dist/jsx/extractor/normalizeTernaries.js +52 -0
  122. package/dist/jsx/extractor/removeUnusedHooks.js +79 -0
  123. package/dist/jsx/index.js +13 -0
  124. package/dist/jsx/patchReactNativeWeb.js +128 -0
  125. package/dist/jsx/types.js +0 -0
  126. package/package.json +25 -19
  127. package/types/constants.d.ts +6 -0
  128. package/types/constants.d.ts.map +1 -0
  129. package/types/extractor/accessSafe.d.ts +3 -0
  130. package/types/extractor/accessSafe.d.ts.map +1 -0
  131. package/types/extractor/babelParse.d.ts +5 -0
  132. package/types/extractor/babelParse.d.ts.map +1 -0
  133. package/types/extractor/buildClassName.d.ts +4 -0
  134. package/types/extractor/buildClassName.d.ts.map +1 -0
  135. package/types/extractor/createEvaluator.d.ts +12 -0
  136. package/types/extractor/createEvaluator.d.ts.map +1 -0
  137. package/types/extractor/createExtractor.d.ts +33 -0
  138. package/types/extractor/createExtractor.d.ts.map +1 -0
  139. package/types/extractor/ensureImportingConcat.d.ts +4 -0
  140. package/types/extractor/ensureImportingConcat.d.ts.map +1 -0
  141. package/types/extractor/evaluateAstNode.d.ts +3 -0
  142. package/types/extractor/evaluateAstNode.d.ts.map +1 -0
  143. package/types/extractor/extractHelpers.d.ts +12 -0
  144. package/types/extractor/extractHelpers.d.ts.map +1 -0
  145. package/types/extractor/extractMediaStyle.d.ts +10 -0
  146. package/types/extractor/extractMediaStyle.d.ts.map +1 -0
  147. package/types/extractor/extractToClassNames.d.ts +21 -0
  148. package/types/extractor/extractToClassNames.d.ts.map +1 -0
  149. package/types/extractor/findTopmostFunction.d.ts +4 -0
  150. package/types/extractor/findTopmostFunction.d.ts.map +1 -0
  151. package/types/extractor/generatedUid.d.ts +5 -0
  152. package/types/extractor/generatedUid.d.ts.map +1 -0
  153. package/types/extractor/getPropValueFromAttributes.d.ts +3 -0
  154. package/types/extractor/getPropValueFromAttributes.d.ts.map +1 -0
  155. package/types/extractor/getSourceModule.d.ts +16 -0
  156. package/types/extractor/getSourceModule.d.ts.map +1 -0
  157. package/types/extractor/getStaticBindingsForScope.d.ts +4 -0
  158. package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -0
  159. package/types/extractor/hoistClassNames.d.ts +6 -0
  160. package/types/extractor/hoistClassNames.d.ts.map +1 -0
  161. package/types/extractor/literalToAst.d.ts +3 -0
  162. package/types/extractor/literalToAst.d.ts.map +1 -0
  163. package/types/extractor/loadTamagui.d.ts +9 -0
  164. package/types/extractor/loadTamagui.d.ts.map +1 -0
  165. package/types/extractor/logLines.d.ts +2 -0
  166. package/types/extractor/logLines.d.ts.map +1 -0
  167. package/types/extractor/normalizeTernaries.d.ts +3 -0
  168. package/types/extractor/normalizeTernaries.d.ts.map +1 -0
  169. package/types/extractor/removeUnusedHooks.d.ts +3 -0
  170. package/types/extractor/removeUnusedHooks.d.ts.map +1 -0
  171. package/types/index.d.ts +8 -0
  172. package/types/index.d.ts.map +1 -0
  173. package/types/patchReactNativeWeb.d.ts +2 -0
  174. package/types/patchReactNativeWeb.d.ts.map +1 -0
  175. package/types/types.d.ts +79 -0
  176. package/types/types.d.ts.map +1 -0
  177. package/dist/extractor/createEvaluator.js.map +0 -7
  178. package/dist/extractor/createExtractor.js.map +0 -7
  179. package/dist/extractor/ensureImportingConcat.js.map +0 -7
  180. package/dist/extractor/extractToClassNames.js.map +0 -7
  181. package/dist/extractor/loadTamagui.js.map +0 -7
  182. package/dist/index.cjs +0 -2085
  183. package/dist/index.cjs.map +0 -7
  184. package/dist/patchReactNativeWeb.js.map +0 -7
  185. package/src/constants.ts +0 -10
  186. package/src/extractor/accessSafe.ts +0 -18
  187. package/src/extractor/babelParse.ts +0 -27
  188. package/src/extractor/buildClassName.ts +0 -61
  189. package/src/extractor/createEvaluator.ts +0 -68
  190. package/src/extractor/createExtractor.ts +0 -1116
  191. package/src/extractor/ensureImportingConcat.ts +0 -33
  192. package/src/extractor/evaluateAstNode.ts +0 -121
  193. package/src/extractor/extractHelpers.ts +0 -102
  194. package/src/extractor/extractMediaStyle.ts +0 -191
  195. package/src/extractor/extractToClassNames.ts +0 -337
  196. package/src/extractor/findTopmostFunction.ts +0 -22
  197. package/src/extractor/generatedUid.ts +0 -43
  198. package/src/extractor/getPropValueFromAttributes.ts +0 -92
  199. package/src/extractor/getSourceModule.ts +0 -101
  200. package/src/extractor/getStaticBindingsForScope.ts +0 -173
  201. package/src/extractor/hoistClassNames.ts +0 -45
  202. package/src/extractor/literalToAst.ts +0 -32
  203. package/src/extractor/loadTamagui.ts +0 -61
  204. package/src/extractor/normalizeTernaries.ts +0 -60
  205. package/src/extractor/removeUnusedHooks.ts +0 -76
  206. package/src/index.ts +0 -9
  207. package/src/patchReactNativeWeb.ts +0 -107
  208. package/src/types.ts +0 -80
  209. package/types.d.ts +0 -210
@@ -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,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,107 +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
-
13
- // write our export files
14
- fs.writeFileSync(path.join(rootDir, 'dist', 'tamagui-exports.js'), moduleExports)
15
- fs.writeFileSync(path.join(rootDir, 'dist', 'cjs', 'tamagui-exports.js'), cjsExports)
16
-
17
- // if entry files not patched, patch them:
18
- const moduleEntry = path.join(rootDir, 'dist', 'index.js')
19
- const moduleEntrySrc = fs.readFileSync(moduleEntry, 'utf-8')
20
- if (!moduleEntrySrc.includes('// tamagui-patch-v4')) {
21
- fs.writeFileSync(
22
- moduleEntry,
23
- `${removePatch(moduleEntrySrc)}
24
-
25
- // tamagui-patch-v4
26
- import * as TExports from './tamagui-exports'
27
- export const TamaguiExports = TExports
28
- // tamagui-patch-end
29
- `
30
- )
31
- }
32
- const cjsEntry = path.join(rootDir, 'dist', 'cjs', 'index.js')
33
- const cjsEntrySrc = fs.readFileSync(cjsEntry, 'utf-8')
34
- if (!cjsEntrySrc.includes('// tamagui-patch-v4')) {
35
- fs.writeFileSync(
36
- cjsEntry,
37
- `${removePatch(cjsEntrySrc)}
38
-
39
- // tamagui-patch-v4
40
- exports.TamaguiExports = _interopRequireDefault(require("./tamagui-exports"));
41
- // tamagui-patch-end
42
- `
43
- )
44
-
45
- console.log(`Tamagui patched react-native-web`)
46
- }
47
- }
48
-
49
- function removePatch(source: string) {
50
- return source.replace(/\/\/ tamagui-patch([.\s\S]*)/g, '')
51
- }
52
-
53
- // view exports/View.ts
54
- const forwardedPropsObj = `{
55
- ...fwdProps.defaultProps,
56
- ...fwdProps.accessibilityProps,
57
- ...fwdProps.clickProps,
58
- ...fwdProps.focusProps,
59
- ...fwdProps.keyboardProps,
60
- ...fwdProps.mouseProps,
61
- ...fwdProps.touchProps,
62
- ...fwdProps.styleProps,
63
- href: true,
64
- lang: true,
65
- onScroll: true,
66
- onWheel: true,
67
- pointerEvents: true
68
- }
69
- `
70
-
71
- const moduleExports = `
72
- export { atomic } from './exports/StyleSheet/compile'
73
- export { default as createCompileableStyle } from './exports/StyleSheet/createCompileableStyle'
74
- export { default as createReactDOMStyle } from './exports/StyleSheet/createReactDOMStyle'
75
- export { default as i18Style } from './exports/StyleSheet/i18nStyle'
76
- export { default as createDOMProps } from './modules/createDOMProps'
77
- export { default as AccessibilityUtil } from './modules/AccessibilityUtil'
78
- export { default as createElement } from './exports/createElement'
79
- export { default as css } from './exports/StyleSheet/css'
80
- export { default as TextAncestorContext } from './exports/Text/TextAncestorContext'
81
- export { default as pick } from './modules/pick'
82
- export { default as useElementLayout } from './modules/useElementLayout'
83
- export { default as useMergeRefs } from './modules/useMergeRefs'
84
- export { default as usePlatformMethods } from './modules/usePlatformMethods'
85
- export { default as useResponderEvents } from './modules/useResponderEvents'
86
- import * as fwdProps from './modules/forwardedProps'
87
- export const forwardedProps = ${forwardedPropsObj}
88
- `
89
-
90
- const cjsExports = `
91
- exports.atomic = require('./exports/StyleSheet/compile').atomic
92
- exports.createCompileableStyle = require('./exports/StyleSheet/createCompileableStyle')
93
- exports.createReactDOMStyle = require('./exports/StyleSheet/createReactDOMStyle')
94
- exports.i18Style = require('./exports/StyleSheet/i18nStyle')
95
- exports.createDOMProps = require('./modules/createDOMProps')
96
- exports.AccessibilityUtil = require('./modules/AccessibilityUtil')
97
- exports.createElement = require('./exports/createElement')
98
- exports.css = require('./exports/StyleSheet/css')
99
- exports.TextAncestorContext = require('./exports/Text/TextAncestorContext')
100
- exports.pick = require('./modules/pick')
101
- exports.useElementLayout = require('./modules/useElementLayout')
102
- exports.useMergeRefs = require('./modules/useMergeRefs')
103
- exports.usePlatformMethods = require('./modules/usePlatformMethods')
104
- exports.useResponderEvents = require('./modules/useResponderEvents')
105
- const fwdProps = require('./modules/forwardedProps')
106
- exports.forwardedProps = ${forwardedPropsObj}
107
- `
package/src/types.ts DELETED
@@ -1,80 +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
- exclude?: RegExp
19
- logTimings?: boolean
20
-
21
- // probably non user options
22
- cssPath?: string
23
- cssData?: any
24
- deoptProps?: Set<string>
25
- excludeProps?: string[]
26
- }
27
-
28
- export type ExtractedAttrAttr = {
29
- type: 'attr'
30
- value: t.JSXAttribute | t.JSXSpreadAttribute
31
- }
32
-
33
- export type ExtractedAttrStyle = { type: 'style'; value: Object }
34
-
35
- export type ExtractedAttr = ExtractedAttrAttr | { type: 'ternary'; value: Ternary } | ExtractedAttrStyle
36
-
37
- export type ExtractTagProps = {
38
- attrs: ExtractedAttr[]
39
- node: t.JSXOpeningElement
40
- attemptEval: (exprNode: t.Node, evalFn?: ((node: t.Node) => any) | undefined) => any
41
- jsxPath: NodePath<t.JSXElement>
42
- programPath: NodePath<t.Program>
43
- originalNodeName: string
44
- lineNumbers: string
45
- filePath: string
46
- isFlattened: boolean
47
- }
48
-
49
- export type ExtractorParseProps = TamaguiOptions & {
50
- sourcePath?: string
51
- shouldPrintDebug?: boolean
52
- onExtractTag: (props: ExtractTagProps) => void
53
- getFlattenedNode: (props: { isTextView: boolean; tag: string }) => string
54
- onDidFlatten?: () => void
55
- }
56
-
57
- export interface Ternary {
58
- test: t.Expression
59
- // shorthand props that don't use hooks
60
- inlineMediaQuery?: string
61
- remove: Function
62
- consequent: Object | null
63
- alternate: Object | null
64
- }
65
-
66
- export type StyleObject = {
67
- property: string
68
- value: string
69
- className: string
70
- identifier: string
71
- rules: string[]
72
- }
73
-
74
- export type ClassNameToStyleObj = {
75
- [key: string]: StyleObject
76
- }
77
-
78
- export interface PluginContext {
79
- write: (path: string, rules: { [key: string]: string }) => any
80
- }