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

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 (244) 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 +162 -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 +10 -1
  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 +136 -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/LICENSE +0 -21
  200. package/dist/extractor/createEvaluator.js.map +0 -7
  201. package/dist/extractor/createExtractor.js +0 -828
  202. package/dist/extractor/createExtractor.js.map +0 -7
  203. package/dist/extractor/ensureImportingConcat.js.map +0 -7
  204. package/dist/extractor/evaluateAstNode.js.map +0 -7
  205. package/dist/extractor/extractHelpers.js.map +0 -7
  206. package/dist/extractor/extractMediaStyle.js.map +0 -7
  207. package/dist/extractor/extractToClassNames.js.map +0 -7
  208. package/dist/extractor/getStaticBindingsForScope.js.map +0 -7
  209. package/dist/extractor/literalToAst.js +0 -34
  210. package/dist/extractor/literalToAst.js.map +0 -7
  211. package/dist/extractor/loadTamagui.js +0 -41
  212. package/dist/extractor/loadTamagui.js.map +0 -7
  213. package/dist/extractor/logLines.js.map +0 -7
  214. package/dist/extractor/normalizeTernaries.js.map +0 -7
  215. package/dist/extractor/removeUnusedHooks.js.map +0 -7
  216. package/dist/index.cjs +0 -2126
  217. package/dist/index.cjs.map +0 -7
  218. package/dist/patchReactNativeWeb.js.map +0 -7
  219. package/src/constants.ts +0 -10
  220. package/src/extractor/accessSafe.ts +0 -18
  221. package/src/extractor/babelParse.ts +0 -27
  222. package/src/extractor/buildClassName.ts +0 -61
  223. package/src/extractor/createEvaluator.ts +0 -68
  224. package/src/extractor/createExtractor.ts +0 -1138
  225. package/src/extractor/ensureImportingConcat.ts +0 -33
  226. package/src/extractor/evaluateAstNode.ts +0 -121
  227. package/src/extractor/extractHelpers.ts +0 -102
  228. package/src/extractor/extractMediaStyle.ts +0 -191
  229. package/src/extractor/extractToClassNames.ts +0 -323
  230. package/src/extractor/findTopmostFunction.ts +0 -22
  231. package/src/extractor/generatedUid.ts +0 -43
  232. package/src/extractor/getPropValueFromAttributes.ts +0 -92
  233. package/src/extractor/getSourceModule.ts +0 -101
  234. package/src/extractor/getStaticBindingsForScope.ts +0 -173
  235. package/src/extractor/hoistClassNames.ts +0 -45
  236. package/src/extractor/literalToAst.ts +0 -32
  237. package/src/extractor/loadTamagui.ts +0 -61
  238. package/src/extractor/logLines.ts +0 -11
  239. package/src/extractor/normalizeTernaries.ts +0 -60
  240. package/src/extractor/removeUnusedHooks.ts +0 -76
  241. package/src/index.ts +0 -9
  242. package/src/patchReactNativeWeb.ts +0 -123
  243. package/src/types.ts +0 -89
  244. package/types.d.ts +0 -219
