@tamagui/static 1.0.0-alpha.2 → 1.0.0-alpha.20

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 (187) hide show
  1. package/dist/cjs/constants.js +33 -0
  2. package/dist/cjs/constants.js.map +7 -0
  3. package/dist/cjs/extractor/accessSafe.js +33 -0
  4. package/dist/cjs/extractor/accessSafe.js.map +7 -0
  5. package/dist/cjs/extractor/babelParse.js +52 -0
  6. package/dist/cjs/extractor/babelParse.js.map +7 -0
  7. package/dist/cjs/extractor/buildClassName.js +62 -0
  8. package/dist/cjs/extractor/buildClassName.js.map +7 -0
  9. package/dist/cjs/extractor/createEvaluator.js +73 -0
  10. package/dist/cjs/extractor/createEvaluator.js.map +7 -0
  11. package/dist/cjs/extractor/createExtractor.js +903 -0
  12. package/dist/cjs/extractor/createExtractor.js.map +7 -0
  13. package/dist/cjs/extractor/ensureImportingConcat.js +46 -0
  14. package/dist/cjs/extractor/ensureImportingConcat.js.map +7 -0
  15. package/dist/cjs/extractor/evaluateAstNode.js +116 -0
  16. package/dist/cjs/extractor/evaluateAstNode.js.map +7 -0
  17. package/dist/cjs/extractor/extractHelpers.js +118 -0
  18. package/dist/cjs/extractor/extractHelpers.js.map +7 -0
  19. package/dist/cjs/extractor/extractMediaStyle.js +187 -0
  20. package/dist/cjs/extractor/extractMediaStyle.js.map +7 -0
  21. package/dist/cjs/extractor/extractToClassNames.js +279 -0
  22. package/dist/cjs/extractor/extractToClassNames.js.map +7 -0
  23. package/dist/cjs/extractor/findTopmostFunction.js +29 -0
  24. package/dist/cjs/extractor/findTopmostFunction.js.map +7 -0
  25. package/dist/cjs/extractor/generatedUid.js +50 -0
  26. package/dist/cjs/extractor/generatedUid.js.map +7 -0
  27. package/dist/cjs/extractor/getPropValueFromAttributes.js +71 -0
  28. package/dist/cjs/extractor/getPropValueFromAttributes.js.map +7 -0
  29. package/dist/cjs/extractor/getSourceModule.js +91 -0
  30. package/dist/cjs/extractor/getSourceModule.js.map +7 -0
  31. package/dist/cjs/extractor/getStaticBindingsForScope.js +150 -0
  32. package/dist/cjs/extractor/getStaticBindingsForScope.js.map +7 -0
  33. package/dist/cjs/extractor/hoistClassNames.js +66 -0
  34. package/dist/cjs/extractor/hoistClassNames.js.map +7 -0
  35. package/dist/cjs/extractor/literalToAst.js +56 -0
  36. package/dist/cjs/extractor/literalToAst.js.map +7 -0
  37. package/dist/cjs/extractor/loadTamagui.js +63 -0
  38. package/dist/cjs/extractor/loadTamagui.js.map +7 -0
  39. package/dist/cjs/extractor/logLines.js +23 -0
  40. package/dist/cjs/extractor/logLines.js.map +7 -0
  41. package/dist/cjs/extractor/normalizeTernaries.js +102 -0
  42. package/dist/cjs/extractor/normalizeTernaries.js.map +7 -0
  43. package/dist/cjs/extractor/removeUnusedHooks.js +101 -0
  44. package/dist/cjs/extractor/removeUnusedHooks.js.map +7 -0
  45. package/dist/cjs/index.js +37 -0
  46. package/dist/cjs/index.js.map +7 -0
  47. package/dist/cjs/patchReactNativeWeb.js +129 -0
  48. package/dist/cjs/patchReactNativeWeb.js.map +7 -0
  49. package/dist/cjs/types.js +4 -0
  50. package/dist/cjs/types.js.map +7 -0
  51. package/dist/{constants.js → esm/constants.js} +0 -0
  52. package/dist/{constants.js.map → esm/constants.js.map} +1 -1
  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 +0 -0
  60. package/dist/{extractor → esm/extractor}/createEvaluator.js.map +1 -1
  61. package/dist/{extractor → esm/extractor}/createExtractor.js +92 -54
  62. package/dist/esm/extractor/createExtractor.js.map +7 -0
  63. package/dist/{extractor → esm/extractor}/ensureImportingConcat.js +0 -0
  64. package/dist/{extractor → esm/extractor}/ensureImportingConcat.js.map +1 -1
  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 +3 -1
  68. package/dist/{extractor → esm/extractor}/extractHelpers.js.map +3 -3
  69. package/dist/{extractor → esm/extractor}/extractMediaStyle.js +0 -0
  70. package/dist/{extractor → esm/extractor}/extractMediaStyle.js.map +1 -1
  71. package/dist/{extractor → esm/extractor}/extractToClassNames.js +21 -24
  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 +0 -0
  88. package/dist/{extractor → esm/extractor}/loadTamagui.js.map +1 -1
  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 +0 -0
  92. package/dist/{extractor → esm/extractor}/normalizeTernaries.js.map +1 -1
  93. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js +0 -0
  94. package/dist/{extractor → esm/extractor}/removeUnusedHooks.js.map +1 -1
  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} +18 -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 +9 -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 +839 -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 +93 -0
  110. package/dist/jsx/extractor/extractMediaStyle.js +150 -0
  111. package/dist/jsx/extractor/extractToClassNames.js +239 -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 +40 -0
  120. package/dist/jsx/extractor/logLines.js +16 -0
  121. package/dist/jsx/extractor/normalizeTernaries.js +51 -0
  122. package/dist/jsx/extractor/removeUnusedHooks.js +77 -0
  123. package/dist/jsx/index.js +13 -0
  124. package/dist/jsx/patchReactNativeWeb.js +106 -0
  125. package/dist/jsx/types.js +0 -0
  126. package/package.json +19 -15
  127. package/src/extractor/createExtractor.ts +114 -77
  128. package/src/extractor/extractHelpers.ts +3 -2
  129. package/src/extractor/extractToClassNames.ts +21 -27
  130. package/src/extractor/logLines.ts +11 -0
  131. package/src/patchReactNativeWeb.ts +21 -5
  132. package/src/types.ts +13 -3
  133. package/types/constants.d.ts +4 -0
  134. package/types/constants.d.ts.map +1 -0
  135. package/types/extractor/accessSafe.d.ts +3 -0
  136. package/types/extractor/accessSafe.d.ts.map +1 -0
  137. package/types/extractor/babelParse.d.ts +5 -0
  138. package/types/extractor/babelParse.d.ts.map +1 -0
  139. package/types/extractor/buildClassName.d.ts +4 -0
  140. package/types/extractor/buildClassName.d.ts.map +1 -0
  141. package/types/extractor/createEvaluator.d.ts +12 -0
  142. package/types/extractor/createEvaluator.d.ts.map +1 -0
  143. package/types/extractor/createExtractor.d.ts +14 -0
  144. package/types/extractor/createExtractor.d.ts.map +1 -0
  145. package/types/extractor/ensureImportingConcat.d.ts +4 -0
  146. package/types/extractor/ensureImportingConcat.d.ts.map +1 -0
  147. package/types/extractor/evaluateAstNode.d.ts +3 -0
  148. package/types/extractor/evaluateAstNode.d.ts.map +1 -0
  149. package/types/extractor/extractHelpers.d.ts +12 -0
  150. package/types/extractor/extractHelpers.d.ts.map +1 -0
  151. package/types/extractor/extractMediaStyle.d.ts +10 -0
  152. package/types/extractor/extractMediaStyle.d.ts.map +1 -0
  153. package/types/extractor/extractToClassNames.d.ts +22 -0
  154. package/types/extractor/extractToClassNames.d.ts.map +1 -0
  155. package/types/extractor/findTopmostFunction.d.ts +4 -0
  156. package/types/extractor/findTopmostFunction.d.ts.map +1 -0
  157. package/types/extractor/generatedUid.d.ts +5 -0
  158. package/types/extractor/generatedUid.d.ts.map +1 -0
  159. package/types/extractor/getPropValueFromAttributes.d.ts +3 -0
  160. package/types/extractor/getPropValueFromAttributes.d.ts.map +1 -0
  161. package/types/extractor/getSourceModule.d.ts +16 -0
  162. package/types/extractor/getSourceModule.d.ts.map +1 -0
  163. package/types/extractor/getStaticBindingsForScope.d.ts +4 -0
  164. package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -0
  165. package/types/extractor/hoistClassNames.d.ts +6 -0
  166. package/types/extractor/hoistClassNames.d.ts.map +1 -0
  167. package/types/extractor/literalToAst.d.ts +3 -0
  168. package/types/extractor/literalToAst.d.ts.map +1 -0
  169. package/types/extractor/loadTamagui.d.ts +9 -0
  170. package/types/extractor/loadTamagui.d.ts.map +1 -0
  171. package/types/extractor/logLines.d.ts +2 -0
  172. package/types/extractor/logLines.d.ts.map +1 -0
  173. package/types/extractor/normalizeTernaries.d.ts +3 -0
  174. package/types/extractor/normalizeTernaries.d.ts.map +1 -0
  175. package/types/extractor/removeUnusedHooks.d.ts +3 -0
  176. package/types/extractor/removeUnusedHooks.d.ts.map +1 -0
  177. package/types/index.d.ts +8 -0
  178. package/types/index.d.ts.map +1 -0
  179. package/types/patchReactNativeWeb.d.ts +2 -0
  180. package/types/patchReactNativeWeb.d.ts.map +1 -0
  181. package/types/types.d.ts +79 -0
  182. package/types/types.d.ts.map +1 -0
  183. package/dist/extractor/createExtractor.js.map +0 -7
  184. package/dist/extractor/extractToClassNames.js.map +0 -7
  185. package/dist/index.cjs +0 -2080
  186. package/dist/index.cjs.map +0 -7
  187. package/dist/patchReactNativeWeb.js.map +0 -7
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@tamagui/static",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.20",
4
4
  "source": "src/index.ts",
