@tamagui/static 1.0.0-alpha.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 (76) hide show
  1. package/README.md +21 -0
  2. package/dist/constants.js +10 -0
  3. package/dist/constants.js.map +7 -0
  4. package/dist/extractor/accessSafe.js +11 -0
  5. package/dist/extractor/accessSafe.js.map +7 -0
  6. package/dist/extractor/babelParse.js +30 -0
  7. package/dist/extractor/babelParse.js.map +7 -0
  8. package/dist/extractor/buildClassName.js +40 -0
  9. package/dist/extractor/buildClassName.js.map +7 -0
  10. package/dist/extractor/createEvaluator.js +51 -0
  11. package/dist/extractor/createEvaluator.js.map +7 -0
  12. package/dist/extractor/createExtractor.js +801 -0
  13. package/dist/extractor/createExtractor.js.map +7 -0
  14. package/dist/extractor/ensureImportingConcat.js +24 -0
  15. package/dist/extractor/ensureImportingConcat.js.map +7 -0
  16. package/dist/extractor/evaluateAstNode.js +94 -0
  17. package/dist/extractor/evaluateAstNode.js.map +7 -0
  18. package/dist/extractor/extractHelpers.js +92 -0
  19. package/dist/extractor/extractHelpers.js.map +7 -0
  20. package/dist/extractor/extractMediaStyle.js +151 -0
  21. package/dist/extractor/extractMediaStyle.js.map +7 -0
  22. package/dist/extractor/extractToClassNames.js +243 -0
  23. package/dist/extractor/extractToClassNames.js.map +7 -0
  24. package/dist/extractor/findTopmostFunction.js +23 -0
  25. package/dist/extractor/findTopmostFunction.js.map +7 -0
  26. package/dist/extractor/generatedUid.js +28 -0
  27. package/dist/extractor/generatedUid.js.map +7 -0
  28. package/dist/extractor/getPropValueFromAttributes.js +49 -0
  29. package/dist/extractor/getPropValueFromAttributes.js.map +7 -0
  30. package/dist/extractor/getSourceModule.js +69 -0
  31. package/dist/extractor/getSourceModule.js.map +7 -0
  32. package/dist/extractor/getStaticBindingsForScope.js +128 -0
  33. package/dist/extractor/getStaticBindingsForScope.js.map +7 -0
  34. package/dist/extractor/hoistClassNames.js +44 -0
  35. package/dist/extractor/hoistClassNames.js.map +7 -0
  36. package/dist/extractor/literalToAst.js +34 -0
  37. package/dist/extractor/literalToAst.js.map +7 -0
  38. package/dist/extractor/loadTamagui.js +41 -0
  39. package/dist/extractor/loadTamagui.js.map +7 -0
  40. package/dist/extractor/normalizeTernaries.js +52 -0
  41. package/dist/extractor/normalizeTernaries.js.map +7 -0
  42. package/dist/extractor/removeUnusedHooks.js +78 -0
  43. package/dist/extractor/removeUnusedHooks.js.map +7 -0
  44. package/dist/index.cjs +2079 -0
  45. package/dist/index.cjs.map +7 -0
  46. package/dist/index.js +14 -0
  47. package/dist/index.js.map +7 -0
  48. package/dist/patchReactNativeWeb.js +92 -0
  49. package/dist/patchReactNativeWeb.js.map +7 -0
  50. package/dist/types.js +1 -0
  51. package/dist/types.js.map +7 -0
  52. package/package.json +72 -0
  53. package/src/constants.ts +10 -0
  54. package/src/extractor/accessSafe.ts +18 -0
  55. package/src/extractor/babelParse.ts +27 -0
  56. package/src/extractor/buildClassName.ts +61 -0
  57. package/src/extractor/createEvaluator.ts +68 -0
  58. package/src/extractor/createExtractor.ts +1113 -0
  59. package/src/extractor/ensureImportingConcat.ts +33 -0
  60. package/src/extractor/evaluateAstNode.ts +121 -0
  61. package/src/extractor/extractHelpers.ts +93 -0
  62. package/src/extractor/extractMediaStyle.ts +191 -0
  63. package/src/extractor/extractToClassNames.ts +337 -0
  64. package/src/extractor/findTopmostFunction.ts +22 -0
  65. package/src/extractor/generatedUid.ts +43 -0
  66. package/src/extractor/getPropValueFromAttributes.ts +92 -0
  67. package/src/extractor/getSourceModule.ts +101 -0
  68. package/src/extractor/getStaticBindingsForScope.ts +173 -0
  69. package/src/extractor/hoistClassNames.ts +45 -0
  70. package/src/extractor/literalToAst.ts +32 -0
  71. package/src/extractor/loadTamagui.ts +61 -0
  72. package/src/extractor/normalizeTernaries.ts +60 -0
  73. package/src/extractor/removeUnusedHooks.ts +76 -0
  74. package/src/index.ts +9 -0
  75. package/src/patchReactNativeWeb.ts +107 -0
  76. package/src/types.ts +80 -0
