@tamagui/static 1.0.0-alpha.8 → 1.0.0-beta.179

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 (231) 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 +1091 -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 +411 -196
  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 +100 -47
  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/esm/extractor/logLines.js +22 -0
  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 +1041 -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/normalizeTernaries.js.map +0 -7
  204. package/dist/extractor/removeUnusedHooks.js.map +0 -7
  205. package/dist/index.cjs +0 -2116
  206. package/dist/index.cjs.map +0 -7
  207. package/dist/patchReactNativeWeb.js.map +0 -7
  208. package/src/constants.ts +0 -10
  209. package/src/extractor/accessSafe.ts +0 -18
  210. package/src/extractor/babelParse.ts +0 -27
  211. package/src/extractor/buildClassName.ts +0 -61
  212. package/src/extractor/createEvaluator.ts +0 -68
  213. package/src/extractor/createExtractor.ts +0 -1139
  214. package/src/extractor/ensureImportingConcat.ts +0 -33
  215. package/src/extractor/evaluateAstNode.ts +0 -121
  216. package/src/extractor/extractHelpers.ts +0 -102
  217. package/src/extractor/extractMediaStyle.ts +0 -191
  218. package/src/extractor/extractToClassNames.ts +0 -326
  219. package/src/extractor/findTopmostFunction.ts +0 -22
  220. package/src/extractor/generatedUid.ts +0 -43
  221. package/src/extractor/getPropValueFromAttributes.ts +0 -92
  222. package/src/extractor/getSourceModule.ts +0 -101
  223. package/src/extractor/getStaticBindingsForScope.ts +0 -173
  224. package/src/extractor/hoistClassNames.ts +0 -45
  225. package/src/extractor/literalToAst.ts +0 -32
  226. package/src/extractor/loadTamagui.ts +0 -61
  227. package/src/extractor/normalizeTernaries.ts +0 -60
  228. package/src/extractor/removeUnusedHooks.ts +0 -76
  229. package/src/index.ts +0 -9
  230. package/src/types.ts +0 -89
  231. package/types.d.ts +0 -219