5
- "main": "dist/index.cjs",
6
- "module": "dist/index.js",
7
- "typings": "types.d.ts",
5
+ "typings": "types",
6
+ "main": "dist/cjs",
7
+ "module": "dist/esm",
8
+ "module:jsx": "dist/jsx",
8
9
  "files": [
9
- "dist",
10
- "src"
10
+ "types",
11
+ "src",
12
+ "dist"
11
13
  ],
12
14
  "scripts": {
13
15
  "build": "tamagui-build",
@@ -34,10 +36,12 @@
34
36
  "@types/webpack": "^4.41.26",
35
37
  "babel-loader": "^8.2.2",
36
38
  "css-loader": "^5.2.4",
39
+ "esbuild-loader": "^2.16.0",
37
40
  "jest": "^27.3.1",
38
41
  "loader-utils": "^2.0.0",
39
42
  "react": "*",
40
43
  "react-dom": "*",
44
+ "react-native-web": "^0.17.5",
41
45
  "react-test-renderer": "18.0.0-beta-149b420f6-20211119",
42
46
  "style-loader": "^3.3.0",
43
47
  "typescript": "^4.4.4",
@@ -51,22 +55,22 @@
51
55
  "@babel/traverse": "^7.15.4",
52
56
  "@dish/babel-preset": "^0.0.6",
53
57
  "@expo/match-media": "^0.1.0",
54
- "@tamagui/build": "^1.0.0-alpha.2",
55
- "@tamagui/core": "^1.0.0-alpha.2",
56
- "@tamagui/core-node": "^1.0.0-alpha.2",
57
- "@tamagui/fake-react-native": "^1.0.0-alpha.2",
58
- "@tamagui/helpers": "^1.0.0-alpha.2",
58
+ "@tamagui/build": "^1.0.0-alpha.17",
59
+ "@tamagui/core": "^1.0.0-alpha.20",
60
+ "@tamagui/core-node": "^1.0.0-alpha.17",
61
+ "@tamagui/fake-react-native": "^1.0.0-alpha.17",
62
+ "@tamagui/helpers": "^1.0.0-alpha.17",
59
63
  "babel-literal-to-ast": "^2.1.0",
60
64
  "esbuild": "^0.13.12",
61
65
  "esbuild-register": "^3.1.2",
62
66
  "find-cache-dir": "^3.3.1",
63
- "fs-extra": "^9.0.0",
67
+ "fs-extra": "^9.1.0",
64
68
  "invariant": "^2.2.4",
65
- "lodash": "^4.17.20",
66
- "tamagui": "^1.0.0-alpha.2"
69
+ "lodash": "^4.17.21",
70
+ "tamagui": "^1.0.0-alpha.20"
67
71
  },
