@tamagui/static 2.7.7 → 3.0.0-beta.643.1
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/check-dep-versions.cjs +355 -0
- package/dist/cjs/checkDeps.cjs +302 -0
- package/dist/cjs/compiler.cjs +286 -0
- package/dist/cjs/compilerHost.cjs +1695 -0
- package/dist/cjs/constants.cjs +33 -0
- package/dist/cjs/domStructuralPass.cjs +112 -0
- package/dist/cjs/exports.cjs +42 -0
- package/dist/cjs/extractor/addLocalExports.cjs +64 -0
- package/dist/cjs/extractor/bundle.cjs +203 -0
- package/dist/cjs/extractor/bundleConfig.cjs +747 -0
- package/dist/cjs/extractor/concatClassName.cjs +117 -0
- package/dist/cjs/extractor/detectModuleFormat.cjs +54 -0
- package/dist/cjs/extractor/esbuildAliasPlugin.cjs +47 -0
- package/dist/cjs/extractor/esbuildTsconfigPaths.cjs +92 -0
- package/dist/cjs/extractor/getTamaguiConfigPathFromOptionsConfig.cjs +38 -0
- package/dist/cjs/extractor/loadTamagui.cjs +344 -0
- package/dist/cjs/extractor/regenerateConfig.cjs +161 -0
- package/dist/cjs/extractor/watchTamaguiConfig.cjs +64 -0
- package/dist/cjs/getPragmaOptions.cjs +67 -0
- package/dist/cjs/helpers/memoize.cjs +42 -0
- package/dist/cjs/helpers/requireTamaguiCore.cjs +40 -0
- package/dist/cjs/index.cjs +34 -0
- package/dist/cjs/registerRequire.cjs +220 -0
- package/dist/cjs/server.cjs +66 -0
- package/dist/cjs/staticEvaluationIgnoredModules.cjs +86 -0
- package/dist/cjs/types.cjs +17 -0
- package/dist/cjs/zero/artifact.cjs +145 -0
- package/dist/cjs/zero/graph.cjs +209 -0
- package/dist/cjs/zero/identity.cjs +61 -0
- package/dist/cjs/zero/index.cjs +26 -0
- package/dist/cjs/zero/islands.cjs +232 -0
- package/dist/cjs/zero/options.cjs +163 -0
- package/dist/cjs/zero/ownership.cjs +79 -0
- package/dist/cjs/zero/theme.cjs +328 -0
- package/dist/cjs/zero/transform.cjs +203 -0
- package/dist/esm/check-dep-versions.mjs +329 -0
- package/dist/esm/check-dep-versions.mjs.map +1 -0
- package/dist/esm/checkDeps.mjs +279 -0
- package/dist/esm/checkDeps.mjs.map +1 -0
- package/dist/esm/compiler.mjs +255 -0
- package/dist/esm/compiler.mjs.map +1 -0
- package/dist/esm/compilerHost.mjs +1675 -0
- package/dist/esm/compilerHost.mjs.map +1 -0
- package/dist/esm/constants.mjs +7 -0
- package/dist/esm/constants.mjs.map +1 -0
- package/dist/esm/domStructuralPass.mjs +92 -0
- package/dist/esm/domStructuralPass.mjs.map +1 -0
- package/dist/esm/exports.mjs +26 -0
- package/dist/esm/extractor/addLocalExports.mjs +44 -0
- package/dist/esm/extractor/addLocalExports.mjs.map +1 -0
- package/dist/esm/extractor/bundle.mjs +173 -0
- package/dist/esm/extractor/bundle.mjs.map +1 -0
- package/dist/esm/extractor/bundleConfig.mjs +706 -0
- package/dist/esm/extractor/bundleConfig.mjs.map +1 -0
- package/dist/esm/extractor/concatClassName.mjs +96 -0
- package/dist/esm/extractor/concatClassName.mjs.map +1 -0
- package/dist/esm/extractor/detectModuleFormat.mjs +31 -0
- package/dist/esm/extractor/detectModuleFormat.mjs.map +1 -0
- package/dist/esm/extractor/esbuildAliasPlugin.mjs +26 -0
- package/dist/esm/extractor/esbuildAliasPlugin.mjs.map +1 -0
- package/dist/esm/extractor/esbuildTsconfigPaths.mjs +63 -0
- package/dist/esm/extractor/esbuildTsconfigPaths.mjs.map +1 -0
- package/dist/esm/extractor/getTamaguiConfigPathFromOptionsConfig.mjs +18 -0
- package/dist/esm/extractor/getTamaguiConfigPathFromOptionsConfig.mjs.map +1 -0
- package/dist/esm/extractor/loadTamagui.mjs +306 -0
- package/dist/esm/extractor/loadTamagui.mjs.map +1 -0
- package/dist/esm/extractor/regenerateConfig.mjs +131 -0
- package/dist/esm/extractor/regenerateConfig.mjs.map +1 -0
- package/dist/esm/extractor/watchTamaguiConfig.mjs +43 -0
- package/dist/esm/extractor/watchTamaguiConfig.mjs.map +1 -0
- package/dist/esm/getPragmaOptions.mjs +46 -0
- package/dist/esm/getPragmaOptions.mjs.map +1 -0
- package/dist/esm/helpers/memoize.mjs +21 -0
- package/dist/esm/helpers/memoize.mjs.map +1 -0
- package/dist/esm/helpers/requireTamaguiCore.mjs +19 -0
- package/dist/esm/helpers/requireTamaguiCore.mjs.map +1 -0
- package/dist/esm/index.mjs +8 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/registerRequire.mjs +195 -0
- package/dist/esm/registerRequire.mjs.map +1 -0
- package/dist/esm/server.mjs +40 -0
- package/dist/esm/server.mjs.map +1 -0
- package/dist/esm/setup.mjs +0 -0
- package/dist/esm/staticEvaluationIgnoredModules.mjs +61 -0
- package/dist/esm/staticEvaluationIgnoredModules.mjs.map +1 -0
- package/dist/esm/types.mjs +0 -0
- package/dist/esm/zero/artifact.mjs +119 -0
- package/dist/esm/zero/artifact.mjs.map +1 -0
- package/dist/esm/zero/graph.mjs +170 -0
- package/dist/esm/zero/graph.mjs.map +1 -0
- package/dist/esm/zero/identity.mjs +36 -0
- package/dist/esm/zero/identity.mjs.map +1 -0
- package/dist/esm/zero/index.mjs +15 -0
- package/dist/esm/zero/islands.mjs +197 -0
- package/dist/esm/zero/islands.mjs.map +1 -0
- package/dist/esm/zero/options.mjs +129 -0
- package/dist/esm/zero/options.mjs.map +1 -0
- package/dist/esm/zero/ownership.mjs +48 -0
- package/dist/esm/zero/ownership.mjs.map +1 -0
- package/dist/esm/zero/theme.mjs +303 -0
- package/dist/esm/zero/theme.mjs.map +1 -0
- package/dist/esm/zero/transform.mjs +177 -0
- package/dist/esm/zero/transform.mjs.map +1 -0
- package/package.json +33 -43
- package/src/check-dep-versions.ts +2 -2
- package/src/checkDeps.ts +3 -2
- package/src/compiler.ts +425 -0
- package/src/compilerHost.ts +3125 -0
- package/src/constants.ts +0 -5
- package/src/domStructuralPass.ts +170 -0
- package/src/exports.ts +3 -6
- package/src/extractor/addLocalExports.ts +49 -0
- package/src/extractor/bundle.ts +20 -35
- package/src/extractor/bundleConfig.ts +334 -187
- package/src/extractor/concatClassName.ts +13 -20
- package/src/extractor/getTamaguiConfigPathFromOptionsConfig.ts +5 -4
- package/src/extractor/loadTamagui.ts +196 -143
- package/src/extractor/regenerateConfig.ts +65 -13
- package/src/extractor/watchTamaguiConfig.ts +6 -1
- package/src/helpers/requireTamaguiCore.ts +6 -1
- package/src/registerRequire.ts +94 -132
- package/src/staticEvaluationIgnoredModules.ts +74 -0
- package/src/types.ts +2 -107
- package/src/zero/artifact.ts +142 -0
- package/src/zero/graph.ts +359 -0
- package/src/zero/identity.ts +73 -0
- package/src/zero/index.ts +8 -0
- package/src/zero/islands.ts +262 -0
- package/src/zero/options.ts +239 -0
- package/src/zero/ownership.ts +113 -0
- package/src/zero/theme.ts +478 -0
- package/src/zero/transform.ts +320 -0
- package/types/check-dep-versions.d.ts.map +1 -1
- package/types/checkDeps.d.ts.map +1 -1
- package/types/compiler.d.ts +117 -0
- package/types/compiler.d.ts.map +1 -0
- package/types/compilerHost.d.ts +25 -0
- package/types/compilerHost.d.ts.map +1 -0
- package/types/constants.d.ts +0 -1
- package/types/constants.d.ts.map +1 -1
- package/types/domStructuralPass.d.ts +3 -0
- package/types/domStructuralPass.d.ts.map +1 -0
- package/types/exports.d.ts +3 -6
- package/types/exports.d.ts.map +1 -1
- package/types/extractor/addLocalExports.d.ts +3 -0
- package/types/extractor/addLocalExports.d.ts.map +1 -0
- package/types/extractor/bundle.d.ts +66 -65
- package/types/extractor/bundle.d.ts.map +1 -1
- package/types/extractor/bundleConfig.d.ts +19 -4
- package/types/extractor/bundleConfig.d.ts.map +1 -1
- package/types/extractor/esbuildAliasPlugin.d.ts.map +1 -1
- package/types/extractor/getTamaguiConfigPathFromOptionsConfig.d.ts +1 -1
- package/types/extractor/getTamaguiConfigPathFromOptionsConfig.d.ts.map +1 -1
- package/types/extractor/loadTamagui.d.ts +26 -3
- package/types/extractor/loadTamagui.d.ts.map +1 -1
- package/types/extractor/regenerateConfig.d.ts +1 -1
- package/types/extractor/regenerateConfig.d.ts.map +1 -1
- package/types/extractor/watchTamaguiConfig.d.ts.map +1 -1
- package/types/getPragmaOptions.d.ts +1 -1
- package/types/helpers/memoize.d.ts +1 -1
- package/types/helpers/memoize.d.ts.map +1 -1
- package/types/helpers/requireTamaguiCore.d.ts.map +1 -1
- package/types/index.d.ts.map +1 -1
- package/types/registerRequire.d.ts +2 -2
- package/types/registerRequire.d.ts.map +1 -1
- package/types/staticEvaluationIgnoredModules.d.ts +9 -0
- package/types/staticEvaluationIgnoredModules.d.ts.map +1 -0
- package/types/types.d.ts +2 -90
- package/types/types.d.ts.map +1 -1
- package/types/zero/artifact.d.ts +44 -0
- package/types/zero/artifact.d.ts.map +1 -0
- package/types/zero/graph.d.ts +139 -0
- package/types/zero/graph.d.ts.map +1 -0
- package/types/zero/identity.d.ts +33 -0
- package/types/zero/identity.d.ts.map +1 -0
- package/types/zero/index.d.ts +9 -0
- package/types/zero/index.d.ts.map +1 -0
- package/types/zero/islands.d.ts +56 -0
- package/types/zero/islands.d.ts.map +1 -0
- package/types/zero/options.d.ts +58 -0
- package/types/zero/options.d.ts.map +1 -0
- package/types/zero/ownership.d.ts +51 -0
- package/types/zero/ownership.d.ts.map +1 -0
- package/types/zero/theme.d.ts +83 -0
- package/types/zero/theme.d.ts.map +1 -0
- package/types/zero/transform.d.ts +53 -0
- package/types/zero/transform.d.ts.map +1 -0
- package/dist/check-dep-versions.cjs +0 -372
- package/dist/checkDeps.cjs +0 -271
- package/dist/constants.cjs +0 -51
- package/dist/exports.cjs +0 -48
- package/dist/extractor/accessSafe.cjs +0 -48
- package/dist/extractor/babelParse.cjs +0 -56
- package/dist/extractor/buildClassName.cjs +0 -86
- package/dist/extractor/bundle.cjs +0 -234
- package/dist/extractor/bundleConfig.cjs +0 -729
- package/dist/extractor/concatClassName.cjs +0 -129
- package/dist/extractor/createEvaluator.cjs +0 -88
- package/dist/extractor/createExtractor.cjs +0 -1968
- package/dist/extractor/createLogger.cjs +0 -50
- package/dist/extractor/detectModuleFormat.cjs +0 -55
- package/dist/extractor/ensureImportingConcat.cjs +0 -54
- package/dist/extractor/errors.cjs +0 -28
- package/dist/extractor/esbuildAliasPlugin.cjs +0 -56
- package/dist/extractor/esbuildTsconfigPaths.cjs +0 -108
- package/dist/extractor/evaluateAstNode.cjs +0 -128
- package/dist/extractor/extractHelpers.cjs +0 -182
- package/dist/extractor/extractMediaStyle.cjs +0 -169
- package/dist/extractor/extractToClassNames.cjs +0 -475
- package/dist/extractor/extractToNative.cjs +0 -369
- package/dist/extractor/findTopmostFunction.cjs +0 -43
- package/dist/extractor/generatedUid.cjs +0 -58
- package/dist/extractor/getPrefixLogs.cjs +0 -31
- package/dist/extractor/getPropValueFromAttributes.cjs +0 -85
- package/dist/extractor/getSourceModule.cjs +0 -100
- package/dist/extractor/getStaticBindingsForScope.cjs +0 -213
- package/dist/extractor/getTamaguiConfigPathFromOptionsConfig.cjs +0 -41
- package/dist/extractor/hasTopLevelAwait.cjs +0 -62
- package/dist/extractor/hoistClassNames.cjs +0 -76
- package/dist/extractor/literalToAst.cjs +0 -109
- package/dist/extractor/loadFile.cjs +0 -17
- package/dist/extractor/loadTamagui.cjs +0 -373
- package/dist/extractor/logLines.cjs +0 -43
- package/dist/extractor/normalizeTernaries.cjs +0 -100
- package/dist/extractor/propsToFontFamilyCache.cjs +0 -42
- package/dist/extractor/regenerateConfig.cjs +0 -178
- package/dist/extractor/removeUnusedHooks.cjs +0 -108
- package/dist/extractor/timer.cjs +0 -52
- package/dist/extractor/validHTMLAttributes.cjs +0 -79
- package/dist/extractor/watchTamaguiConfig.cjs +0 -70
- package/dist/getPragmaOptions.cjs +0 -73
- package/dist/helpers/memoize.cjs +0 -45
- package/dist/helpers/requireTamaguiCore.cjs +0 -40
- package/dist/index.cjs +0 -42
- package/dist/registerRequire.cjs +0 -209
- package/dist/server.cjs +0 -82
- package/dist/types.cjs +0 -18
- package/dist/worker.cjs +0 -111
- package/src/extractor/accessSafe.ts +0 -22
- package/src/extractor/babelParse.ts +0 -38
- package/src/extractor/buildClassName.ts +0 -76
- package/src/extractor/createEvaluator.ts +0 -82
- package/src/extractor/createExtractor.ts +0 -2970
- package/src/extractor/createLogger.ts +0 -41
- package/src/extractor/ensureImportingConcat.ts +0 -36
- package/src/extractor/errors.ts +0 -1
- package/src/extractor/evaluateAstNode.ts +0 -136
- package/src/extractor/extractHelpers.ts +0 -211
- package/src/extractor/extractMediaStyle.ts +0 -194
- package/src/extractor/extractToClassNames.ts +0 -733
- package/src/extractor/extractToNative.ts +0 -506
- package/src/extractor/findTopmostFunction.ts +0 -24
- package/src/extractor/generatedUid.ts +0 -41
- package/src/extractor/getPrefixLogs.ts +0 -9
- package/src/extractor/getPropValueFromAttributes.ts +0 -95
- package/src/extractor/getSourceModule.ts +0 -96
- package/src/extractor/getStaticBindingsForScope.ts +0 -237
- package/src/extractor/hasTopLevelAwait.ts +0 -28
- package/src/extractor/hoistClassNames.ts +0 -52
- package/src/extractor/literalToAst.ts +0 -85
- package/src/extractor/loadFile.ts +0 -17
- package/src/extractor/logLines.ts +0 -16
- package/src/extractor/normalizeTernaries.ts +0 -89
- package/src/extractor/propsToFontFamilyCache.ts +0 -18
- package/src/extractor/removeUnusedHooks.ts +0 -83
- package/src/extractor/timer.ts +0 -25
- package/src/extractor/validHTMLAttributes.ts +0 -52
- package/src/worker.ts +0 -142
- package/types/extractor/accessSafe.d.ts +0 -3
- package/types/extractor/accessSafe.d.ts.map +0 -1
- package/types/extractor/babelParse.d.ts +0 -5
- package/types/extractor/babelParse.d.ts.map +0 -1
- package/types/extractor/buildClassName.d.ts +0 -7
- package/types/extractor/buildClassName.d.ts.map +0 -1
- package/types/extractor/createEvaluator.d.ts +0 -12
- package/types/extractor/createEvaluator.d.ts.map +0 -1
- package/types/extractor/createExtractor.d.ts +0 -32
- package/types/extractor/createExtractor.d.ts.map +0 -1
- package/types/extractor/createLogger.d.ts +0 -3
- package/types/extractor/createLogger.d.ts.map +0 -1
- package/types/extractor/ensureImportingConcat.d.ts +0 -4
- package/types/extractor/ensureImportingConcat.d.ts.map +0 -1
- package/types/extractor/errors.d.ts +0 -3
- package/types/extractor/errors.d.ts.map +0 -1
- package/types/extractor/evaluateAstNode.d.ts +0 -3
- package/types/extractor/evaluateAstNode.d.ts.map +0 -1
- package/types/extractor/extractHelpers.d.ts +0 -28
- package/types/extractor/extractHelpers.d.ts.map +0 -1
- package/types/extractor/extractMediaStyle.d.ts +0 -11
- package/types/extractor/extractMediaStyle.d.ts.map +0 -1
- package/types/extractor/extractToClassNames.d.ts +0 -25
- package/types/extractor/extractToClassNames.d.ts.map +0 -1
- package/types/extractor/extractToNative.d.ts +0 -13
- package/types/extractor/extractToNative.d.ts.map +0 -1
- package/types/extractor/findTopmostFunction.d.ts +0 -4
- package/types/extractor/findTopmostFunction.d.ts.map +0 -1
- package/types/extractor/generatedUid.d.ts +0 -5
- package/types/extractor/generatedUid.d.ts.map +0 -1
- package/types/extractor/getPrefixLogs.d.ts +0 -3
- package/types/extractor/getPrefixLogs.d.ts.map +0 -1
- package/types/extractor/getPropValueFromAttributes.d.ts +0 -19
- package/types/extractor/getPropValueFromAttributes.d.ts.map +0 -1
- package/types/extractor/getSourceModule.d.ts +0 -16
- package/types/extractor/getSourceModule.d.ts.map +0 -1
- package/types/extractor/getStaticBindingsForScope.d.ts +0 -5
- package/types/extractor/getStaticBindingsForScope.d.ts.map +0 -1
- package/types/extractor/hasTopLevelAwait.d.ts +0 -2
- package/types/extractor/hasTopLevelAwait.d.ts.map +0 -1
- package/types/extractor/hoistClassNames.d.ts +0 -6
- package/types/extractor/hoistClassNames.d.ts.map +0 -1
- package/types/extractor/literalToAst.d.ts +0 -4
- package/types/extractor/literalToAst.d.ts.map +0 -1
- package/types/extractor/loadFile.d.ts +0 -1
- package/types/extractor/loadFile.d.ts.map +0 -1
- package/types/extractor/logLines.d.ts +0 -2
- package/types/extractor/logLines.d.ts.map +0 -1
- package/types/extractor/normalizeTernaries.d.ts +0 -5
- package/types/extractor/normalizeTernaries.d.ts.map +0 -1
- package/types/extractor/propsToFontFamilyCache.d.ts +0 -4
- package/types/extractor/propsToFontFamilyCache.d.ts.map +0 -1
- package/types/extractor/removeUnusedHooks.d.ts +0 -3
- package/types/extractor/removeUnusedHooks.d.ts.map +0 -1
- package/types/extractor/timer.d.ts +0 -5
- package/types/extractor/timer.d.ts.map +0 -1
- package/types/extractor/validHTMLAttributes.d.ts +0 -52
- package/types/extractor/validHTMLAttributes.d.ts.map +0 -1
- package/types/worker.d.ts +0 -46
- package/types/worker.d.ts.map +0 -1
- /package/dist/{setup.cjs → cjs/setup.cjs} +0 -0
|
@@ -1,733 +0,0 @@
|
|
|
1
|
-
import generate from '@babel/generator'
|
|
2
|
-
import type { NodePath } from '@babel/traverse'
|
|
3
|
-
import * as t from '@babel/types'
|
|
4
|
-
import { mergeProps, StyleObjectIdentifier, StyleObjectRules } from '@tamagui/web'
|
|
5
|
-
import * as path from 'node:path'
|
|
6
|
-
import * as util from 'node:util'
|
|
7
|
-
import { requireTamaguiCore } from '../helpers/requireTamaguiCore'
|
|
8
|
-
import type { StyleObject, TamaguiOptions, Ternary } from '../types'
|
|
9
|
-
import { babelParse } from './babelParse'
|
|
10
|
-
import type { Extractor } from './createExtractor'
|
|
11
|
-
import { createLogger } from './createLogger'
|
|
12
|
-
import { extractMediaStyle } from './extractMediaStyle'
|
|
13
|
-
import { normalizeTernaries } from './normalizeTernaries'
|
|
14
|
-
import {
|
|
15
|
-
forwardFontFamilyName,
|
|
16
|
-
getFontFamilyNameFromProps,
|
|
17
|
-
} from './propsToFontFamilyCache'
|
|
18
|
-
import { timer } from './timer'
|
|
19
|
-
import { BailOptimizationError } from './errors'
|
|
20
|
-
import { concatClassName } from './concatClassName'
|
|
21
|
-
|
|
22
|
-
export type ExtractedResponse = {
|
|
23
|
-
js: string | Buffer
|
|
24
|
-
styles: string
|
|
25
|
-
stylesPath?: string
|
|
26
|
-
ast: t.File
|
|
27
|
-
map: any
|
|
28
|
-
stats: {
|
|
29
|
-
styled: number
|
|
30
|
-
flattened: number
|
|
31
|
-
optimized: number
|
|
32
|
-
found: number
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export type ExtractToClassNamesProps = {
|
|
37
|
-
extractor: Extractor
|
|
38
|
-
source: string | Buffer
|
|
39
|
-
sourcePath?: string
|
|
40
|
-
options: TamaguiOptions
|
|
41
|
-
shouldPrintDebug: boolean | 'verbose'
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// we only expand into ternaries or plain attr, all style is turned into a always-true ternary
|
|
45
|
-
// this lets us more easily combine everything easily
|
|
46
|
-
// all ternaries in this array ONLY have consequent, they are normalized
|
|
47
|
-
const remove = () => {} // we dont remove after this step
|
|
48
|
-
const spaceString = t.stringLiteral(' ')
|
|
49
|
-
|
|
50
|
-
export async function extractToClassNames({
|
|
51
|
-
extractor,
|
|
52
|
-
source,
|
|
53
|
-
sourcePath = '',
|
|
54
|
-
options,
|
|
55
|
-
shouldPrintDebug,
|
|
56
|
-
}: ExtractToClassNamesProps): Promise<ExtractedResponse | null> {
|
|
57
|
-
const tm = timer()
|
|
58
|
-
const { getCSSStylesAtomic, createMediaStyle } = requireTamaguiCore('web')
|
|
59
|
-
|
|
60
|
-
if (sourcePath.includes('node_modules')) {
|
|
61
|
-
return null
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (shouldPrintDebug) {
|
|
65
|
-
console.warn(`--- ${sourcePath} --- \n\n`)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (typeof source !== 'string') {
|
|
69
|
-
throw new Error('`source` must be a string of javascript')
|
|
70
|
-
}
|
|
71
|
-
if (!path.isAbsolute(sourcePath)) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
'`sourcePath` must be an absolute path to a .js file, got: ' + sourcePath
|
|
74
|
-
)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!/.[tj]sx?$/i.test(sourcePath || '')) {
|
|
78
|
-
console.warn(`${sourcePath.slice(0, 100)} - bad filename.`)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!options.disableExtraction && !options['_disableLoadTamagui']) {
|
|
82
|
-
// dont include loading in timing of parsing (one time cost)
|
|
83
|
-
await extractor.loadTamagui(options)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const printLog = createLogger(sourcePath, options)
|
|
87
|
-
|
|
88
|
-
// Using a map for (officially supported) guaranteed insertion order
|
|
89
|
-
let ast: t.File
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
ast = babelParse(source, sourcePath)
|
|
93
|
-
} catch (err) {
|
|
94
|
-
console.error('babel parse error:', sourcePath.slice(0, 100))
|
|
95
|
-
throw err
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
tm.mark(`babel-parse`, shouldPrintDebug === 'verbose')
|
|
99
|
-
|
|
100
|
-
const cssMap = new Map<string, { css: string; commentTexts: string[] }>()
|
|
101
|
-
const tamaguiConfig = extractor.getTamagui()!
|
|
102
|
-
|
|
103
|
-
const res = await extractor.parse(ast, {
|
|
104
|
-
shouldPrintDebug,
|
|
105
|
-
...options,
|
|
106
|
-
platform: 'web',
|
|
107
|
-
sourcePath,
|
|
108
|
-
extractStyledDefinitions: true,
|
|
109
|
-
onStyledDefinitionRule(identifier, rules) {
|
|
110
|
-
const css = rules.join('\n')
|
|
111
|
-
if (shouldPrintDebug) {
|
|
112
|
-
console.info(`adding styled() rule: .${identifier} ${css}`)
|
|
113
|
-
}
|
|
114
|
-
cssMap.set(`.${identifier}`, { css, commentTexts: [] })
|
|
115
|
-
},
|
|
116
|
-
getFlattenedNode: ({ tag }) => {
|
|
117
|
-
return tag
|
|
118
|
-
},
|
|
119
|
-
onExtractTag: ({
|
|
120
|
-
parserProps,
|
|
121
|
-
attrs,
|
|
122
|
-
node,
|
|
123
|
-
attemptEval,
|
|
124
|
-
jsxPath,
|
|
125
|
-
originalNodeName,
|
|
126
|
-
filePath,
|
|
127
|
-
lineNumbers,
|
|
128
|
-
staticConfig,
|
|
129
|
-
}) => {
|
|
130
|
-
// bail out of views that don't accept className (falls back to runtime + style={})
|
|
131
|
-
if (staticConfig.acceptsClassName === false) {
|
|
132
|
-
throw new BailOptimizationError()
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// re-worked how we do this
|
|
136
|
-
// merging ternaries on top of base styles is not simple, because we need to ensure the final
|
|
137
|
-
// className has no duplicate style props and selector order is preserved
|
|
138
|
-
// before we tried to be smart and build a big binary expression
|
|
139
|
-
// instead, what we'll do now is pre-calculate the entire className for every possible path
|
|
140
|
-
// for super complex components that means we *will* output a lot of bigger classNames
|
|
141
|
-
// but its so much simpler than trying to implement a multi-stage solver here
|
|
142
|
-
// and in the end its just strings that gzip very well
|
|
143
|
-
// its also much easier to intuit/debug for end users and ourselves
|
|
144
|
-
|
|
145
|
-
// example:
|
|
146
|
-
// a ? 'a' : 'b'
|
|
147
|
-
// b ? 'c' : 'd'
|
|
148
|
-
// we want:
|
|
149
|
-
// a && b ? 'a c' : ''
|
|
150
|
-
// !a && b ? 'b c' : ''
|
|
151
|
-
// a && !b ? 'a d' : ''
|
|
152
|
-
// !a && !b ? 'b d' : ''
|
|
153
|
-
|
|
154
|
-
// we also simplified the compiler to only handle views that can be fully flattened
|
|
155
|
-
// this means we don't need to account for strange in-between spreads, so we can merge things
|
|
156
|
-
// fairly simply. first, we just merge forward all the non-ternary styles into ternaries.
|
|
157
|
-
|
|
158
|
-
// save for the end
|
|
159
|
-
const finalAttrs: t.JSXAttribute[] = []
|
|
160
|
-
|
|
161
|
-
let mergeForwardBaseStyle: object | null = null
|
|
162
|
-
let attrClassName: t.Expression | null = null
|
|
163
|
-
let baseFontFamily = ''
|
|
164
|
-
let mediaStylesSeen = 1
|
|
165
|
-
|
|
166
|
-
const comment = util.format(
|
|
167
|
-
'/* %s:%s (%s) */',
|
|
168
|
-
filePath,
|
|
169
|
-
lineNumbers,
|
|
170
|
-
originalNodeName
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
// detect static `group="<name>"` literal — runtime side normally emits the
|
|
174
|
-
// container-name/container-type CSS lazily via getSplitStyles (createComponent
|
|
175
|
-
// path), but extracted elements never go through that code, so we emit the
|
|
176
|
-
// equivalent CSS at compile time and add `t_group_<name>` to the className.
|
|
177
|
-
let staticGroupName: string | null = null
|
|
178
|
-
for (const attr of jsxPath.node.openingElement.attributes) {
|
|
179
|
-
if (!t.isJSXAttribute(attr) || !t.isJSXIdentifier(attr.name)) continue
|
|
180
|
-
if (attr.name.name !== 'group') continue
|
|
181
|
-
if (t.isStringLiteral(attr.value)) {
|
|
182
|
-
staticGroupName = attr.value.value
|
|
183
|
-
} else if (
|
|
184
|
-
t.isJSXExpressionContainer(attr.value) &&
|
|
185
|
-
t.isStringLiteral(attr.value.expression)
|
|
186
|
-
) {
|
|
187
|
-
staticGroupName = attr.value.expression.value
|
|
188
|
-
}
|
|
189
|
-
// dynamic group={someProp} falls back to the runtime path.
|
|
190
|
-
break
|
|
191
|
-
}
|
|
192
|
-
if (staticGroupName) {
|
|
193
|
-
const containerIdentifier = `t_group_${staticGroupName}`
|
|
194
|
-
const containerType =
|
|
195
|
-
(tamaguiConfig.settings?.webContainerType as string | undefined) ||
|
|
196
|
-
'inline-size'
|
|
197
|
-
const containerSelector = `.${containerIdentifier}`
|
|
198
|
-
if (!cssMap.has(containerSelector)) {
|
|
199
|
-
cssMap.set(containerSelector, {
|
|
200
|
-
css: `${containerSelector} { container-name: ${staticGroupName}; container-type: ${containerType}; }`,
|
|
201
|
-
commentTexts: [comment],
|
|
202
|
-
})
|
|
203
|
-
} else {
|
|
204
|
-
cssMap.get(containerSelector)!.commentTexts.push(comment)
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// invariant guard for $group-/$theme- CSS extraction (below): those styles
|
|
209
|
-
// can only be extracted to static @container / theme CSS when a class toggle
|
|
210
|
-
// alone expresses the change. JS animation drivers (reanimated, motion, moti,
|
|
211
|
-
// react-native) interpolate in JS and need the runtime path to read hard
|
|
212
|
-
// values when the element animates — a static class can't drive them. gated
|
|
213
|
-
// on whether THIS element animates rather than on the driver, because motion
|
|
214
|
-
// is isReactNative:false yet still needs JS.
|
|
215
|
-
//
|
|
216
|
-
// in practice createExtractor's deoptProps already de-opts every animated
|
|
217
|
-
// element upstream except CSS-only transitions, which lower to ordinary
|
|
218
|
-
// CSS. it keeps the invariant local and correct regardless of future
|
|
219
|
-
// deoptProps changes — it only ever bails the animated case to runtime,
|
|
220
|
-
// never widens what extracts.
|
|
221
|
-
const hasCSSOnlyAnimationDriver =
|
|
222
|
-
tamaguiConfig.animations.outputStyle === 'css' && !tamaguiConfig.animationDrivers
|
|
223
|
-
let elementIsAnimated = false
|
|
224
|
-
for (const attr of jsxPath.node.openingElement.attributes) {
|
|
225
|
-
if (!t.isJSXAttribute(attr) || !t.isJSXIdentifier(attr.name)) continue
|
|
226
|
-
const n = attr.name.name
|
|
227
|
-
if (n === 'transition' && hasCSSOnlyAnimationDriver) continue
|
|
228
|
-
if (
|
|
229
|
-
n === 'animation' ||
|
|
230
|
-
n === 'transition' ||
|
|
231
|
-
n === 'animateOnly' ||
|
|
232
|
-
n === 'animatePresence' ||
|
|
233
|
-
n === 'animatedBy' ||
|
|
234
|
-
n === 'enterStyle' ||
|
|
235
|
-
n === 'exitStyle'
|
|
236
|
-
) {
|
|
237
|
-
elementIsAnimated = true
|
|
238
|
-
break
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function addStyle(style: StyleObject) {
|
|
243
|
-
const identifier = style[StyleObjectIdentifier]
|
|
244
|
-
const rules = style[StyleObjectRules]
|
|
245
|
-
const selector = `.${identifier}`
|
|
246
|
-
if (cssMap.has(selector)) {
|
|
247
|
-
const val = cssMap.get(selector)!
|
|
248
|
-
val.commentTexts.push(comment)
|
|
249
|
-
} else if (rules.length) {
|
|
250
|
-
cssMap.set(selector, {
|
|
251
|
-
css: rules.join('\n'),
|
|
252
|
-
commentTexts: [comment],
|
|
253
|
-
})
|
|
254
|
-
}
|
|
255
|
-
return identifier
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function addStyles(style: object) {
|
|
259
|
-
const cssStyles = getCSSStylesAtomic(style as any)
|
|
260
|
-
const classNames: string[] = []
|
|
261
|
-
|
|
262
|
-
for (const style of cssStyles) {
|
|
263
|
-
const property = style[0]
|
|
264
|
-
const mediaName = property.slice(1)
|
|
265
|
-
|
|
266
|
-
// $group- styles extract through createMediaStyle which emits @container
|
|
267
|
-
// rules keyed on the parent's `t_group_<name>` className.
|
|
268
|
-
if (mediaName.startsWith('group-')) {
|
|
269
|
-
// a class toggle can't drive a JS animation driver's interpolation;
|
|
270
|
-
// keep animated elements on the runtime path (matches pre-extraction
|
|
271
|
-
// behavior for the group case).
|
|
272
|
-
if (elementIsAnimated) {
|
|
273
|
-
throw new BailOptimizationError()
|
|
274
|
-
}
|
|
275
|
-
const mediaStyle = createMediaStyle(
|
|
276
|
-
style,
|
|
277
|
-
mediaName,
|
|
278
|
-
extractor.getTamagui()!.media,
|
|
279
|
-
'group',
|
|
280
|
-
false,
|
|
281
|
-
mediaStylesSeen
|
|
282
|
-
)
|
|
283
|
-
const identifier = addStyle(mediaStyle)
|
|
284
|
-
classNames.push(identifier)
|
|
285
|
-
continue
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Check for theme/platform media queries (e.g., $theme-dark, $platform-web)
|
|
289
|
-
const mediaTypeMatch = mediaName.match(/^(theme|platform)-/)
|
|
290
|
-
if (mediaTypeMatch) {
|
|
291
|
-
const mediaType = mediaTypeMatch[1] as 'theme' | 'platform'
|
|
292
|
-
// $theme- values can change at runtime (theme switch); if this
|
|
293
|
-
// element animates, a JS driver must interpolate the change, so keep
|
|
294
|
-
// it on the runtime path. $platform- is build-time static — never gated.
|
|
295
|
-
if (mediaType === 'theme' && elementIsAnimated) {
|
|
296
|
-
throw new BailOptimizationError()
|
|
297
|
-
}
|
|
298
|
-
// createMediaStyle internally calls getGroupPropParts(`theme-` + key)
|
|
299
|
-
// for theme, so we must pass the short key ("dark", not "theme-dark")
|
|
300
|
-
// to avoid double-prefixing. Platform stays as-is (runtime parity).
|
|
301
|
-
const innerKey =
|
|
302
|
-
mediaType === 'theme' ? mediaName.slice('theme-'.length) : mediaName
|
|
303
|
-
const mediaStyle = createMediaStyle(
|
|
304
|
-
style,
|
|
305
|
-
innerKey,
|
|
306
|
-
extractor.getTamagui()!.media,
|
|
307
|
-
mediaType,
|
|
308
|
-
false,
|
|
309
|
-
mediaStylesSeen
|
|
310
|
-
)
|
|
311
|
-
const identifier = addStyle(mediaStyle)
|
|
312
|
-
classNames.push(identifier)
|
|
313
|
-
continue
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (mediaName in tamaguiConfig.media) {
|
|
317
|
-
const mediaStyle = createMediaStyle(
|
|
318
|
-
style,
|
|
319
|
-
mediaName,
|
|
320
|
-
extractor.getTamagui()!.media,
|
|
321
|
-
true,
|
|
322
|
-
false,
|
|
323
|
-
mediaStylesSeen
|
|
324
|
-
)
|
|
325
|
-
const identifier = addStyle(mediaStyle)
|
|
326
|
-
classNames.push(identifier)
|
|
327
|
-
continue
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const identifier = addStyle(style)
|
|
331
|
-
classNames.push(identifier)
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return classNames
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const onlyTernaries: Ternary[] = attrs.flatMap((attr) => {
|
|
338
|
-
if (attr.type === 'attr') {
|
|
339
|
-
const value = attr.value
|
|
340
|
-
|
|
341
|
-
if (t.isJSXSpreadAttribute(value)) {
|
|
342
|
-
// we only handle flattened stuff now so skip this
|
|
343
|
-
console.error(`Should never happen`)
|
|
344
|
-
return []
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (value.name.name === 'className') {
|
|
348
|
-
let inner: any = value.value
|
|
349
|
-
if (t.isJSXExpressionContainer(inner)) {
|
|
350
|
-
inner = inner.expression
|
|
351
|
-
}
|
|
352
|
-
try {
|
|
353
|
-
const evaluatedValue = inner ? attemptEval(inner) : null
|
|
354
|
-
if (typeof evaluatedValue === 'string') {
|
|
355
|
-
attrClassName = t.stringLiteral(evaluatedValue)
|
|
356
|
-
}
|
|
357
|
-
} catch (e) {
|
|
358
|
-
if (inner) {
|
|
359
|
-
attrClassName ||= inner
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return []
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
finalAttrs.push(value)
|
|
366
|
-
return []
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (attr.type === 'style') {
|
|
370
|
-
mergeForwardBaseStyle = mergeProps(mergeForwardBaseStyle || {}, attr.value)
|
|
371
|
-
baseFontFamily = getFontFamilyNameFromProps(attr.value) || ''
|
|
372
|
-
return []
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
let ternary = attr.value
|
|
376
|
-
|
|
377
|
-
if (ternary.inlineMediaQuery) {
|
|
378
|
-
const mediaExtraction = extractMediaStyle(
|
|
379
|
-
parserProps,
|
|
380
|
-
attr.value,
|
|
381
|
-
jsxPath,
|
|
382
|
-
extractor.getTamagui()!,
|
|
383
|
-
sourcePath || '',
|
|
384
|
-
mediaStylesSeen++,
|
|
385
|
-
shouldPrintDebug
|
|
386
|
-
)
|
|
387
|
-
|
|
388
|
-
if (mediaExtraction) {
|
|
389
|
-
if (mediaExtraction.mediaStyles) {
|
|
390
|
-
mergeForwardBaseStyle = mergeProps(mergeForwardBaseStyle || {}, {
|
|
391
|
-
[`$${ternary.inlineMediaQuery}`]: attr.value.consequent!,
|
|
392
|
-
})
|
|
393
|
-
}
|
|
394
|
-
if (mediaExtraction.ternaryWithoutMedia) {
|
|
395
|
-
ternary = mediaExtraction.ternaryWithoutMedia
|
|
396
|
-
} else {
|
|
397
|
-
return []
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
let mergedAlternate
|
|
403
|
-
let mergedConsequent
|
|
404
|
-
|
|
405
|
-
if (ternary.alternate && Object.keys(ternary.alternate).length) {
|
|
406
|
-
mergedAlternate = mergeProps(
|
|
407
|
-
mergeForwardBaseStyle || {},
|
|
408
|
-
ternary.alternate || {}
|
|
409
|
-
)
|
|
410
|
-
forwardFontFamilyName(ternary.alternate, mergedAlternate, baseFontFamily)
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
if (ternary.consequent && Object.keys(ternary.consequent).length) {
|
|
414
|
-
mergedConsequent = mergeProps(
|
|
415
|
-
mergeForwardBaseStyle || {},
|
|
416
|
-
ternary.consequent || {}
|
|
417
|
-
)
|
|
418
|
-
forwardFontFamilyName(ternary.consequent, mergedConsequent, baseFontFamily)
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// merge the base style forward into both sides
|
|
422
|
-
return {
|
|
423
|
-
...ternary,
|
|
424
|
-
alternate: mergedAlternate,
|
|
425
|
-
consequent: mergedConsequent,
|
|
426
|
-
}
|
|
427
|
-
})
|
|
428
|
-
|
|
429
|
-
const hasTernaries = Boolean(onlyTernaries.length)
|
|
430
|
-
|
|
431
|
-
const baseClassNames = mergeForwardBaseStyle
|
|
432
|
-
? addStyles(mergeForwardBaseStyle)
|
|
433
|
-
: null
|
|
434
|
-
|
|
435
|
-
let baseClassNameStr = !baseClassNames ? '' : baseClassNames.join(' ')
|
|
436
|
-
|
|
437
|
-
if (baseFontFamily) {
|
|
438
|
-
baseClassNameStr = `font_${baseFontFamily}${baseClassNameStr ? ` ${baseClassNameStr}` : ''}`
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
// add t_group_<name> for elements with a static group="<name>" literal so
|
|
442
|
-
// descendants' @container <name> rules can match (runtime path normally
|
|
443
|
-
// appends this class; extracted elements bypass it).
|
|
444
|
-
if (staticGroupName) {
|
|
445
|
-
baseClassNameStr = `t_group_${staticGroupName}${baseClassNameStr ? ` ${baseClassNameStr}` : ''}`
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
// add is_View or is_Text base class matching runtime behavior
|
|
449
|
-
const baseTypeClass = staticConfig.isText ? 'is_Text' : 'is_View'
|
|
450
|
-
baseClassNameStr = `${baseTypeClass}${baseClassNameStr ? ` ${baseClassNameStr}` : ''}`
|
|
451
|
-
|
|
452
|
-
// add component name class (skip 'Text' since is_Text already covers it)
|
|
453
|
-
const componentNameFinal = staticConfig.componentName
|
|
454
|
-
let base =
|
|
455
|
-
componentNameFinal && componentNameFinal !== 'Text'
|
|
456
|
-
? t.stringLiteral(
|
|
457
|
-
`is_${componentNameFinal}${baseClassNameStr ? ` ${baseClassNameStr}` : ''}`
|
|
458
|
-
)
|
|
459
|
-
: t.stringLiteral(baseClassNameStr || '')
|
|
460
|
-
|
|
461
|
-
attrClassName = attrClassName as t.Expression | null // actual typescript bug, flatMap doesn't update from never
|
|
462
|
-
|
|
463
|
-
const baseClassNameExpression: t.Expression = (() => {
|
|
464
|
-
if (attrClassName) {
|
|
465
|
-
if (t.isStringLiteral(attrClassName)) {
|
|
466
|
-
return t.stringLiteral(
|
|
467
|
-
base.value ? `${base.value} ${attrClassName.value}` : attrClassName.value
|
|
468
|
-
)
|
|
469
|
-
} else {
|
|
470
|
-
// space after to ensure its a string and its spaced
|
|
471
|
-
return t.binaryExpression(
|
|
472
|
-
'+',
|
|
473
|
-
t.binaryExpression('+', attrClassName, spaceString),
|
|
474
|
-
base
|
|
475
|
-
)
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
return base
|
|
480
|
-
})()
|
|
481
|
-
|
|
482
|
-
const expandedTernaries: Ternary[] = []
|
|
483
|
-
|
|
484
|
-
if (onlyTernaries.length) {
|
|
485
|
-
// normalize tests to reduce duplicates
|
|
486
|
-
const normalizedTernaries = normalizeTernaries(onlyTernaries)
|
|
487
|
-
|
|
488
|
-
for (const ternary of normalizedTernaries) {
|
|
489
|
-
if (!expandedTernaries.length) {
|
|
490
|
-
expandTernary(ternary)
|
|
491
|
-
continue
|
|
492
|
-
}
|
|
493
|
-
// snapshot current array before iterating - expandTernary mutates expandedTernaries
|
|
494
|
-
const prevTernaries = [...expandedTernaries]
|
|
495
|
-
for (const prev of prevTernaries) {
|
|
496
|
-
expandTernary(ternary, prev)
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
function expandTernary(ternary: Ternary, prev?: Ternary) {
|
|
502
|
-
// need to diverge into two (or four if alternate)
|
|
503
|
-
if (ternary.consequent && Object.keys(ternary.consequent).length) {
|
|
504
|
-
const fontFamily = getFontFamilyNameFromProps(ternary.consequent)
|
|
505
|
-
|
|
506
|
-
expandedTernaries.push({
|
|
507
|
-
fontFamily,
|
|
508
|
-
// prevTest && test: merge consequent
|
|
509
|
-
test: prev
|
|
510
|
-
? t.logicalExpression('&&', prev.test, ternary.test)
|
|
511
|
-
: ternary.test,
|
|
512
|
-
consequent: prev
|
|
513
|
-
? mergeProps(prev.consequent!, ternary.consequent)
|
|
514
|
-
: ternary.consequent,
|
|
515
|
-
remove,
|
|
516
|
-
alternate: null,
|
|
517
|
-
})
|
|
518
|
-
|
|
519
|
-
if (prev) {
|
|
520
|
-
expandedTernaries.push({
|
|
521
|
-
fontFamily,
|
|
522
|
-
// !prevTest && test: just consequent
|
|
523
|
-
test: t.logicalExpression(
|
|
524
|
-
'&&',
|
|
525
|
-
t.unaryExpression('!', prev.test),
|
|
526
|
-
ternary.test
|
|
527
|
-
),
|
|
528
|
-
consequent: ternary.consequent,
|
|
529
|
-
alternate: null,
|
|
530
|
-
remove,
|
|
531
|
-
})
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
if (ternary.alternate && Object.keys(ternary.alternate).length) {
|
|
536
|
-
const fontFamily = getFontFamilyNameFromProps(ternary.alternate)
|
|
537
|
-
const negated = t.unaryExpression('!', ternary.test)
|
|
538
|
-
expandedTernaries.push({
|
|
539
|
-
fontFamily,
|
|
540
|
-
// prevTest && !test: merge alternate
|
|
541
|
-
test: prev ? t.logicalExpression('&&', prev.test, negated) : negated,
|
|
542
|
-
consequent: prev
|
|
543
|
-
? mergeProps(prev.alternate!, ternary.alternate)
|
|
544
|
-
: ternary.alternate,
|
|
545
|
-
remove,
|
|
546
|
-
alternate: null,
|
|
547
|
-
})
|
|
548
|
-
|
|
549
|
-
if (prev) {
|
|
550
|
-
expandedTernaries.push({
|
|
551
|
-
fontFamily,
|
|
552
|
-
test: t.logicalExpression(
|
|
553
|
-
'&&',
|
|
554
|
-
t.unaryExpression('!', prev.test),
|
|
555
|
-
ternary.test
|
|
556
|
-
),
|
|
557
|
-
consequent: ternary.alternate,
|
|
558
|
-
remove,
|
|
559
|
-
alternate: null,
|
|
560
|
-
})
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
let ternaryClassNameExpr: t.Expression | null = null
|
|
566
|
-
|
|
567
|
-
// next: create all CSS, build className strings and hoist, and create final node with props
|
|
568
|
-
if (hasTernaries) {
|
|
569
|
-
for (const ternary of expandedTernaries) {
|
|
570
|
-
if (!ternary.consequent) continue
|
|
571
|
-
const classNames = addStyles(ternary.consequent)
|
|
572
|
-
if (ternary.fontFamily) {
|
|
573
|
-
classNames.unshift(`font_${ternary.fontFamily}`)
|
|
574
|
-
}
|
|
575
|
-
const baseString = t.isStringLiteral(baseClassNameExpression)
|
|
576
|
-
? baseClassNameExpression.value
|
|
577
|
-
: ''
|
|
578
|
-
|
|
579
|
-
const fullClassNameWithDups =
|
|
580
|
-
(baseString ? `${baseString} ` : '') + classNames.join(' ')
|
|
581
|
-
|
|
582
|
-
// we concat here as the base could be conditionally overriden by our classNames
|
|
583
|
-
const fullClassName = concatClassName(fullClassNameWithDups)
|
|
584
|
-
|
|
585
|
-
const classNameLiteral = t.stringLiteral(fullClassName)
|
|
586
|
-
|
|
587
|
-
if (!ternaryClassNameExpr) {
|
|
588
|
-
ternaryClassNameExpr = t.conditionalExpression(
|
|
589
|
-
ternary.test,
|
|
590
|
-
classNameLiteral,
|
|
591
|
-
baseClassNameExpression
|
|
592
|
-
)
|
|
593
|
-
} else {
|
|
594
|
-
ternaryClassNameExpr = t.conditionalExpression(
|
|
595
|
-
ternary.test,
|
|
596
|
-
classNameLiteral,
|
|
597
|
-
ternaryClassNameExpr
|
|
598
|
-
)
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
let finalExpression: t.Expression | null =
|
|
604
|
-
ternaryClassNameExpr || baseClassNameExpression || null
|
|
605
|
-
|
|
606
|
-
if (shouldPrintDebug) {
|
|
607
|
-
console.info('attrs', JSON.stringify(attrs, null, 2))
|
|
608
|
-
console.info('expandedTernaries', JSON.stringify(expandedTernaries, null, 2))
|
|
609
|
-
console.info('finalExpression', JSON.stringify(finalExpression, null, 2))
|
|
610
|
-
console.info({ hasTernaries, baseClassNameExpression })
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
if (finalExpression) {
|
|
614
|
-
// hoist to global variables
|
|
615
|
-
finalExpression = hoistClassNames(jsxPath, finalExpression)
|
|
616
|
-
|
|
617
|
-
// console.log('finalExpression', finalExpression)
|
|
618
|
-
|
|
619
|
-
const classNameProp = t.jsxAttribute(
|
|
620
|
-
t.jsxIdentifier('className'),
|
|
621
|
-
t.jsxExpressionContainer(finalExpression!)
|
|
622
|
-
)
|
|
623
|
-
finalAttrs.unshift(classNameProp)
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
node.attributes = finalAttrs
|
|
627
|
-
},
|
|
628
|
-
})
|
|
629
|
-
|
|
630
|
-
if (!res || (!res.modified && !res.optimized && !res.flattened && !res.styled)) {
|
|
631
|
-
if (shouldPrintDebug) {
|
|
632
|
-
console.info('no res or none modified', res)
|
|
633
|
-
}
|
|
634
|
-
return null
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
const styles = Array.from(cssMap.values())
|
|
638
|
-
.map((x) => x.css)
|
|
639
|
-
.join('\n')
|
|
640
|
-
.trim()
|
|
641
|
-
|
|
642
|
-
// @ts-ignore
|
|
643
|
-
const result = generate(
|
|
644
|
-
ast as any,
|
|
645
|
-
{
|
|
646
|
-
concise: false,
|
|
647
|
-
filename: sourcePath,
|
|
648
|
-
// this makes the debug output terrible, and i think sourcemap works already
|
|
649
|
-
retainLines: false,
|
|
650
|
-
sourceFileName: sourcePath,
|
|
651
|
-
sourceMaps: true,
|
|
652
|
-
},
|
|
653
|
-
source
|
|
654
|
-
)
|
|
655
|
-
|
|
656
|
-
if (shouldPrintDebug) {
|
|
657
|
-
console.info(
|
|
658
|
-
'\n -------- output code ------- \n\n',
|
|
659
|
-
result.code
|
|
660
|
-
.split('\n')
|
|
661
|
-
.filter((x) => !x.startsWith('//'))
|
|
662
|
-
.join('\n')
|
|
663
|
-
)
|
|
664
|
-
console.info('\n -------- output style -------- \n\n', styles)
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
printLog(res)
|
|
668
|
-
|
|
669
|
-
return {
|
|
670
|
-
ast,
|
|
671
|
-
styles,
|
|
672
|
-
js: result.code,
|
|
673
|
-
map: result.map,
|
|
674
|
-
stats: {
|
|
675
|
-
styled: res.styled,
|
|
676
|
-
flattened: res.flattened,
|
|
677
|
-
optimized: res.optimized,
|
|
678
|
-
found: res.found,
|
|
679
|
-
},
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
function hoistClassNames(path: NodePath<t.JSXElement>, expr: t.Expression) {
|
|
684
|
-
if (t.isStringLiteral(expr)) {
|
|
685
|
-
return hoistClassName(path, expr.value)
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
if (t.isLogicalExpression(expr)) {
|
|
689
|
-
const left = t.isStringLiteral(expr.left)
|
|
690
|
-
? hoistClassName(path, expr.left.value)
|
|
691
|
-
: expr.left
|
|
692
|
-
const right = t.isStringLiteral(expr.right)
|
|
693
|
-
? hoistClassName(path, expr.right.value)
|
|
694
|
-
: hoistClassNames(path, expr.right)
|
|
695
|
-
return t.logicalExpression(expr.operator, left, right)
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
if (t.isConditionalExpression(expr)) {
|
|
699
|
-
const cons = t.isStringLiteral(expr.consequent)
|
|
700
|
-
? hoistClassName(path, expr.consequent.value)
|
|
701
|
-
: hoistClassNames(path, expr.consequent)
|
|
702
|
-
|
|
703
|
-
const alt = t.isStringLiteral(expr.alternate)
|
|
704
|
-
? hoistClassName(path, expr.alternate.value)
|
|
705
|
-
: hoistClassNames(path, expr.alternate)
|
|
706
|
-
|
|
707
|
-
return t.conditionalExpression(expr.test, cons, alt)
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
return expr
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
function hoistClassName(path: NodePath<t.JSXElement>, str: string) {
|
|
714
|
-
const uid = path.scope.generateUidIdentifier('cn')
|
|
715
|
-
const parent = path.findParent((path) => path.isProgram())
|
|
716
|
-
if (!parent) throw new Error(`no program?`)
|
|
717
|
-
const variable = t.variableDeclaration('const', [
|
|
718
|
-
t.variableDeclarator(uid, t.stringLiteral(cleanupClassName(str))),
|
|
719
|
-
])
|
|
720
|
-
// @ts-ignore
|
|
721
|
-
parent.unshiftContainer('body', variable)
|
|
722
|
-
return uid
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
function cleanupClassName(inStr: string) {
|
|
726
|
-
const out = new Set<string>()
|
|
727
|
-
for (const part of inStr.split(' ')) {
|
|
728
|
-
if (!part || part === ' ') continue
|
|
729
|
-
if (part === 'font_') continue
|
|
730
|
-
out.add(part)
|
|
731
|
-
}
|
|
732
|
-
return [...out].join(' ')
|
|
733
|
-
}
|