@@ -0,0 +1,61 @@
1
+ import * as t from '@babel/types'
2
+
3
+ import { ClassNameObject } from '../types'
4
+
5
+ export function buildClassName(
6
+ classNameObjects: ClassNameObject[]
7
+ ): t.Expression | t.StringLiteral | null {
8
+ return classNameObjects.reduce<t.Expression | null>((acc, val) => {
9
+ if (acc == null) {
10
+ if (
11
+ // pass conditional expressions through
12
+ t.isConditionalExpression(val) ||
13
+ // pass non-null literals through
14
+ t.isStringLiteral(val) ||
15
+ t.isNumericLiteral(val)
16
+ ) {
17
+ return val
18
+ }
19
+ return t.logicalExpression('||', val, t.stringLiteral(''))
20
+ }
21
+
22
+ let inner: t.Expression
23
+ if (t.isStringLiteral(val)) {
24
+ if (t.isStringLiteral(acc)) {
25
+ // join adjacent string literals
26
+ return t.stringLiteral(`${acc.value} ${val.value}`)
27
+ }
28
+ inner = t.stringLiteral(` ${val.value}`)
29
+ } else if (t.isLiteral(val)) {
30
+ inner = t.binaryExpression('+', t.stringLiteral(' '), val)
31
+ } else if (t.isConditionalExpression(val) || t.isBinaryExpression(val)) {
32
+ if (t.isStringLiteral(acc)) {
33
+ return t.binaryExpression('+', t.stringLiteral(`${acc.value} `), val)
34
+ }
35
+ inner = t.binaryExpression('+', t.stringLiteral(' '), val)
36
+ } else if (t.isIdentifier(val) || t.isMemberExpression(val)) {
37
+ // identifiers and member expressions make for reasonable ternaries
38
+ inner = t.conditionalExpression(
39
+ val,
40
+ t.binaryExpression('+', t.stringLiteral(' '), val),
41
+ t.stringLiteral('')
42
+ )
43
+ } else {
44
+ if (t.isStringLiteral(acc)) {
45
+ return t.binaryExpression(
46
+ '+',
47
+ t.stringLiteral(`${acc.value} `),
48
+ t.logicalExpression('||', val, t.stringLiteral(''))
49
+ )
50
+ }
51
+ // use a logical expression for more complex prop values
52
+ inner = t.binaryExpression(
53
+ '+',
54
+ t.stringLiteral(' '),
55
+ t.logicalExpression('||', val, t.stringLiteral(''))
56
+ )
57
+ }
58
+
59
+ return t.binaryExpression('+', acc, inner)
60
+ }, null)
61
+ }
@@ -0,0 +1,68 @@
1
+ import vm from 'vm'
2
+
3
+ import generate from '@babel/generator'
4
+ import { NodePath } from '@babel/traverse'
5
+ import * as t from '@babel/types'
6
+ import type { TamaguiConfig } from '@tamagui/core'
7
+
8
+ import { FAILED_EVAL } from './createExtractor'
9
+ import { evaluateAstNode } from './evaluateAstNode'
10
+ import { isValidThemeHook } from './extractHelpers'
11
+
12
+ export function createEvaluator({
13
+ tamaguiConfig,
14
+ staticNamespace,
15
+ sourcePath,
16
+ traversePath,
17
+ shouldPrintDebug,
18
+ }: {
19
+ tamaguiConfig: TamaguiConfig
20
+ staticNamespace: Record<string, any>
21
+ sourcePath: string
22
+ traversePath: NodePath<t.JSXElement>
23
+ shouldPrintDebug: boolean
24
+ }) {
25
+ // called when evaluateAstNode encounters a dynamic-looking prop
26
+ const evalFn = (n: t.Node) => {
27
+ // themes
28
+ if (
29
+ t.isMemberExpression(n) &&
30
+ t.isIdentifier(n.property) &&
31
+ isValidThemeHook(traversePath, n, sourcePath)
32
+ ) {
33
+ const key = n.property.name
34
+ if (shouldPrintDebug) {
35
+ console.log(' > found theme prop', key)
36
+ }
37
+ console.log('SHOULD FIND THEME (NESTED NOW)', key) // tamaguiConfig.themes)
38
+ // if (!themeKeys.has(key)) {
39
+ // throw new Error(` > accessing non-existent theme key: ${key}`)
40
+ // }
41
+ return `var(--${key})`
42
+ }
43
+ // variable
44
+ if (t.isIdentifier(n) && staticNamespace.hasOwnProperty(n.name)) {
45
+ return staticNamespace[n.name]
46
+ }
47
+ const evalContext = vm.createContext(staticNamespace)
48
+ const code = `(${generate(n as any).code})`
49
+ // if (shouldPrintDebug) {
50
+ // console.log('evaluating', { n, code, evalContext })
51
+ // }
52
+ return vm.runInContext(code, evalContext)
53
+ }
54
+
55
+ return (n: t.Node) => {
56
+ return evaluateAstNode(n, evalFn)
57
+ }
58
+ }
59
+
60
+ export function createSafeEvaluator(attemptEval: (n: t.Node) => any) {
61
+ return (n: t.Node) => {
62
+ try {
63
+ return attemptEval(n)
64
+ } catch (err) {
65
+ return FAILED_EVAL
66
+ }
67
+ }
68
+ }