68
72
  "peerDependencies": {
69
73
  "react-native-web": "*"
70
74
  },
71
- "gitHead": "a9f30fdf4ebd448961a6e133741cd4b903185559"
75
+ "gitHead": "ddc994b04b098548fc81daf8933aea60de20101c"
72
76
  }
@@ -13,6 +13,7 @@ import { findTopmostFunction } from './findTopmostFunction'
13
13
  import { getStaticBindingsForScope } from './getStaticBindingsForScope'
14
14
  import { literalToAst } from './literalToAst'
15
15
  import { loadTamagui } from './loadTamagui'
16
+ import { logLines } from './logLines'
16
17
  import { normalizeTernaries } from './normalizeTernaries'
17
18
  import { removeUnusedHooks } from './removeUnusedHooks'
18
19
 
@@ -51,9 +52,10 @@ export function createExtractor() {
51
52
  })
52
53
 
53
54
  let loadedTamaguiConfig: TamaguiInternalConfig
55
+ let hasLogged = false
54
56
 
55
57
  return {
56
- getTamaguiConfig() {
58
+ getTamagui() {
57
59
  return loadedTamaguiConfig
58
60
  },
59
61
  parse: (
@@ -66,7 +68,10 @@ export function createExtractor() {
66
68
  sourcePath = '',
67
69
  onExtractTag,
68
70
  getFlattenedNode,
69
- onDidFlatten,
71
+ disableExtraction,
72
+ disableExtractInlineMedia,
73
+ disableExtractVariables,
74
+ disableDebugAttr,
70
75
  ...props
71
76
  }: ExtractorParseProps
72
77
  ) => {
@@ -87,27 +92,25 @@ export function createExtractor() {
87
92
  loadedTamaguiConfig = tamaguiConfig
88
93
 
89
94
  const defaultTheme = tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]]
90
-
91
- // TODO this can be passed in / set based on if source changed
92
- // const shouldReCheckTheme = false //Date.now() - hasParsedFileLast > 600
93
- // hasParsedFileLast = Date.now()
94
-
95
- let doesUseValidImport = false
96
95
  const body = fileOrPath.type === 'Program' ? fileOrPath.get('body') : fileOrPath.program.body
97
96
 
98
97
  /**
99
98
  * Step 1: Determine if importing any statically extractable components
100
99
  */
101
- const isInternalImport = (importStr: string) =>
102
- isInsideTamagui(sourcePath) && importStr[0] === '.'
100
+ const isInternalImport = (importStr: string) => {
101
+ return isInsideTamagui(sourcePath) && importStr[0] === '.'
102
+ }
103
103
 
104
104
  const validComponents: { [key: string]: any } = Object.keys(components)
105
- .filter((key) => !!components[key]?.staticConfig)
105
+ // check if uppercase to avoid hitting media query proxy before init
106
+ .filter((key) => key[0].toUpperCase() === key[0] && !!components[key]?.staticConfig)
106
107
  .reduce((obj, name) => {
107
108
  obj[name] = components[name]
108
109
  return obj
109
110
  }, {})
110
111
 
112
+ let doesUseValidImport = false
113
+
111
114
  for (const bodyPath of body) {
112
115
  if (bodyPath.type !== 'ImportDeclaration') continue
113
116
  const node = ('node' in bodyPath ? bodyPath.node : bodyPath) as any
@@ -119,7 +122,6 @@ export function createExtractor() {
119
122
  return validComponents[name] || validHooks[name]
120
123
  })
121
124
  ) {
122
- console.log('WHAT')
123
125
  doesUseValidImport = true
124
126
  break
125
127
  }
@@ -149,6 +151,12 @@ export function createExtractor() {
149
151
  */
150
152
  let programPath: NodePath<t.Program>
151
153
 
154
+ const res = {
155
+ flattened: 0,
156
+ optimized: 0,
157
+ modified: 0,
158
+ }
159
+
152
160
  callTraverse({
153
161
  Program: {
154
162
  enter(path) {
@@ -187,8 +195,41 @@ export function createExtractor() {
187
195
  return
188
196
  }
189
197
 
190
- const { staticConfig } = component
191
198
  const originalNodeName = node.name.name
199
+
200
+ if (shouldPrintDebug) {
201
+ console.log(`\n<${originalNodeName} />`)
202
+ }
203
+
204
+ const filePath = sourcePath.replace(process.cwd(), '.')
205
+ const lineNumbers = node.loc
206
+ ? node.loc.start.line +
207
+ (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : '')
208
+ : ''
209
+
210
+ // add data-is
211
+ if (shouldAddDebugProp && !disableDebugAttr) {
212
+ const preName = componentName ? `${componentName}:` : ''
213
+ res.modified++
214
+ node.attributes.unshift(
215
+ t.jsxAttribute(
216
+ t.jsxIdentifier('data-is'),
217
+ t.stringLiteral(
218
+ ` ${preName}${node.name.name} ${filePath.replace('./', '')}:${lineNumbers} `
219
+ )
220
+ )
221
+ )
222
+ }
223
+
224
+ if (disableExtraction) {
225
+ if (!hasLogged) {
226
+ console.log('🥚 Tamagui disableExtraction set: no CSS or optimizations will be run')
227
+ hasLogged = true
228
+ }
229
+ return
230
+ }
231
+
232
+ const { staticConfig } = component
192
233
  const isTextView = staticConfig.isText || false
193
234
  const validStyles = staticConfig?.validStyles ?? {}
194
235
 
@@ -225,10 +266,6 @@ export function createExtractor() {
225
266
  return res
226
267
  }
227
268
 
228
- if (shouldPrintDebug) {
229
- console.log(`\n<${originalNodeName} />`)
230
- }
231
-
232
269
  // Generate scope object at this level
233
270
  const staticNamespace = getStaticBindingsForScope(
234
271
  traversePath.scope,
@@ -401,9 +438,9 @@ export function createExtractor() {
401
438
  }
402
439
  const propName = prop.key['name']
403
440
  if (!isStaticAttributeName(propName) && propName !== 'tag') {
404
- // if (shouldPrintDebug) {
405
- console.log(' not a valid style prop!', propName)
406
- // }
441
+ if (shouldPrintDebug) {
442
+ console.log(' not a valid style prop!', propName)
443
+ }
407
444
  return false
408
445
  }
409
446
  return true
@@ -491,7 +528,9 @@ export function createExtractor() {
491
528
  const [test, alt, cons] = conditional
492
529
  if (!test) throw new Error(`no test`)
493
530
  if ([alt, cons].some((side) => side && !isExtractable(side))) {
494
- console.log('not extractable', alt, cons)
531
+ if (shouldPrintDebug) {
532
+ console.log('not extractable', alt, cons)
533
+ }
495
534
  return attr
496
535
  }
497
536
  // split into individual ternaries per object property
@@ -541,6 +580,11 @@ export function createExtractor() {
541
580
 
542
581
  // shorthand media queries
543
582
  if (name[0] === '$' && t.isJSXExpressionContainer(attribute?.value)) {
583
+ // allow disabling this extraction
584
+ if (disableExtractInlineMedia) {
585
+ return attr
586
+ }
587
+
544
588
  const shortname = name.slice(1)
545
589
  if (mediaQueryConfig[shortname]) {
546
590
  const expression = attribute.value.expression
@@ -589,11 +633,24 @@ export function createExtractor() {
589
633
  }
590
634
  }
591
635
 
636
+ // native shouldn't extract variables
637
+ if (disableExtractVariables) {
638
+ if (value) {
639
+ if (value.type === 'StringLiteral' && value.value[0] === '$') {
640
+ if (shouldPrintDebug) {
641
+ console.log(' native, disable extract var', value.value)
642
+ }
643
+ inlinePropCount++
644
+ return attr
645
+ }
646
+ }
647
+ }
648
+
592
649
  // if value can be evaluated, extract it and filter it out
593
650
  const styleValue = attemptEvalSafe(value)
594
651
 
595
- // we never flatten if a prop isn't a valid static attribute
596
- // but we need to make sure its post-prop mapping
652
+ // never flatten if a prop isn't a valid static attribute
653
+ // only post prop-mapping
597
654
  if (!isStaticAttributeName(name)) {
598
655
  let keys = [name]
599
656
  if (staticConfig.propMapper) {
@@ -623,6 +680,8 @@ export function createExtractor() {
623
680
  return {
624
681
  type: 'style',
625
682
  value: { [name]: styleValue },
683
+ name,
684
+ attr: path.node,
626
685
  }
627
686
  }
628
687
 
@@ -754,7 +813,7 @@ export function createExtractor() {
754
813
 
755
814
  // see if we can filter them
756
815
  if (shouldPrintDebug) {
757
- console.log(' - attrs (before): ', attrs.map(attrStr).join(', '))
816
+ console.log(' - attrs (before):\n', logLines(attrs.map(attrStr).join(', ')))
758
817
  }
759
818
 
760
819
  // now update to new values
@@ -774,12 +833,6 @@ export function createExtractor() {
774
833
  modifiedComponents.add(parentFn)
775
834
  }
776
835
 
777
- const filePath = sourcePath.replace(process.cwd(), '.')
778
- const lineNumbers = node.loc
779
- ? node.loc.start.line +
780
- (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : '')
781
- : ''
782
-
783
836
  // combine ternaries
784
837
  let ternaries: Ternary[] = []
785
838
  attrs = attrs
@@ -869,29 +922,27 @@ export function createExtractor() {
869
922
  if (cur.type === 'style') {
870
923
  // TODO need to loop over initial props not just style props
871
924
  for (const key in cur.value) {
872
- const shouldInsertNull =
873
- !!(
874
- staticConfig.ensureOverriddenProp?.[key] // || staticConfig.defaultProps?.[key]
875
- )
925
+ const shouldEnsureOverridden = !!staticConfig.ensureOverriddenProp?.[key]
876
926
  const isSetInAttrsAlready = attrs.some(
877
927
  (x) =>
878
928
  x.type === 'attr' &&
879
929
  x.value.type === 'JSXAttribute' &&
880
930
  x.value.name.name === key
881
931
  )
882
- const shouldInsertNullOverride = shouldInsertNull && !isSetInAttrsAlready
883
- // if (shouldPrintDebug) {
884
- // // prettier-ignore
885
- // console.log('what is', key, { shouldInsertNullOverride, shouldInsertNull, isSetInAttrsAlready })
886
- // }
887
- if (shouldInsertNullOverride) {
888
- acc.push({
889
- type: 'attr',
890
- value: t.jsxAttribute(
891
- t.jsxIdentifier(key),
892
- t.jsxExpressionContainer(t.nullLiteral())
893
- ),
894
- })
932
+
933
+ if (!isSetInAttrsAlready) {
934
+ const isVariant = !!staticConfig.variants?.[cur.name || '']
935
+ if (isVariant || shouldEnsureOverridden) {
936
+ acc.push({
937
+ type: 'attr',
938
+ value:
939
+ cur.attr ||
940
+ t.jsxAttribute(
941
+ t.jsxIdentifier(key),
942
+ t.jsxExpressionContainer(t.nullLiteral())
943
+ ),
944
+ })
945
+ }
895
946
  }
896
947
  }
897
948
  }
@@ -901,7 +952,7 @@ export function createExtractor() {
901
952
  }
902
953
 
903
954
  if (shouldPrintDebug) {
904
- console.log(' - attrs (flattened): ', attrs.map(attrStr).join(', '))
955
+ console.log(' - attrs (flattened): \n', logLines(attrs.map(attrStr).join(', ')))
905
956
  }
906
957
 
907
958
  // evaluate away purely style props
@@ -927,7 +978,7 @@ export function createExtractor() {
927
978
  }, [])
928
979
 
929
980
  if (shouldPrintDebug) {
930
- console.log(' - attrs (evaluated styles): ', attrs.map(attrStr).join(', '))
981
+ console.log(' - attrs (evaluated styles): \n', logLines(attrs.map(attrStr).join(', ')))
931
982
  }
932
983
 
933
984
  // combine styles, leave undefined values
@@ -945,7 +996,7 @@ export function createExtractor() {
945
996
  }, [])
946
997
 
947
998
  if (shouldPrintDebug) {
948
- console.log(' - attrs (combined 🔀): ', attrs.map(attrStr).join(', '))
999
+ console.log(' - attrs (combined 🔀): \n', logLines(attrs.map(attrStr).join(', ')))
949
1000
  }
950
1001
 
951
1002
  // post process
@@ -960,9 +1011,10 @@ export function createExtractor() {
960
1011
  const out = postProcessStyles(props, staticConfig, defaultTheme)
961
1012
  const next = out?.style ?? props
962
1013
  if (shouldPrintDebug) {
963
- console.log(' >> getStyles: ', objToStr(props), '==>>', objToStr(next))
964
- console.log(' >> style: ', objToStr(out.style))
965
- console.log(' >> viewp: ', objToStr(out.viewProps))
1014
+ console.log(' getStyles props >>\n', logLines(objToStr(props)))
1015
+ console.log(' getStyles next >>\n', logLines(objToStr(next)))
1016
+ console.log(' getStyles style >>\n', logLines(objToStr(out.style)))
1017
+ console.log(' getStyles viewp >>\n', logLines(objToStr(out.viewProps)))
966
1018
  }
967
1019
  if (staticConfig.validStyles) {
968
1020
  for (const key in next) {
@@ -974,10 +1026,6 @@ export function createExtractor() {
974
1026
  return next
975
1027
  }
976
1028
 
977
- if (shouldPrintDebug) {
978
- console.log(' staticConfig.defaultProps', staticConfig.defaultProps)
979
- }
980
-
981
1029
  // used to ensure we pass the entire prop bundle to getStyles
982
1030
  const completeStylesProcessed = getStyles({
983
1031
  ...staticConfig.defaultProps,
@@ -1006,9 +1054,9 @@ export function createExtractor() {
1006
1054
 
1007
1055
  if (shouldPrintDebug) {
1008
1056
  // prettier-ignore
1009
- console.log(' completeStaticProps', objToStr(completeStaticProps))
1057
+ console.log(' completeStaticProps\n', logLines(objToStr(completeStaticProps)))
1010
1058
  // prettier-ignore
1011
- console.log(' completeStylesProcessed', objToStr(completeStylesProcessed))
1059
+ console.log(' completeStylesProcessed\n', logLines(objToStr(completeStylesProcessed)))
1012
1060
  }
1013
1061
 
1014
1062
  for (const attr of attrs) {
@@ -1047,22 +1095,7 @@ export function createExtractor() {
1047
1095
  }
1048
1096
 
1049
1097
  if (shouldPrintDebug) {
1050
- console.log(' - attrs (after): ', attrs.map(attrStr).join(', '))
1051
- }
1052
-
1053
- // add data-is
1054
- if (shouldAddDebugProp) {
1055
- const preName = componentName ? `${componentName}:` : ''
1056
- // unshift so spreads/nesting overwrite
1057
- attrs.unshift({
1058
- type: 'attr',
1059
- value: t.jsxAttribute(
1060
- t.jsxIdentifier('data-is'),
1061
- t.stringLiteral(
1062
- ` ${preName}${node.name.name} ${filePath.replace('./', '')}:${lineNumbers} `
1063
- )
1064
- ),
1065
- })
1098
+ console.log(' - attrs (after):\n', logLines(attrs.map(attrStr).join(', ')))
1066
1099
  }
1067
1100
 
1068
1101
  if (shouldFlatten) {
@@ -1072,7 +1105,7 @@ export function createExtractor() {
1072
1105
  }
1073
1106
  isFlattened = true
1074
1107
  node.name.name = flatNode
1075
- onDidFlatten?.()
1108
+ res.flattened++
1076
1109
  if (closingElement) {
1077
1110
  closingElement.name.name = flatNode
1078
1111
  }
@@ -1081,9 +1114,11 @@ export function createExtractor() {
1081
1114
  if (shouldPrintDebug) {
1082
1115
  // prettier-ignore
1083
1116
  console.log(' [❊] inline props ', inlinePropCount, shouldDeopt ? ' deopted' : '', hasSpread ? ' spread' : '', '!flatten', staticConfig.neverFlatten)
1084
- console.log(' - attrs (end): ', attrs.map(attrStr).join(', '))
1117
+ console.log(' - attrs (end):\n', logLines(attrs.map(attrStr).join(', ')))
1085
1118
  }
1086
1119
 
1120
+ res.optimized++
1121
+
1087
1122
  onExtractTag({
1088
1123
  attrs,
1089
1124
  node,
@@ -1110,6 +1145,8 @@ export function createExtractor() {
1110
1145
  removeUnusedHooks(comp, shouldPrintDebug)
1111
1146
  }
1112
1147
  }
1148
+
1149
+ return res
1113
1150
  },
1114
1151
  }
1115
1152
  }
@@ -97,5 +97,6 @@ export function isValidThemeHook(
97
97
  return true
98
98
  }
99
99
 
100
- export const isInsideTamagui = (srcName: string) =>
101
- srcName.includes('/tamagui/src') || srcName.includes('/core/src')
100
+ export const isInsideTamagui = (srcName: string) => {
101
+ return srcName.includes('/dist/jsx') || srcName.includes('/core/src')
102
+ }
@@ -18,6 +18,7 @@ import { ensureImportingConcat } from './ensureImportingConcat'
18
18
  import { isSimpleSpread } from './extractHelpers'
19
19
  import { extractMediaStyle } from './extractMediaStyle'
20
20
  import { hoistClassNames } from './hoistClassNames'
21
+ import { logLines } from './logLines'
21
22
 
22
23
  export const CONCAT_CLASSNAME_IMPORT = 'concatClassName'
23
24
 
@@ -61,7 +62,7 @@ export function extractToClassNames({
61
62
  '`sourcePath` must be an absolute path to a .js file'
62
63
  )
63
64
 
64
- const shouldLogTiming = shouldPrintDebug || options.logTimings
65
+ const shouldLogTiming = options.logTimings ?? true
65
66
  const start = Date.now()
66
67
  const mem = shouldLogTiming ? process.memoryUsage() : null
67
68
 
@@ -79,17 +80,11 @@ export function extractToClassNames({
79
80
  const cssMap = new Map<string, { css: string; commentTexts: string[] }>()
80
81
  const existingHoists: { [key: string]: t.Identifier } = {}
81
82
 
82
- let flattened = 0
83
- let optimized = 0
84
-
85
- extractor.parse(ast, {
83
+ const res = extractor.parse(ast, {
86
84
  sourcePath,
87
85
  shouldPrintDebug,
88
86
  ...options,
89
87
  getFlattenedNode: ({ tag }) => tag,
90
- onDidFlatten() {
91
- flattened++
92
- },
93
88
  onExtractTag: ({
94
89
  attrs,
95
90
  node,
@@ -100,8 +95,6 @@ export function extractToClassNames({
100
95
  lineNumbers,
101
96
  programPath,
102
97
  }) => {
103
- optimized++
104
-
105
98
  let finalClassNames: ClassNameObject[] = []
106
99
  let finalAttrs: (t.JSXAttribute | t.JSXSpreadAttribute)[] = []
107
100
  let finalStyles: StyleObject[] = []
@@ -144,10 +137,6 @@ export function extractToClassNames({
144
137
  case 'style':
145
138
  const styles = addStyles(attr.value)
146
139
  const newClassNames = concatClassName(styles.map((x) => x.identifier).join(' '))
147
- if (shouldPrintDebug) {
148
- // console.log(' style', attr.value)
149
- console.log(' classnames', newClassNames, finalClassNames)
150
- }
151
140
  // prettier-ignore
152
141
  const existing = finalClassNames.find((x) => x.type == 'StringLiteral') as t.StringLiteral | null
153
142
  if (existing) {
@@ -157,7 +146,7 @@ export function extractToClassNames({
157
146
  }
158
147
  if (shouldPrintDebug) {
159
148
  // prettier-ignore
160
- console.log(' classnames (after)', finalClassNames.map(x => x['value']).join(' '))
149
+ console.log(' classnames (after)\n', logLines(finalClassNames.map(x => x['value']).join(' ')))
161
150
  }
162
151
  break
163
152
  case 'attr':
@@ -190,7 +179,7 @@ export function extractToClassNames({
190
179
  const mediaExtraction = extractMediaStyle(
191
180
  attr.value,
192
181
  jsxPath,
193
- extractor.getTamaguiConfig(),
182
+ extractor.getTamagui(),
194
183
  sourcePath,
195
184
  lastMediaImportance,
196
185
  shouldPrintDebug
@@ -276,15 +265,15 @@ export function extractToClassNames({
276
265
  },
277
266
  })
278
267
 
279
- if (!optimized) {
268
+ if (!res || (!res.modified && !res.optimized && !res.flattened)) {
269
+ if (shouldPrintDebug) {
270
+ console.log('no res or none modified', res)
271
+ }
280
272
  return null
281
273
  }
282
274
 
283
275
  const styles = Array.from(cssMap.values())
284
- .map((x) => {
285
- // remove comments
286
- return x.css //shouldInternalDedupe ? x.css : `${x.commentTexts.join('\n')}\n${x.css}`
287
- })
276
+ .map((x) => x.css)
288
277
  .join('\n')
289
278
  .trim()
290
279
 
@@ -320,12 +309,17 @@ export function extractToClassNames({
320
309
  console.log('\n -------- output style -------- \n\n', styles)
321
310
  }
322
311
 
323
- if (shouldLogTiming && mem) {
324
- // console.log(`${parseTime} / ${traverseTime} / ${generateTime}`)
325
- const memUsed =
326
- Math.round(((process.memoryUsage().heapUsed - mem.heapUsed) / 1024 / 1204) * 10) / 10
327
- // prettier-ignore
328
- console.log(` 🥚 ${basename(sourcePath).padStart(40)} ${`${Date.now() - start}`.padStart(3)}ms ׁ· ${`${optimized}`.padStart(4)} optimized · ${flattened} flattened ${memUsed > 10 ? `used ${memUsed}MB` : ''}`)
312
+ if (shouldLogTiming) {
313
+ const memUsed = mem
314
+ ? Math.round(((process.memoryUsage().heapUsed - mem.heapUsed) / 1024 / 1204) * 10) / 10
315
+ : 0
316
+ const timing = `${Date.now() - start}`.padStart(3)
317
+ const path = basename(sourcePath).padStart(40)
318
+ const numOptimized = `${res.optimized}`.padStart(4)
319
+ const memory = memUsed > 10 ? `used ${memUsed}MB` : ''
320
+ console.log(
321
+ ` 🥚 ${path} ${timing}ms ׁ· ${numOptimized} optimized · ${res.flattened} flattened ${memory}`
322
+ )
329
323
  }
330
324
 
331
325
  return {
@@ -0,0 +1,11 @@
1
+ export const logLines = (str: string) => {
2
+ let lines: string[] = ['']
3
+ const items = str.split(' ')
4
+ for (const item of items) {
5
+ if (item.length + lines[lines.length - 1].length > 100) {
6
+ lines.push('')
7
+ }
8
+ lines[lines.length - 1] += item + ' '
9
+ }
10
+ return lines.map((line, i) => ' ' + (i == 0 ? '' : ' ') + line.trim()).join('\n')
11
+ }
@@ -9,10 +9,28 @@ import path from 'path'
9
9
  // keep it sync
10
10
  export function patchReactNativeWeb() {
11
11
  const rootDir = require.resolve('react-native-web').replace(/\/dist.*/, '')
12
+ const modulePath = path.join(rootDir, 'dist', 'tamagui-exports.js')
13
+ const cjsPath = path.join(rootDir, 'dist', 'cjs', 'tamagui-exports.js')
12
14
 
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)
15
+ const isEqual = (() => {
16
+ let res = false
17
+ try {
18
+ res =
19
+ fs.readFileSync(modulePath, 'utf-8') === moduleExports &&
20
+ fs.readFileSync(cjsPath, 'utf-8') == cjsExports
21
+ } catch {
22
+ res = false
23
+ }
24
+ return res
25
+ })()
26
+
27
+ if (!isEqual) {
28
+ console.log('🥚 Tamagui patching react-native-web to share atomic styling primitives')
29
+ console.log(' > adding', modulePath)
30
+ console.log(' > adding', cjsPath)
31
+ fs.writeFileSync(modulePath, moduleExports)
32
+ fs.writeFileSync(cjsPath, cjsExports)
33
+ }
16
34
 
17
35
  // if entry files not patched, patch them:
18
36
  const moduleEntry = path.join(rootDir, 'dist', 'index.js')
@@ -41,8 +59,6 @@ exports.TamaguiExports = _interopRequireDefault(require("./tamagui-exports"));
41
59
  // tamagui-patch-end
42
60
  `
43
61
  )
44
-
45
- console.log(`Tamagui patched react-native-web`)
46
62
  }
47
63
  }
48
64
 
package/src/types.ts CHANGED
@@ -15,6 +15,9 @@ export interface TamaguiOptions {
15
15
  evaluateVars?: boolean
16
16
  importsWhitelist?: string[]
17
17
  disableExtraction?: boolean
18
+ disableDebugAttr?: boolean
19
+ disableExtractInlineMedia?: boolean
20
+ disableExtractVariables?: boolean
18
21
  exclude?: RegExp
19
22
  logTimings?: boolean
20
23
 
@@ -30,9 +33,17 @@ export type ExtractedAttrAttr = {
30
33
  value: t.JSXAttribute | t.JSXSpreadAttribute
31
34
  }
32
35
 
33
- export type ExtractedAttrStyle = { type: 'style'; value: Object }
36
+ export type ExtractedAttrStyle = {
37
+ type: 'style'
38
+ value: Object
39
+ attr?: t.JSXAttribute | t.JSXSpreadAttribute
40
+ name?: string
41
+ }
34
42
 
35
- export type ExtractedAttr = ExtractedAttrAttr | { type: 'ternary'; value: Ternary } | ExtractedAttrStyle
43
+ export type ExtractedAttr =
44
+ | ExtractedAttrAttr
45
+ | { type: 'ternary'; value: Ternary }
46
+ | ExtractedAttrStyle
36
47
 
37
48
  export type ExtractTagProps = {
38
49
  attrs: ExtractedAttr[]
@@ -51,7 +62,6 @@ export type ExtractorParseProps = TamaguiOptions & {
51
62
  shouldPrintDebug?: boolean
52
63
  onExtractTag: (props: ExtractTagProps) => void
53
64
  getFlattenedNode: (props: { isTextView: boolean; tag: string }) => string
54
- onDidFlatten?: () => void
55
65
  }
56
66
 
57
67
  export interface Ternary {