@tamagui/static 1.0.1-beta.56 → 1.0.1-beta.60
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.
- package/dist/cjs/extractor/createExtractor.js +18 -1
- package/dist/cjs/extractor/createExtractor.js.map +2 -2
- package/dist/cjs/patchReactNativeWeb.js +1 -1
- package/dist/cjs/patchReactNativeWeb.js.map +2 -2
- package/dist/esm/extractor/createExtractor.js +18 -1
- package/dist/esm/extractor/createExtractor.js.map +2 -2
- package/dist/esm/patchReactNativeWeb.js +1 -1
- package/dist/esm/patchReactNativeWeb.js.map +2 -2
- package/dist/jsx/extractor/createExtractor.js +18 -1
- package/dist/jsx/patchReactNativeWeb.js +1 -1
- package/package.json +9 -7
- package/src/constants.ts +13 -0
- package/src/extractor/accessSafe.ts +18 -0
- package/src/extractor/babelParse.ts +27 -0
- package/src/extractor/buildClassName.ts +61 -0
- package/src/extractor/createEvaluator.ts +69 -0
- package/src/extractor/createExtractor.ts +1696 -0
- package/src/extractor/ensureImportingConcat.ts +39 -0
- package/src/extractor/evaluateAstNode.ts +121 -0
- package/src/extractor/extractHelpers.ts +119 -0
- package/src/extractor/extractMediaStyle.ts +190 -0
- package/src/extractor/extractToClassNames.ts +455 -0
- package/src/extractor/findTopmostFunction.ts +22 -0
- package/src/extractor/generatedUid.ts +43 -0
- package/src/extractor/getPrefixLogs.ts +6 -0
- package/src/extractor/getPropValueFromAttributes.ts +92 -0
- package/src/extractor/getSourceModule.ts +101 -0
- package/src/extractor/getStaticBindingsForScope.ts +183 -0
- package/src/extractor/hoistClassNames.ts +45 -0
- package/src/extractor/literalToAst.ts +84 -0
- package/src/extractor/loadTamagui.ts +139 -0
- package/src/extractor/logLines.ts +16 -0
- package/src/extractor/normalizeTernaries.ts +63 -0
- package/src/extractor/removeUnusedHooks.ts +76 -0
- package/src/extractor/timer.ts +18 -0
- package/src/extractor/validHTMLAttributes.ts +99 -0
- package/src/index.ts +9 -0
- package/src/patchReactNativeWeb.ts +165 -0
- package/src/types.ts +97 -0
- package/types/extractor/createExtractor.d.ts +14 -1
- package/types/extractor/createExtractor.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/static",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.60",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"types": "./types/index.d.ts",
|
|
6
6
|
"main": "dist/cjs",
|
|
7
7
|
"module": "dist/esm",
|
|
8
8
|
"module:jsx": "dist/jsx",
|
|
9
9
|
"files": [
|
|
10
|
+
"src",
|
|
10
11
|
"types",
|
|
11
12
|
"dist"
|
|
12
13
|
],
|
|
@@ -16,6 +17,7 @@
|
|
|
16
17
|
"clean": "tamagui-build clean",
|
|
17
18
|
"clean:build": "tamagui-build clean:build",
|
|
18
19
|
"test": "NODE_ENV=test TAMAGUI_COMPILE_PROCESS=1 jest --forceExit",
|
|
20
|
+
"test:babel": "yarn test tests/babel-test.tsx",
|
|
19
21
|
"test:debug": "NODE_ENV=test TAMAGUI_COMPILE_PROCESS=1 DEBUG_FILE=extract-specs.tsx DEBUG=1 yarn test",
|
|
20
22
|
"test:watch": "NODE_ENV=test TAMAGUI_COMPILE_PROCESS=1 yarn test --watch",
|
|
21
23
|
"test:update-snapshots": "NODE_ENV=test jest --updateSnapshot"
|
|
@@ -34,11 +36,11 @@
|
|
|
34
36
|
"@babel/parser": "^7.15.7",
|
|
35
37
|
"@babel/traverse": "^7.15.4",
|
|
36
38
|
"@expo/match-media": "^0.3.0",
|
|
37
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
38
|
-
"@tamagui/core-node": "^1.0.1-beta.
|
|
39
|
-
"@tamagui/fake-react-native": "^1.0.1-beta.
|
|
40
|
-
"@tamagui/helpers": "^1.0.1-beta.
|
|
41
|
-
"@tamagui/proxy-worm": "^1.0.1-beta.
|
|
39
|
+
"@tamagui/build": "^1.0.1-beta.60",
|
|
40
|
+
"@tamagui/core-node": "^1.0.1-beta.60",
|
|
41
|
+
"@tamagui/fake-react-native": "^1.0.1-beta.60",
|
|
42
|
+
"@tamagui/helpers": "^1.0.1-beta.60",
|
|
43
|
+
"@tamagui/proxy-worm": "^1.0.1-beta.60",
|
|
42
44
|
"babel-literal-to-ast": "^2.1.0",
|
|
43
45
|
"esbuild": "^0.14.36",
|
|
44
46
|
"esbuild-register": "^3.3.2",
|
|
@@ -46,7 +48,7 @@
|
|
|
46
48
|
"fs-extra": "^9.1.0",
|
|
47
49
|
"invariant": "^2.2.4",
|
|
48
50
|
"lodash": "^4.17.21",
|
|
49
|
-
"tamagui": "^1.0.1-beta.
|
|
51
|
+
"tamagui": "^1.0.1-beta.60"
|
|
50
52
|
},
|
|
51
53
|
"devDependencies": {
|
|
52
54
|
"@babel/plugin-syntax-typescript": "^7.14.5",
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import findCacheDir from 'find-cache-dir'
|
|
2
|
+
|
|
3
|
+
export const CSS_FILE_NAME = '__snack.css'
|
|
4
|
+
|
|
5
|
+
// ENSURE THIS ISNT THE SAME AS THE SEPARATOR USED FOR STYLE KEYS
|
|
6
|
+
// SEE matching one in concatClassName
|
|
7
|
+
export const MEDIA_SEP = '_'
|
|
8
|
+
|
|
9
|
+
// ensure cache dir
|
|
10
|
+
export const cacheDir = findCacheDir({ name: 'tamagui', create: true })
|
|
11
|
+
|
|
12
|
+
export const FAILED_EVAL = Symbol('failed_style_eval')
|
|
13
|
+
export const CONCAT_CLASSNAME_IMPORT = 'concatClassName'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as t from '@babel/types'
|
|
2
|
+
|
|
3
|
+
// accessSafe wraps memberExpressions in object/null checks
|
|
4
|
+
// TODO: inject this as a function? this gets pretty repetitive
|
|
5
|
+
export function accessSafe(obj: t.Expression, member: string): t.LogicalExpression {
|
|
6
|
+
return t.logicalExpression(
|
|
7
|
+
'&&',
|
|
8
|
+
t.logicalExpression(
|
|
9
|
+
'&&',
|
|
10
|
+
// typeof obj === 'object
|
|
11
|
+
t.binaryExpression('===', t.unaryExpression('typeof', obj), t.stringLiteral('object')),
|
|
12
|
+
// obj !== null
|
|
13
|
+
t.binaryExpression('!==', obj, t.nullLiteral())
|
|
14
|
+
),
|
|
15
|
+
// obj.member
|
|
16
|
+
t.memberExpression(obj, t.identifier(member), false)
|
|
17
|
+
)
|
|
18
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as babelParser from '@babel/parser'
|
|
2
|
+
|
|
3
|
+
const plugins: babelParser.ParserPlugin[] = [
|
|
4
|
+
'asyncGenerators',
|
|
5
|
+
'classProperties',
|
|
6
|
+
'dynamicImport',
|
|
7
|
+
'functionBind',
|
|
8
|
+
'jsx',
|
|
9
|
+
'numericSeparator',
|
|
10
|
+
'objectRestSpread',
|
|
11
|
+
'optionalCatchBinding',
|
|
12
|
+
'decorators-legacy',
|
|
13
|
+
'typescript',
|
|
14
|
+
'optionalChaining',
|
|
15
|
+
'nullishCoalescingOperator',
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
export const parserOptions: babelParser.ParserOptions = Object.freeze({
|
|
19
|
+
plugins,
|
|
20
|
+
sourceType: 'module',
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const parser = babelParser.parse.bind(babelParser)
|
|
24
|
+
|
|
25
|
+
export function babelParse(code: string | Buffer): any {
|
|
26
|
+
return parser(code.toString(), parserOptions)
|
|
27
|
+
}
|
|
@@ -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,69 @@
|
|
|
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
|
+
import { createCSSVariable } from '@tamagui/core-node'
|
|
8
|
+
|
|
9
|
+
import { FAILED_EVAL } from '../constants'
|
|
10
|
+
import { evaluateAstNode } from './evaluateAstNode'
|
|
11
|
+
import { isValidThemeHook } from './extractHelpers'
|
|
12
|
+
|
|
13
|
+
export function createEvaluator({
|
|
14
|
+
tamaguiConfig,
|
|
15
|
+
staticNamespace,
|
|
16
|
+
sourcePath,
|
|
17
|
+
traversePath,
|
|
18
|
+
shouldPrintDebug,
|
|
19
|
+
}: {
|
|
20
|
+
tamaguiConfig: TamaguiConfig
|
|
21
|
+
staticNamespace: Record<string, any>
|
|
22
|
+
sourcePath: string
|
|
23
|
+
traversePath: NodePath<t.JSXElement>
|
|
24
|
+
shouldPrintDebug: boolean | 'verbose'
|
|
25
|
+
}) {
|
|
26
|
+
// called when evaluateAstNode encounters a dynamic-looking prop
|
|
27
|
+
const evalFn = (n: t.Node) => {
|
|
28
|
+
// themes
|
|
29
|
+
if (
|
|
30
|
+
t.isMemberExpression(n) &&
|
|
31
|
+
t.isIdentifier(n.property) &&
|
|
32
|
+
isValidThemeHook(traversePath, n, sourcePath)
|
|
33
|
+
) {
|
|
34
|
+
const key = n.property.name
|
|
35
|
+
if (shouldPrintDebug) {
|
|
36
|
+
console.log(' > found theme prop', key)
|
|
37
|
+
}
|
|
38
|
+
console.log('SHOULD FIND THEME (NESTED NOW)', key) // tamaguiConfig.themes)
|
|
39
|
+
// if (!themeKeys.has(key)) {
|
|
40
|
+
// throw new Error(` > accessing non-existent theme key: ${key}`)
|
|
41
|
+
// }
|
|
42
|
+
return createCSSVariable(key)
|
|
43
|
+
}
|
|
44
|
+
// variable
|
|
45
|
+
if (t.isIdentifier(n) && staticNamespace.hasOwnProperty(n.name)) {
|
|
46
|
+
return staticNamespace[n.name]
|
|
47
|
+
}
|
|
48
|
+
const evalContext = vm.createContext(staticNamespace)
|
|
49
|
+
const code = `(${generate(n as any).code})`
|
|
50
|
+
// if (shouldPrintDebug) {
|
|
51
|
+
// console.log('evaluating', { n, code, evalContext })
|
|
52
|
+
// }
|
|
53
|
+
return vm.runInContext(code, evalContext)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return (n: t.Node) => {
|
|
57
|
+
return evaluateAstNode(n, evalFn)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function createSafeEvaluator(attemptEval: (n: t.Node) => any) {
|
|
62
|
+
return (n: t.Node) => {
|
|
63
|
+
try {
|
|
64
|
+
return attemptEval(n)
|
|
65
|
+
} catch (err) {
|
|
66
|
+
return FAILED_EVAL
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|