@@ -1,323 +0,0 @@
1
- import * as path from 'path'
2
- import { basename } from 'path'
3
- import * as util from 'util'
4
-
5
- import generate from '@babel/generator'
6
- import * as t from '@babel/types'
7
- import { getStylesAtomic } from '@tamagui/core-node'
8
- import { concatClassName } from '@tamagui/helpers'
9
- import invariant from 'invariant'
10
- import { getRemainingRequest } from 'loader-utils'
11
- import { ViewStyle } from 'react-native'
12
-
13
- import { ClassNameObject, StyleObject, TamaguiOptions } from '../types'
14
- import { babelParse } from './babelParse'
15
- import { buildClassName } from './buildClassName'
16
- import { Extractor } from './createExtractor'
17
- import { ensureImportingConcat } from './ensureImportingConcat'
18
- import { isSimpleSpread } from './extractHelpers'
19
- import { extractMediaStyle } from './extractMediaStyle'
20
- import { hoistClassNames } from './hoistClassNames'
21
- import { logLines } from './logLines'
22
-
23
- export const CONCAT_CLASSNAME_IMPORT = 'concatClassName'
24
-
25
- const mergeStyleGroups = {
26
- shadowOpacity: true,
27
- shadowRadius: true,
28
- shadowColor: true,
29
- shadowOffset: true,
30
- }
31
-
32
- export function extractToClassNames({
33
- loader,
34
- extractor,
35
- source,
36
- sourcePath,
37
- options,
38
- shouldPrintDebug,
39
- threaded,
40
- cssPath,
41
- }: {
42
- loader: any
43
- extractor: Extractor
44
- source: string | Buffer
45
- sourcePath: string
46
- options: TamaguiOptions
47
- shouldPrintDebug: boolean
48
- cssPath: string
49
- threaded?: boolean
50
- }): null | {
51
- js: string | Buffer
52
- styles: string
53
- stylesPath?: string
54
- ast: t.File
55
- map: any // RawSourceMap from 'source-map'
56
- } {
57
- if (typeof source !== 'string') {
58
- throw new Error('`source` must be a string of javascript')
59
- }
60
- invariant(
61
- typeof sourcePath === 'string' && path.isAbsolute(sourcePath),
62
- '`sourcePath` must be an absolute path to a .js file'
63
- )
64
-
65
- const shouldLogTiming = shouldPrintDebug || options.logTimings
66
- const start = Date.now()
67
- const mem = shouldLogTiming ? process.memoryUsage() : null
68
-
69
- // Using a map for (officially supported) guaranteed insertion order
70
- let ast: t.File
71
-
72
- try {
73
- // @ts-ignore
74
- ast = babelParse(source)
75
- } catch (err) {
76
- console.error('babel parse error:', sourcePath)
77
- throw err
78
- }
79
-
80
- const cssMap = new Map<string, { css: string; commentTexts: string[] }>()
81
- const existingHoists: { [key: string]: t.Identifier } = {}
82
-
83
- const res = extractor.parse(ast, {
84
- sourcePath,
85
- shouldPrintDebug,
86
- ...options,
87
- getFlattenedNode: ({ tag }) => tag,
88
- onExtractTag: ({
89
- attrs,
90
- node,
91
- attemptEval,
92
- jsxPath,
93
- originalNodeName,
94
- filePath,
95
- lineNumbers,
96
- programPath,
97
- }) => {
98
- let finalClassNames: ClassNameObject[] = []
99
- let finalAttrs: (t.JSXAttribute | t.JSXSpreadAttribute)[] = []
100
- let finalStyles: StyleObject[] = []
101
-
102
- const viewStyles = {}
103
- for (const attr of attrs) {
104
- if (attr.type === 'style') {
105
- Object.assign(viewStyles, attr.value)
106
- }
107
- }
108
-
109
- const ensureNeededPrevStyle = (style: ViewStyle) => {
110
- // ensure all group keys are merged
111
- const keys = Object.keys(style)
112
- if (!keys.some((key) => mergeStyleGroups[key])) {
113
- return style
114
- }
115
- for (const k in mergeStyleGroups) {
116
- if (k in viewStyles) {
117
- style[k] = style[k] ?? viewStyles[k]
118
- }
119
- }
120
- return style
121
- }
122
-
123
- const addStyles = (style: ViewStyle | null) => {
124
- if (!style) return []
125
- const styleWithPrev = ensureNeededPrevStyle(style)
126
- const res = getStylesAtomic(styleWithPrev)
127
- if (res.length) {
128
- finalStyles = [...finalStyles, ...res]
129
- }
130
- return res
131
- }
132
-
133
- // 1 to start above any :hover styles
134
- let lastMediaImportance = 1
135
- for (const attr of attrs) {
136
- switch (attr.type) {
137
- case 'style':
138
- const styles = addStyles(attr.value)
139
- const newClassNames = concatClassName(styles.map((x) => x.identifier).join(' '))
140
- // prettier-ignore
141
- const existing = finalClassNames.find((x) => x.type == 'StringLiteral') as t.StringLiteral | null
142
- if (existing) {
143
- existing.value = `${existing.value} ${newClassNames}`
144
- } else {
145
- finalClassNames = [...finalClassNames, t.stringLiteral(newClassNames)]
146
- }
147
- if (shouldPrintDebug) {
148
- // prettier-ignore
149
- console.log(' classnames (after)\n', logLines(finalClassNames.map(x => x['value']).join(' ')))
150
- }
151
- break
152
- case 'attr':
153
- const val = attr.value
154
- if (t.isJSXSpreadAttribute(val)) {
155
- if (isSimpleSpread(val)) {
156
- finalClassNames.push(
157
- t.logicalExpression(
158
- '&&',
159
- val.argument,
160
- t.memberExpression(val.argument, t.identifier('className'))
161
- )
162
- )
163
- }
164
- } else if (val.name.name === 'className') {
165
- const value = val.value
166
- if (value) {
167
- try {
168
- const evaluatedValue = attemptEval(value)
169
- finalClassNames.push(t.stringLiteral(evaluatedValue))
170
- } catch (e) {
171
- finalClassNames.push(value['expression'])
172
- }
173
- }
174
- continue
175
- }
176
- finalAttrs.push(val)
177
- break
178
- case 'ternary':
179
- const mediaExtraction = extractMediaStyle(
180
- attr.value,
181
- jsxPath,
182
- extractor.getTamagui(),
183
- sourcePath,
184
- lastMediaImportance,
185
- shouldPrintDebug
186
- )
187
- if (mediaExtraction) {
188
- lastMediaImportance++
189
- finalStyles = [...finalStyles, ...mediaExtraction.mediaStyles]
190
- finalClassNames = [
191
- ...finalClassNames,
192
- ...mediaExtraction.mediaStyles.map((x) => t.stringLiteral(x.identifier)),
193
- ]
194
- if (!mediaExtraction.ternaryWithoutMedia) {
195
- continue
196
- }
197
- }
198
- const ternary = mediaExtraction?.ternaryWithoutMedia || attr.value
199
- const consInfo = addStyles(ternary.consequent)
200
- const altInfo = addStyles(ternary.alternate)
201
- const cCN = consInfo.map((x) => x.identifier).join(' ')
202
- const aCN = altInfo.map((x) => x.identifier).join(' ')
203
- if (consInfo.length && altInfo.length) {
204
- finalClassNames.push(
205
- t.conditionalExpression(ternary.test, t.stringLiteral(cCN), t.stringLiteral(aCN))
206
- )
207
- } else {
208
- finalClassNames.push(
209
- t.conditionalExpression(
210
- ternary.test,
211
- t.stringLiteral(' ' + cCN),
212
- t.stringLiteral(' ' + aCN)
213
- )
214
- )
215
- }
216
- break
217
- }
218
- }
219
-
220
- node.attributes = finalAttrs
221
-
222
- if (finalClassNames.length) {
223
- // inserts the _cn variable and uses it for className
224
- const names = buildClassName(finalClassNames)
225
- const nameExpr = names ? hoistClassNames(jsxPath, existingHoists, names) : null
226
- let expr = nameExpr
227
-
228
- // if has some spreads, use concat helper
229
- if (nameExpr && !t.isIdentifier(nameExpr)) {
230
- ensureImportingConcat(programPath)
231
- const simpleSpreads = attrs.filter(
232
- (x) => t.isJSXSpreadAttribute(x.value) && isSimpleSpread(x.value)
233
- )
234
- expr = t.callExpression(t.identifier(CONCAT_CLASSNAME_IMPORT), [
235
- expr,
236
- ...simpleSpreads.map((val) => val.value['argument']),
237
- ])
238
- }
239
-
240
- node.attributes.push(
241
- t.jsxAttribute(t.jsxIdentifier('className'), t.jsxExpressionContainer(expr))
242
- )
243
- }
244
-
245
- const comment = util.format('/* %s:%s (%s) */', filePath, lineNumbers, originalNodeName)
246
-
247
- for (const { className, rules } of finalStyles) {
248
- if (cssMap.has(className)) {
249
- if (comment) {
250
- const val = cssMap.get(className)!
251
- val.commentTexts.push(comment)
252
- cssMap.set(className, val)
253
- }
254
- } else if (rules.length) {
255
- if (rules.length > 1) {
256
- console.log(' rules error', { rules })
257
- throw new Error(`Shouldn't have more than one rule`)
258
- }
259
- cssMap.set(className, {
260
- css: rules[0],
261
- commentTexts: [comment],
262
- })
263
- }
264
- }
265
- },
266
- })
267
-
268
- if (!res || !res.modified) {
269
- return null
270
- }
271
-
272
- const styles = Array.from(cssMap.values())
273
- .map((x) => x.css)
274
- .join('\n')
275
- .trim()
276
-
277
- if (styles) {
278
- const cssQuery = threaded
279
- ? `cssData=${Buffer.from(styles).toString('base64')}`
280
- : `cssPath=${cssPath}`
281
- const remReq = getRemainingRequest(loader)
282
- const importPath = `${cssPath}!=!tamagui-loader?${cssQuery}!${remReq}`
283
- ast.program.body.unshift(t.importDeclaration([], t.stringLiteral(importPath)))
284
- }
285
-
286
- const result = generate(
287
- ast,
288
- {
289
- concise: false,
290
- filename: sourcePath,
291
- retainLines: false,
292
- sourceFileName: sourcePath,
293
- sourceMaps: true,
294
- },
295
- source
296
- )
297
-
298
- if (shouldPrintDebug) {
299
- console.log(
300
- '\n -------- output code ------- \n\n',
301
- result.code
302
- .split('\n')
303
- .filter((x) => !x.startsWith('//'))
304
- .join('\n')
305
- )
306
- console.log('\n -------- output style -------- \n\n', styles)
307
- }
308
-
309
- if (shouldLogTiming && mem) {
310
- // console.log(`${parseTime} / ${traverseTime} / ${generateTime}`)
311
- const memUsed =
312
- Math.round(((process.memoryUsage().heapUsed - mem.heapUsed) / 1024 / 1204) * 10) / 10
313
- // prettier-ignore
314
- console.log(` 🥚 ${basename(sourcePath).padStart(40)} ${`${Date.now() - start}`.padStart(3)}ms ׁ· ${`${res.optimized}`.padStart(4)} optimized · ${res.flattened} flattened ${memUsed > 10 ? `used ${memUsed}MB` : ''}`)
315
- }
316
-
317
- return {
318
- ast,
319
- styles,
320
- js: result.code,
321
- map: result.map,
322
- }
323
- }
@@ -1,22 +0,0 @@
1
- import { NodePath } from '@babel/traverse'
2
- import * as t from '@babel/types'
3
-
4
- export function findTopmostFunction(jsxPath: NodePath<t.JSXElement>) {
5
- // get topmost fn
6
- const isFunction = (path: NodePath<any>) =>
7
- path.isArrowFunctionExpression() || path.isFunctionDeclaration() || path.isFunctionExpression()
8
- let compFn: NodePath<any> | null = jsxPath.findParent(isFunction)
9
- while (compFn) {
10
- const parent = compFn.findParent(isFunction)
11
- if (parent) {
12
- compFn = parent
13
- } else {
14
- break
15
- }
16
- }
17
- if (!compFn) {
18
- // console.error(`Couldn't find a topmost function for media query extraction`)
19
- return null
20
- }
21
- return compFn
22
- }
@@ -1,43 +0,0 @@
1
- import * as t from '@babel/types'
2
- import invariant from 'invariant'
3
-
4
- // TODO: open a PR upstream
5
- declare module '@babel/types' {
6
- export function toIdentifier(input: string): string
7
- }
8
-
9
- // A clone of path.scope.generateUid that doesn't prepend underscores
10
- export function generateUid(scope: any, name: string): string {
11
- invariant(typeof scope === 'object', 'generateUid expects a scope object as its first parameter')
12
- invariant(
13
- typeof name === 'string' && name !== '',
14
- 'generateUid expects a valid name as its second parameter'
15
- )
16
-
17
- name = t
18
- .toIdentifier(name)
19
- .replace(/^_+/, '')
20
- .replace(/[0-9]+$/g, '')
21
-
22
- let uid
23
- let i = 0
24
- do {
25
- if (i > 1) {
26
- uid = name + i
27
- } else {
28
- uid = name
29
- }
30
- i++
31
- } while (
32
- scope.hasLabel(uid) ||
33
- scope.hasBinding(uid) ||
34
- scope.hasGlobal(uid) ||
35
- scope.hasReference(uid)
36
- )
37
-
38
- const program = scope.getProgramParent()
39
- program.references[uid] = true
40
- program.uids[uid] = true
41
-
42
- return uid
43
- }
@@ -1,92 +0,0 @@
1
- import generate from '@babel/generator'
2
- import * as t from '@babel/types'
3
-
4
- import { accessSafe } from './accessSafe'
5
-
6
- /**
7
- * getPropValueFromAttributes gets a prop by name from a list of attributes and accounts for potential spread operators.
8
- * Here's an example. Given this component:
9
- * ```
10
- * <Block coolProp="wow" {...spread1} neatProp="ok" {...spread2} />```
11
- * getPropValueFromAttributes will return the following:
12
- * - for propName `coolProp`:
13
- * ```
14
- * accessSafe(spread1, 'coolProp') || accessSafe(spread2, 'coolProp') || 'wow'```
15
- * - for propName `neatProp`:
16
- * ```
17
- * accessSafe(spread2, 'neatProp') || 'ok'```
18
- * - for propName `notPresent`: `null`
19
- *
20
- * The returned value should (obviously) be placed after spread operators.
21
- */
22
- export function getPropValueFromAttributes(
23
- propName: string,
24
- attrs: (t.JSXAttribute | t.JSXSpreadAttribute)[]
25
- ): t.Expression | null {
26
- let propIndex: number = -1
27
- let jsxAttr: t.JSXAttribute | null = null
28
- for (let idx = -1, len = attrs.length; ++idx < len; ) {
29
- const attr = attrs[idx]
30
- if (t.isJSXAttribute(attr) && attr.name && attr.name.name === propName) {
31
- propIndex = idx
32
- jsxAttr = attr
33
- break
34
- }
35
- }
36
-
37
- if (!jsxAttr || jsxAttr.value == null) {
38
- return null
39
- }
40
-
41
- let propValue:
42
- | t.JSXElement
43
- | t.JSXFragment
44
- | t.StringLiteral
45
- | t.JSXExpressionContainer
46
- | t.JSXEmptyExpression
47
- | t.Expression = jsxAttr.value
48
-
49
- if (t.isJSXExpressionContainer(propValue)) {
50
- propValue = propValue.expression
51
- }
52
-
53
- // TODO how to handle this??
54
- if (t.isJSXEmptyExpression(propValue)) {
55
- console.error('encountered JSXEmptyExpression')
56
- return null
57
- }
58
-
59
- // filter out spread props that occur before propValue
60
- const applicableSpreads = attrs
61
- .filter(
62
- // 1. idx is greater than propValue prop index
63
- // 2. attr is a spread operator
64
- (attr, idx): attr is t.JSXSpreadAttribute => {
65
- if (t.isJSXSpreadAttribute(attr)) {
66
- if (t.isIdentifier(attr.argument) || t.isMemberExpression(attr.argument)) {
67
- return idx > propIndex
68
- }
69
- if (t.isLogicalExpression(attr.argument)) {
70
- return false
71
- }
72
- throw new Error(
73
- `unsupported spread of type "${attr.argument.type}": ${generate(attr).code}`
74
- )
75
- }
76
- return false
77
- }
78
- )
79
- .map((attr) => attr.argument)
80
-
81
- // if spread operators occur after propValue, create a binary expression for each operator
82
- // i.e. before1.propValue || before2.propValue || propValue
83
- // TODO: figure out how to do this without all the extra parens
84
- if (applicableSpreads.length > 0) {
85
- propValue = applicableSpreads.reduce<t.Expression>(
86
- (acc, val) => t.logicalExpression('||', accessSafe(val, propName), acc),
87
- propValue
88
- )
89
- }
90
-
91
- return propValue
92
- }
@@ -1,101 +0,0 @@
1
- import * as t from '@babel/types'
2
-
3
- export interface SourceModule {
4
- sourceModule?: string
5
- imported?: string
6
- local?: string
7
- destructured?: boolean
8
- usesImportSyntax: boolean
9
- }
10
-
11
- export function getSourceModule(
12
- itemName: string,
13
- itemBinding: {
14
- constant?: boolean
15
- path: { node: t.Node; parent: any }
16
- }
17
- ): SourceModule | null {
18
- // TODO: deal with reassignment
19
- if (!itemBinding.constant) {
20
- return null
21
- }
22
-
23
- let sourceModule: string | undefined
24
- let imported: string | undefined
25
- let local: string | undefined
26
- let destructured: boolean | undefined
27
- let usesImportSyntax = false
28
-
29
- const itemNode = itemBinding.path.node
30
-
31
- if (
32
- // import x from 'y';
33
- t.isImportDefaultSpecifier(itemNode) ||
34
- // import {x} from 'y';
35
- t.isImportSpecifier(itemNode)
36
- ) {
37
- if (t.isImportDeclaration(itemBinding.path.parent)) {
38
- sourceModule = itemBinding.path.parent.source.value
39
- local = itemNode.local.name
40
- usesImportSyntax = true
41
- if (t.isImportSpecifier(itemNode)) {
42
- imported = itemNode.imported['name']
43
- destructured = true
44
- } else {
45
- imported = itemNode.local.name
46
- destructured = false
47
- }
48
- }
49
- } else if (
50
- t.isVariableDeclarator(itemNode) &&
51
- itemNode.init != null &&
52
- t.isCallExpression(itemNode.init) &&
53
- t.isIdentifier(itemNode.init.callee) &&
54
- itemNode.init.callee.name === 'require' &&
55
- itemNode.init.arguments.length === 1
56
- ) {
57
- const firstArg = itemNode.init.arguments[0]
58
- if (!t.isStringLiteral(firstArg)) {
59
- return null
60
- }
61
- sourceModule = firstArg.value
62
-
63
- if (t.isIdentifier(itemNode.id)) {
64
- local = itemNode.id.name
65
- imported = itemNode.id.name
66
- destructured = false
67
- } else if (t.isObjectPattern(itemNode.id)) {
68
- for (const objProp of itemNode.id.properties) {
69
- if (
70
- t.isObjectProperty(objProp) &&
71
- t.isIdentifier(objProp.value) &&
72
- objProp.value.name === itemName
73
- ) {
74
- local = objProp.value.name
75
- // @ts-ignore TODO remove this is only an issue on CI
76
- imported = objProp.key.name
77
- destructured = true
78
- break
79
- }
80
- }
81
-
82
- if (!local || !imported) {
83
- console.error('could not find prop with value `%s`', itemName)
84
- return null
85
- }
86
- } else {
87
- console.error('Unhandled id type: %s', itemNode.id.type)
88
- return null
89
- }
90
- } else {
91
- return null
92
- }
93
-
94
- return {
95
- destructured,
96
- imported,
97
- local,
98
- sourceModule,
99
- usesImportSyntax,
100
- }
101
- }