@@ -1,326 +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
-
22
- export const CONCAT_CLASSNAME_IMPORT = 'concatClassName'
23
-
24
- const mergeStyleGroups = {
25
- shadowOpacity: true,
26
- shadowRadius: true,
27
- shadowColor: true,
28
- shadowOffset: true,
29
- }
30
-
31
- export function extractToClassNames({
32
- loader,
33
- extractor,
34
- source,
35
- sourcePath,
36
- options,
37
- shouldPrintDebug,
38
- threaded,
39
- cssPath,
40
- }: {
41
- loader: any
42
- extractor: Extractor
43
- source: string | Buffer
44
- sourcePath: string
45
- options: TamaguiOptions
46
- shouldPrintDebug: boolean
47
- cssPath: string
48
- threaded?: boolean
49
- }): null | {
50
- js: string | Buffer
51
- styles: string
52
- stylesPath?: string
53
- ast: t.File
54
- map: any // RawSourceMap from 'source-map'
55
- } {
56
- if (typeof source !== 'string') {
57
- throw new Error('`source` must be a string of javascript')
58
- }
59
- invariant(
60
- typeof sourcePath === 'string' && path.isAbsolute(sourcePath),
61
- '`sourcePath` must be an absolute path to a .js file'
62
- )
63
-
64
- const shouldLogTiming = shouldPrintDebug || options.logTimings
65
- const start = Date.now()
66
- const mem = shouldLogTiming ? process.memoryUsage() : null
67
-
68
- // Using a map for (officially supported) guaranteed insertion order
69
- let ast: t.File
70
-
71
- try {
72
- // @ts-ignore
73
- ast = babelParse(source)
74
- } catch (err) {
75
- console.error('babel parse error:', sourcePath)
76
- throw err
77
- }
78
-
79
- const cssMap = new Map<string, { css: string; commentTexts: string[] }>()
80
- const existingHoists: { [key: string]: t.Identifier } = {}
81
-
82
- const res = extractor.parse(ast, {
83
- sourcePath,
84
- shouldPrintDebug,
85
- ...options,
86
- getFlattenedNode: ({ tag }) => tag,
87
- onExtractTag: ({
88
- attrs,
89
- node,
90
- attemptEval,
91
- jsxPath,
92
- originalNodeName,
93
- filePath,
94
- lineNumbers,
95
- programPath,
96
- }) => {
97
- let finalClassNames: ClassNameObject[] = []
98
- let finalAttrs: (t.JSXAttribute | t.JSXSpreadAttribute)[] = []
99
- let finalStyles: StyleObject[] = []
100
-
101
- const viewStyles = {}
102
- for (const attr of attrs) {
103
- if (attr.type === 'style') {
104
- Object.assign(viewStyles, attr.value)
105
- }
106
- }
107
-
108
- const ensureNeededPrevStyle = (style: ViewStyle) => {
109
- // ensure all group keys are merged
110
- const keys = Object.keys(style)
111
- if (!keys.some((key) => mergeStyleGroups[key])) {
112
- return style
113
- }
114
- for (const k in mergeStyleGroups) {
115
- if (k in viewStyles) {
116
- style[k] = style[k] ?? viewStyles[k]
117
- }
118
- }
119
- return style
120
- }
121
-
122
- const addStyles = (style: ViewStyle | null) => {
123
- if (!style) return []
124
- const styleWithPrev = ensureNeededPrevStyle(style)
125
- const res = getStylesAtomic(styleWithPrev)
126
- if (res.length) {
127
- finalStyles = [...finalStyles, ...res]
128
- }
129
- return res
130
- }
131
-
132
- // 1 to start above any :hover styles
133
- let lastMediaImportance = 1
134
- for (const attr of attrs) {
135
- switch (attr.type) {
136
- case 'style':
137
- const styles = addStyles(attr.value)
138
- const newClassNames = concatClassName(styles.map((x) => x.identifier).join(' '))
139
- if (shouldPrintDebug) {
140
- // console.log(' style', attr.value)
141
- console.log(' classnames', newClassNames, finalClassNames)
142
- }
143
- // prettier-ignore
144
- const existing = finalClassNames.find((x) => x.type == 'StringLiteral') as t.StringLiteral | null
145
- if (existing) {
146
- existing.value = `${existing.value} ${newClassNames}`
147
- } else {
148
- finalClassNames = [...finalClassNames, t.stringLiteral(newClassNames)]
149
- }
150
- if (shouldPrintDebug) {
151
- // prettier-ignore
152
- console.log(' classnames (after)', finalClassNames.map(x => x['value']).join(' '))
153
- }
154
- break
155
- case 'attr':
156
- const val = attr.value
157
- if (t.isJSXSpreadAttribute(val)) {
158
- if (isSimpleSpread(val)) {
159
- finalClassNames.push(
160
- t.logicalExpression(
161
- '&&',
162
- val.argument,
163
- t.memberExpression(val.argument, t.identifier('className'))
164
- )
165
- )
166
- }
167
- } else if (val.name.name === 'className') {
168
- const value = val.value
169
- if (value) {
170
- try {
171
- const evaluatedValue = attemptEval(value)
172
- finalClassNames.push(t.stringLiteral(evaluatedValue))
173
- } catch (e) {
174
- finalClassNames.push(value['expression'])
175
- }
176
- }
177
- continue
178
- }
179
- finalAttrs.push(val)
180
- break
181
- case 'ternary':
182
- const mediaExtraction = extractMediaStyle(
183
- attr.value,
184
- jsxPath,
185
- extractor.getTamagui(),
186
- sourcePath,
187
- lastMediaImportance,
188
- shouldPrintDebug
189
- )
190
- if (mediaExtraction) {
191
- lastMediaImportance++
192
- finalStyles = [...finalStyles, ...mediaExtraction.mediaStyles]
193
- finalClassNames = [
194
- ...finalClassNames,
195
- ...mediaExtraction.mediaStyles.map((x) => t.stringLiteral(x.identifier)),
196
- ]
197
- if (!mediaExtraction.ternaryWithoutMedia) {
198
- continue
199
- }
200
- }
201
- const ternary = mediaExtraction?.ternaryWithoutMedia || attr.value
202
- const consInfo = addStyles(ternary.consequent)
203
- const altInfo = addStyles(ternary.alternate)
204
- const cCN = consInfo.map((x) => x.identifier).join(' ')
205
- const aCN = altInfo.map((x) => x.identifier).join(' ')
206
- if (consInfo.length && altInfo.length) {
207
- finalClassNames.push(
208
- t.conditionalExpression(ternary.test, t.stringLiteral(cCN), t.stringLiteral(aCN))
209
- )
210
- } else {
211
- finalClassNames.push(
212
- t.conditionalExpression(
213
- ternary.test,
214
- t.stringLiteral(' ' + cCN),
215
- t.stringLiteral(' ' + aCN)
216
- )
217
- )
218
- }
219
- break
220
- }
221
- }
222
-
223
- node.attributes = finalAttrs
224
-
225
- if (finalClassNames.length) {
226
- // inserts the _cn variable and uses it for className
227
- const names = buildClassName(finalClassNames)
228
- const nameExpr = names ? hoistClassNames(jsxPath, existingHoists, names) : null
229
- let expr = nameExpr
230
-
231
- // if has some spreads, use concat helper
232
- if (nameExpr && !t.isIdentifier(nameExpr)) {
233
- ensureImportingConcat(programPath)
234
- const simpleSpreads = attrs.filter(
235
- (x) => t.isJSXSpreadAttribute(x.value) && isSimpleSpread(x.value)
236
- )
237
- expr = t.callExpression(t.identifier(CONCAT_CLASSNAME_IMPORT), [
238
- expr,
239
- ...simpleSpreads.map((val) => val.value['argument']),
240
- ])
241
- }
242
-
243
- node.attributes.push(
244
- t.jsxAttribute(t.jsxIdentifier('className'), t.jsxExpressionContainer(expr))
245
- )
246
- }
247
-
248
- const comment = util.format('/* %s:%s (%s) */', filePath, lineNumbers, originalNodeName)
249
-
250
- for (const { className, rules } of finalStyles) {
251
- if (cssMap.has(className)) {
252
- if (comment) {
253
- const val = cssMap.get(className)!
254
- val.commentTexts.push(comment)
255
- cssMap.set(className, val)
256
- }
257
- } else if (rules.length) {
258
- if (rules.length > 1) {
259
- console.log(' rules error', { rules })
260
- throw new Error(`Shouldn't have more than one rule`)
261
- }
262
- cssMap.set(className, {
263
- css: rules[0],
264
- commentTexts: [comment],
265
- })
266
- }
267
- }
268
- },
269
- })
270
-
271
- if (!res || !res.modified) {
272
- return null
273
- }
274
-
275
- const styles = Array.from(cssMap.values())
276
- .map((x) => x.css)
277
- .join('\n')
278
- .trim()
279
-
280
- if (styles) {
281
- const cssQuery = threaded
282
- ? `cssData=${Buffer.from(styles).toString('base64')}`
283
- : `cssPath=${cssPath}`
284
- const remReq = getRemainingRequest(loader)
285
- const importPath = `${cssPath}!=!tamagui-loader?${cssQuery}!${remReq}`
286
- ast.program.body.unshift(t.importDeclaration([], t.stringLiteral(importPath)))
287
- }
288
-
289
- const result = generate(
290
- ast,
291
- {
292
- concise: false,
293
- filename: sourcePath,
294
- retainLines: false,
295
- sourceFileName: sourcePath,
296
- sourceMaps: true,
297
- },
298
- source
299
- )
300
-
301
- if (shouldPrintDebug) {
302
- console.log(
303
- '\n -------- output code ------- \n\n',
304
- result.code
305
- .split('\n')
306
- .filter((x) => !x.startsWith('//'))
307
- .join('\n')
308
- )
309
- console.log('\n -------- output style -------- \n\n', styles)
310
- }
311
-
312
- if (shouldLogTiming && mem) {
313
- // console.log(`${parseTime} / ${traverseTime} / ${generateTime}`)
314
- const memUsed =
315
- Math.round(((process.memoryUsage().heapUsed - mem.heapUsed) / 1024 / 1204) * 10) / 10
316
- // prettier-ignore
317
- 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` : ''}`)
318
- }
319
-
320
- return {
321
- ast,
322
- styles,
323
- js: result.code,
324
- map: result.map,
325
- }
326
- }
@@ -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
- }