@tamagui/static 1.74.3 → 1.74.5
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/require.js +89 -0
- package/dist/cjs/require.js.map +6 -0
- package/dist/cjs/require.native.js +90 -0
- package/dist/cjs/require.native.js.map +6 -0
- package/dist/esm/constants.native.js +10 -0
- package/dist/esm/constants.native.js.map +6 -0
- package/dist/esm/extractor/accessSafe.native.js +23 -0
- package/dist/esm/extractor/accessSafe.native.js.map +6 -0
- package/dist/esm/extractor/babelParse.native.js +35 -0
- package/dist/esm/extractor/babelParse.native.js.map +6 -0
- package/dist/esm/extractor/buildClassName.native.js +48 -0
- package/dist/esm/extractor/buildClassName.native.js.map +6 -0
- package/dist/esm/extractor/bundle.native.js +93 -0
- package/dist/esm/extractor/bundle.native.js.map +6 -0
- package/dist/esm/extractor/bundleConfig.native.js +301 -0
- package/dist/esm/extractor/bundleConfig.native.js.map +6 -0
- package/dist/esm/extractor/createEvaluator.native.js +35 -0
- package/dist/esm/extractor/createEvaluator.native.js.map +6 -0
- package/dist/esm/extractor/createExtractor.native.js +1095 -0
- package/dist/esm/extractor/createExtractor.native.js.map +6 -0
- package/dist/esm/extractor/ensureImportingConcat.native.js +26 -0
- package/dist/esm/extractor/ensureImportingConcat.native.js.map +6 -0
- package/dist/esm/extractor/esbuildAliasPlugin.native.js +20 -0
- package/dist/esm/extractor/esbuildAliasPlugin.native.js.map +6 -0
- package/dist/esm/extractor/evaluateAstNode.native.js +75 -0
- package/dist/esm/extractor/evaluateAstNode.native.js.map +6 -0
- package/dist/esm/extractor/extractHelpers.native.js +93 -0
- package/dist/esm/extractor/extractHelpers.native.js.map +6 -0
- package/dist/esm/extractor/extractMediaStyle.native.js +100 -0
- package/dist/esm/extractor/extractMediaStyle.native.js.map +6 -0
- package/dist/esm/extractor/extractToClassNames.native.js +290 -0
- package/dist/esm/extractor/extractToClassNames.native.js.map +6 -0
- package/dist/esm/extractor/findTopmostFunction.native.js +16 -0
- package/dist/esm/extractor/findTopmostFunction.native.js.map +6 -0
- package/dist/esm/extractor/generateTamaguiStudioConfig.native.js +93 -0
- package/dist/esm/extractor/generateTamaguiStudioConfig.native.js.map +6 -0
- package/dist/esm/extractor/generatedUid.native.js +18 -0
- package/dist/esm/extractor/generatedUid.native.js.map +6 -0
- package/dist/esm/extractor/getPrefixLogs.native.js +7 -0
- package/dist/esm/extractor/getPrefixLogs.native.js.map +6 -0
- package/dist/esm/extractor/getPropValueFromAttributes.native.js +43 -0
- package/dist/esm/extractor/getPropValueFromAttributes.native.js.map +6 -0
- package/dist/esm/extractor/getSourceModule.native.js +42 -0
- package/dist/esm/extractor/getSourceModule.native.js.map +6 -0
- package/dist/esm/extractor/getStaticBindingsForScope.native.js +127 -0
- package/dist/esm/extractor/getStaticBindingsForScope.native.js.map +6 -0
- package/dist/esm/extractor/getTamaguiConfigPathFromOptionsConfig.native.js +8 -0
- package/dist/esm/extractor/getTamaguiConfigPathFromOptionsConfig.native.js.map +6 -0
- package/dist/esm/extractor/hoistClassNames.native.js +37 -0
- package/dist/esm/extractor/hoistClassNames.native.js.map +6 -0
- package/dist/esm/extractor/literalToAst.native.js +66 -0
- package/dist/esm/extractor/literalToAst.native.js.map +6 -0
- package/dist/esm/extractor/loadFile.native.js +14 -0
- package/dist/esm/extractor/loadFile.native.js.map +6 -0
- package/dist/esm/extractor/loadTamagui.native.js +248 -0
- package/dist/esm/extractor/loadTamagui.native.js.map +6 -0
- package/dist/esm/extractor/logLines.native.js +14 -0
- package/dist/esm/extractor/logLines.native.js.map +6 -0
- package/dist/esm/extractor/normalizeTernaries.native.js +37 -0
- package/dist/esm/extractor/normalizeTernaries.native.js.map +6 -0
- package/dist/esm/extractor/propsToFontFamilyCache.native.js +18 -0
- package/dist/esm/extractor/propsToFontFamilyCache.native.js.map +6 -0
- package/dist/esm/extractor/removeUnusedHooks.native.js +51 -0
- package/dist/esm/extractor/removeUnusedHooks.native.js.map +6 -0
- package/dist/esm/extractor/timer.native.js +22 -0
- package/dist/esm/extractor/timer.native.js.map +6 -0
- package/dist/esm/extractor/validHTMLAttributes.native.js +52 -0
- package/dist/esm/extractor/validHTMLAttributes.native.js.map +6 -0
- package/dist/esm/getPragmaOptions.native.js +22 -0
- package/dist/esm/getPragmaOptions.native.js.map +6 -0
- package/dist/esm/helpers/memoize.native.js +17 -0
- package/dist/esm/helpers/memoize.native.js.map +6 -0
- package/dist/esm/helpers/requireTamaguiCore.native.js +9 -0
- package/dist/esm/helpers/requireTamaguiCore.native.js.map +6 -0
- package/dist/esm/index.native.js +3 -0
- package/dist/esm/index.native.js.map +6 -0
- package/dist/esm/registerRequire.native.js +83 -0
- package/dist/esm/registerRequire.native.js.map +6 -0
- package/dist/esm/require.js +66 -0
- package/dist/esm/require.js.map +6 -0
- package/dist/esm/setup.native.js +2 -0
- package/dist/esm/setup.native.js.map +6 -0
- package/dist/esm/static.native.js +15 -0
- package/dist/esm/static.native.js.map +6 -0
- package/dist/esm/types.native.js +1 -0
- package/dist/esm/types.native.js.map +6 -0
- package/dist/esm/webpackPlugin.native.js +11 -0
- package/dist/esm/webpackPlugin.native.js.map +6 -0
- package/package.json +16 -16
@@ -0,0 +1,1095 @@
|
|
1
|
+
import { basename, relative } from "path";
|
2
|
+
import traverse from "@babel/traverse";
|
3
|
+
import * as t from "@babel/types";
|
4
|
+
import { Color, colorLog } from "@tamagui/cli-color";
|
5
|
+
import { createDOMProps } from "react-native-web-internals";
|
6
|
+
import { FAILED_EVAL } from "../constants";
|
7
|
+
import { requireTamaguiCore } from "../helpers/requireTamaguiCore";
|
8
|
+
import { createEvaluator, createSafeEvaluator } from "./createEvaluator";
|
9
|
+
import { evaluateAstNode } from "./evaluateAstNode";
|
10
|
+
import {
|
11
|
+
attrStr,
|
12
|
+
findComponentName,
|
13
|
+
getValidComponent,
|
14
|
+
getValidComponentsPaths,
|
15
|
+
getValidImport,
|
16
|
+
isPresent,
|
17
|
+
isValidImport,
|
18
|
+
objToStr
|
19
|
+
} from "./extractHelpers";
|
20
|
+
import { findTopmostFunction } from "./findTopmostFunction";
|
21
|
+
import { cleanupBeforeExit, getStaticBindingsForScope } from "./getStaticBindingsForScope";
|
22
|
+
import { literalToAst } from "./literalToAst";
|
23
|
+
import { loadTamagui, loadTamaguiSync } from "./loadTamagui";
|
24
|
+
import { logLines } from "./logLines";
|
25
|
+
import { normalizeTernaries } from "./normalizeTernaries";
|
26
|
+
import { setPropsToFontFamily } from "./propsToFontFamilyCache";
|
27
|
+
import { removeUnusedHooks } from "./removeUnusedHooks";
|
28
|
+
import { timer } from "./timer";
|
29
|
+
import { validHTMLAttributes } from "./validHTMLAttributes";
|
30
|
+
const UNTOUCHED_PROPS = {
|
31
|
+
key: !0,
|
32
|
+
style: !0,
|
33
|
+
className: !0
|
34
|
+
}, INLINE_EXTRACTABLE = {
|
35
|
+
ref: "ref",
|
36
|
+
key: "key"
|
37
|
+
}, validHooks = {
|
38
|
+
useMedia: !0,
|
39
|
+
useTheme: !0
|
40
|
+
}, createTernary = (x) => x;
|
41
|
+
let hasLoggedBaseInfo = !1;
|
42
|
+
function isFullyDisabled(props) {
|
43
|
+
return props.disableExtraction && props.disableDebugAttr;
|
44
|
+
}
|
45
|
+
function createExtractor({ logger = console } = { logger: console }) {
|
46
|
+
const componentState = {
|
47
|
+
focus: !1,
|
48
|
+
hover: !1,
|
49
|
+
unmounted: !0,
|
50
|
+
press: !1,
|
51
|
+
pressIn: !1
|
52
|
+
}, styleProps = {
|
53
|
+
resolveValues: "value",
|
54
|
+
noClassNames: !1,
|
55
|
+
isAnimated: !1
|
56
|
+
}, shouldAddDebugProp = (
|
57
|
+
// really basic disable this for next.js because it messes with ssr
|
58
|
+
!process.env.npm_package_dependencies_next && !1
|
59
|
+
);
|
60
|
+
let projectInfo = null;
|
61
|
+
function loadSync(props) {
|
62
|
+
return isFullyDisabled(props) ? null : projectInfo ||= loadTamaguiSync(props);
|
63
|
+
}
|
64
|
+
async function load(props) {
|
65
|
+
return isFullyDisabled(props) ? null : projectInfo ||= await loadTamagui(props);
|
66
|
+
}
|
67
|
+
return {
|
68
|
+
options: {
|
69
|
+
logger
|
70
|
+
},
|
71
|
+
cleanupBeforeExit,
|
72
|
+
loadTamagui: load,
|
73
|
+
loadTamaguiSync: loadSync,
|
74
|
+
getTamagui() {
|
75
|
+
return projectInfo?.tamaguiConfig;
|
76
|
+
},
|
77
|
+
parseSync: (f, props) => {
|
78
|
+
const projectInfo2 = loadSync(props);
|
79
|
+
return parseWithConfig(projectInfo2 || {}, f, props);
|
80
|
+
},
|
81
|
+
parse: async (f, props) => {
|
82
|
+
const projectInfo2 = await load(props);
|
83
|
+
return parseWithConfig(projectInfo2 || {}, f, props);
|
84
|
+
}
|
85
|
+
};
|
86
|
+
function parseWithConfig({ components, tamaguiConfig }, fileOrPath, options) {
|
87
|
+
const {
|
88
|
+
config = "tamagui.config.ts",
|
89
|
+
importsWhitelist = ["constants.js"],
|
90
|
+
evaluateVars = !0,
|
91
|
+
sourcePath = "",
|
92
|
+
onExtractTag,
|
93
|
+
onStyleRule,
|
94
|
+
getFlattenedNode,
|
95
|
+
disable,
|
96
|
+
disableExtraction,
|
97
|
+
disableExtractInlineMedia,
|
98
|
+
disableExtractVariables,
|
99
|
+
disableDebugAttr,
|
100
|
+
enableDynamicEvaluation = !1,
|
101
|
+
includeExtensions = [".ts", ".tsx", ".jsx"],
|
102
|
+
extractStyledDefinitions = !1,
|
103
|
+
prefixLogs,
|
104
|
+
excludeProps,
|
105
|
+
platform,
|
106
|
+
...restProps
|
107
|
+
} = options;
|
108
|
+
if (sourcePath.includes(".tamagui-dynamic-eval"))
|
109
|
+
return null;
|
110
|
+
const {
|
111
|
+
expandStylesAndRemoveNullishValues,
|
112
|
+
getSplitStyles,
|
113
|
+
mediaQueryConfig,
|
114
|
+
propMapper,
|
115
|
+
proxyThemeVariables,
|
116
|
+
pseudoDescriptors
|
117
|
+
} = requireTamaguiCore(platform);
|
118
|
+
let shouldPrintDebug = options.shouldPrintDebug || !1;
|
119
|
+
if (disable === !0 || Array.isArray(disable) && disable.includes(sourcePath))
|
120
|
+
return null;
|
121
|
+
if (!isFullyDisabled(options) && !components)
|
122
|
+
throw new Error("Must provide components");
|
123
|
+
if (sourcePath && includeExtensions && !includeExtensions.some((ext) => sourcePath.endsWith(ext)))
|
124
|
+
return shouldPrintDebug && logger.info(
|
125
|
+
`Ignoring file due to includeExtensions: ${sourcePath}, includeExtensions: ${includeExtensions.join(
|
126
|
+
", "
|
127
|
+
)}`
|
128
|
+
), null;
|
129
|
+
function isValidStyleKey(name, staticConfig) {
|
130
|
+
if (!projectInfo)
|
131
|
+
throw new Error("Tamagui extractor not loaded yet");
|
132
|
+
return platform === "native" && name[0] === "$" && mediaQueryConfig[name.slice(1)] ? !1 : !!(staticConfig.validStyles?.[name] || pseudoDescriptors[name] || // dont disable variants or else you lose many things flattening
|
133
|
+
staticConfig.variants?.[name] || projectInfo?.tamaguiConfig?.shorthands[name] || name[0] === "$" && mediaQueryConfig[name.slice(1)]);
|
134
|
+
}
|
135
|
+
const isTargetingHTML = platform === "web", ogDebug = shouldPrintDebug, tm = timer(), propsWithFileInfo = {
|
136
|
+
...options,
|
137
|
+
sourcePath,
|
138
|
+
allLoadedComponents: components ? [...components] : []
|
139
|
+
};
|
140
|
+
hasLoggedBaseInfo || (hasLoggedBaseInfo = !0, shouldPrintDebug && logger.info(
|
141
|
+
[
|
142
|
+
"loaded components:",
|
143
|
+
propsWithFileInfo.allLoadedComponents.map((comp) => Object.keys(comp.nameToInfo).join(", ")).join(", ")
|
144
|
+
].join(" ")
|
145
|
+
), process.env.DEBUG?.startsWith("tamagui") && logger.info(
|
146
|
+
[
|
147
|
+
"loaded:",
|
148
|
+
JSON.stringify(propsWithFileInfo.allLoadedComponents, null, 2)
|
149
|
+
].join(`
|
150
|
+
`)
|
151
|
+
)), tm.mark("load-tamagui", !!shouldPrintDebug), isFullyDisabled(options) || tamaguiConfig?.themes || (console.error(
|
152
|
+
'\u26D4\uFE0F Error: Missing "themes" in your tamagui.config file, this may be due to duplicated dependency versions. Try out https://github.com/bmish/check-dependency-version-consistency to see if there are mis-matches, or search your lockfile.'
|
153
|
+
), console.log(" Got config:", tamaguiConfig), process.exit(0));
|
154
|
+
const firstThemeName = Object.keys(tamaguiConfig?.themes || {})[0], firstTheme = tamaguiConfig?.themes[firstThemeName] || {};
|
155
|
+
(!firstTheme || typeof firstTheme != "object") && (console.error("Missing theme, an error occurred when importing your config"), console.log("Got config:", tamaguiConfig), console.log("Looking for theme:", firstThemeName), process.exit(0));
|
156
|
+
const proxiedTheme = proxyThemeVariables(firstTheme), themeAccessListeners = /* @__PURE__ */ new Set(), defaultTheme = new Proxy(proxiedTheme, {
|
157
|
+
get(target, key) {
|
158
|
+
return Reflect.has(target, key) && themeAccessListeners.forEach((cb) => cb(String(key))), Reflect.get(target, key);
|
159
|
+
}
|
160
|
+
}), body = fileOrPath.type === "Program" ? fileOrPath.get("body") : fileOrPath.program.body;
|
161
|
+
isFullyDisabled(options) || Object.keys(components || []).length === 0 && (console.warn(
|
162
|
+
"Warning: Tamagui didn't find any valid components (DEBUG=tamagui for more)"
|
163
|
+
), process.env.DEBUG === "tamagui" && console.log("components", Object.keys(components || []), components)), shouldPrintDebug === "verbose" && logger.info(
|
164
|
+
`allLoadedComponent modules ${propsWithFileInfo.allLoadedComponents.map((k) => k.moduleName).join(", ")}`
|
165
|
+
);
|
166
|
+
let doesUseValidImport = !1, hasImportedTheme = !1;
|
167
|
+
const importDeclarations = [];
|
168
|
+
for (const bodyPath of body) {
|
169
|
+
if (bodyPath.type !== "ImportDeclaration")
|
170
|
+
continue;
|
171
|
+
const node = "node" in bodyPath ? bodyPath.node : bodyPath, moduleName = node.source.value, valid = isValidImport(propsWithFileInfo, moduleName);
|
172
|
+
if (valid && importDeclarations.push(node), extractStyledDefinitions && valid && node.specifiers.some((specifier) => specifier.local.name === "styled")) {
|
173
|
+
doesUseValidImport = !0;
|
174
|
+
break;
|
175
|
+
}
|
176
|
+
if (valid) {
|
177
|
+
const names = node.specifiers.map((specifier) => specifier.local.name), isValidComponent = names.some(
|
178
|
+
(name) => !!(isValidImport(propsWithFileInfo, moduleName, name) || validHooks[name])
|
179
|
+
);
|
180
|
+
if (shouldPrintDebug === "verbose" && logger.info(
|
181
|
+
` - import ${isValidComponent ? "\u2705" : "\u21E3"} - ${names.join(
|
182
|
+
", "
|
183
|
+
)} from '${moduleName}' - (valid: ${JSON.stringify(
|
184
|
+
getValidComponentsPaths(propsWithFileInfo)
|
185
|
+
)})`
|
186
|
+
), isValidComponent) {
|
187
|
+
doesUseValidImport = !0;
|
188
|
+
break;
|
189
|
+
}
|
190
|
+
}
|
191
|
+
}
|
192
|
+
if (shouldPrintDebug && logger.info(
|
193
|
+
`${JSON.stringify({ doesUseValidImport, hasImportedTheme }, null, 2)}
|
194
|
+
`
|
195
|
+
), !doesUseValidImport)
|
196
|
+
return null;
|
197
|
+
function getValidImportedComponent(componentName) {
|
198
|
+
const importDeclaration = importDeclarations.find(
|
199
|
+
(dec) => dec.specifiers.some((spec) => spec.local.name === componentName)
|
200
|
+
);
|
201
|
+
return importDeclaration ? getValidImport(
|
202
|
+
propsWithFileInfo,
|
203
|
+
importDeclaration.source.value,
|
204
|
+
componentName
|
205
|
+
) : null;
|
206
|
+
}
|
207
|
+
tm.mark("import-check", !!shouldPrintDebug);
|
208
|
+
let couldntParse = !1;
|
209
|
+
const modifiedComponents = /* @__PURE__ */ new Set(), bindingCache = {}, callTraverse = (a) => fileOrPath.type === "File" ? traverse(fileOrPath, a) : fileOrPath.traverse(a), shouldDisableExtraction = disableExtraction === !0 || Array.isArray(disableExtraction) && disableExtraction.includes(sourcePath);
|
210
|
+
let programPath = null;
|
211
|
+
const res = {
|
212
|
+
styled: 0,
|
213
|
+
flattened: 0,
|
214
|
+
optimized: 0,
|
215
|
+
modified: 0,
|
216
|
+
found: 0
|
217
|
+
}, version = `${Math.random()}`;
|
218
|
+
if (callTraverse({
|
219
|
+
// @ts-ignore
|
220
|
+
Program: {
|
221
|
+
enter(path) {
|
222
|
+
programPath = path;
|
223
|
+
}
|
224
|
+
},
|
225
|
+
// styled() calls
|
226
|
+
CallExpression(path) {
|
227
|
+
if (disable || shouldDisableExtraction || extractStyledDefinitions === !1 || !t.isIdentifier(path.node.callee) || path.node.callee.name !== "styled")
|
228
|
+
return;
|
229
|
+
const variableName = t.isVariableDeclarator(path.parent) && t.isIdentifier(path.parent.id) ? path.parent.id.name : "unknown", parentNode = path.node.arguments[0];
|
230
|
+
if (!t.isIdentifier(parentNode))
|
231
|
+
return;
|
232
|
+
const parentName = parentNode.name, definition = path.node.arguments[1];
|
233
|
+
if (!parentName || !definition || !t.isObjectExpression(definition))
|
234
|
+
return;
|
235
|
+
let Component = getValidImportedComponent(variableName);
|
236
|
+
if (!Component) {
|
237
|
+
if (enableDynamicEvaluation !== !0)
|
238
|
+
return;
|
239
|
+
try {
|
240
|
+
shouldPrintDebug && logger.info(
|
241
|
+
`Unknown component: ${variableName} = styled(${parentName}) attempting dynamic load: ${sourcePath}`
|
242
|
+
);
|
243
|
+
const out2 = loadTamaguiSync({
|
244
|
+
forceExports: !0,
|
245
|
+
components: [sourcePath],
|
246
|
+
cacheKey: version
|
247
|
+
});
|
248
|
+
if (!out2?.components) {
|
249
|
+
shouldPrintDebug && logger.info(`Couldn't load, got ${out2}`);
|
250
|
+
return;
|
251
|
+
}
|
252
|
+
if (propsWithFileInfo.allLoadedComponents = [
|
253
|
+
...propsWithFileInfo.allLoadedComponents,
|
254
|
+
...out2.components
|
255
|
+
], Component = out2.components.flatMap((x) => x.nameToInfo[variableName] ?? [])[0], !out2.cached) {
|
256
|
+
const foundNames = out2.components?.map((x) => Object.keys(x.nameToInfo).join(", ")).join(", ").trim();
|
257
|
+
foundNames && colorLog(
|
258
|
+
Color.FgYellow,
|
259
|
+
` | Tamagui found dynamic components: ${foundNames}`
|
260
|
+
);
|
261
|
+
}
|
262
|
+
} catch {
|
263
|
+
shouldPrintDebug && logger.info(
|
264
|
+
`skip optimize styled(${variableName}), unable to pre-process (DEBUG=tamagui for more)`
|
265
|
+
);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
if (!Component) {
|
269
|
+
shouldPrintDebug && logger.info(" No component found");
|
270
|
+
return;
|
271
|
+
}
|
272
|
+
const componentSkipProps = /* @__PURE__ */ new Set([
|
273
|
+
...Component.staticConfig.inlineWhenUnflattened || [],
|
274
|
+
...Component.staticConfig.inlineProps || [],
|
275
|
+
...Component.staticConfig.deoptProps || [],
|
276
|
+
// for now skip variants, will return to them
|
277
|
+
"variants",
|
278
|
+
"defaultVariants",
|
279
|
+
// skip fontFamily its basically a "variant", important for theme use to be value always
|
280
|
+
"fontFamily",
|
281
|
+
"name",
|
282
|
+
"focusStyle",
|
283
|
+
"hoverStyle",
|
284
|
+
"pressStyle"
|
285
|
+
]), skipped = /* @__PURE__ */ new Set(), styles = {}, staticNamespace = getStaticBindingsForScope(
|
286
|
+
path.scope,
|
287
|
+
importsWhitelist,
|
288
|
+
sourcePath,
|
289
|
+
bindingCache,
|
290
|
+
shouldPrintDebug
|
291
|
+
), attemptEval = evaluateVars ? createEvaluator({
|
292
|
+
props: propsWithFileInfo,
|
293
|
+
staticNamespace,
|
294
|
+
sourcePath,
|
295
|
+
shouldPrintDebug
|
296
|
+
}) : evaluateAstNode, attemptEvalSafe = createSafeEvaluator(attemptEval);
|
297
|
+
for (const property of definition.properties) {
|
298
|
+
if (!t.isObjectProperty(property) || !t.isIdentifier(property.key) || !isValidStyleKey(property.key.name, Component.staticConfig) || // TODO make pseudos and variants work
|
299
|
+
// skip pseudos
|
300
|
+
pseudoDescriptors[property.key.name] || // skip variants
|
301
|
+
Component.staticConfig.variants?.[property.key.name] || componentSkipProps.has(property.key.name)) {
|
302
|
+
skipped.add(property);
|
303
|
+
continue;
|
304
|
+
}
|
305
|
+
const out2 = attemptEvalSafe(property.value);
|
306
|
+
out2 === FAILED_EVAL ? skipped.add(property) : styles[property.key.name] = out2;
|
307
|
+
}
|
308
|
+
const out = getSplitStyles(
|
309
|
+
styles,
|
310
|
+
Component.staticConfig,
|
311
|
+
defaultTheme,
|
312
|
+
"",
|
313
|
+
componentState,
|
314
|
+
styleProps,
|
315
|
+
void 0,
|
316
|
+
void 0,
|
317
|
+
void 0,
|
318
|
+
shouldPrintDebug
|
319
|
+
), classNames = {
|
320
|
+
...out.classNames
|
321
|
+
};
|
322
|
+
if (shouldPrintDebug && logger.info(
|
323
|
+
[
|
324
|
+
`Extracted styled(${variableName})
|
325
|
+
`,
|
326
|
+
JSON.stringify(styles, null, 2),
|
327
|
+
`
|
328
|
+
classNames:`,
|
329
|
+
JSON.stringify(classNames, null, 2),
|
330
|
+
`
|
331
|
+
rulesToInsert:`,
|
332
|
+
out.rulesToInsert.flatMap((rule) => rule.rules).join(`
|
333
|
+
`)
|
334
|
+
].join(" ")
|
335
|
+
), definition.properties = definition.properties.map((prop) => {
|
336
|
+
if (skipped.has(prop) || !t.isObjectProperty(prop) || !t.isIdentifier(prop.key))
|
337
|
+
return prop;
|
338
|
+
const key = prop.key.name, value = classNames[key];
|
339
|
+
return value ? t.objectProperty(t.stringLiteral(key), t.stringLiteral(value)) : prop;
|
340
|
+
}), out.rulesToInsert)
|
341
|
+
for (const { identifier, rules } of out.rulesToInsert)
|
342
|
+
onStyleRule?.(identifier, rules);
|
343
|
+
res.styled++, shouldPrintDebug && logger.info(`Extracted styled(${variableName})`);
|
344
|
+
},
|
345
|
+
JSXElement(traversePath) {
|
346
|
+
tm.mark("jsx-element", !!shouldPrintDebug);
|
347
|
+
const node = traversePath.node.openingElement, ogAttributes = node.attributes.map((attr) => ({ ...attr })), componentName = findComponentName(traversePath.scope), closingElement = traversePath.node.closingElement;
|
348
|
+
if (t.isJSXMemberExpression(closingElement?.name) || !t.isJSXIdentifier(node.name))
|
349
|
+
return;
|
350
|
+
const binding = traversePath.scope.getBinding(node.name.name);
|
351
|
+
let moduleName = "";
|
352
|
+
if (binding && t.isImportDeclaration(binding.path.parent) && (moduleName = binding.path.parent.source.value, !isValidImport(propsWithFileInfo, moduleName, binding.identifier.name))) {
|
353
|
+
shouldPrintDebug && logger.info(
|
354
|
+
` - Binding for ${componentName} not internal import or from components ${binding.identifier.name} in ${moduleName}`
|
355
|
+
);
|
356
|
+
return;
|
357
|
+
}
|
358
|
+
const component = getValidComponent(propsWithFileInfo, moduleName, node.name.name);
|
359
|
+
if (!component || !component.staticConfig) {
|
360
|
+
shouldPrintDebug && logger.info(` - No Tamagui conf on this: ${node.name.name}`);
|
361
|
+
return;
|
362
|
+
}
|
363
|
+
const originalNodeName = node.name.name;
|
364
|
+
res.found++;
|
365
|
+
const filePath = `./${relative(process.cwd(), sourcePath)}`, lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "", codePosition = `${filePath}:${lineNumbers}`, debugPropValue = node.attributes.filter(
|
366
|
+
(n) => t.isJSXAttribute(n) && t.isJSXIdentifier(n.name) && n.name.name === "debug"
|
367
|
+
).map((n) => n.value === null ? !0 : t.isStringLiteral(n.value) ? n.value.value : !1)[0];
|
368
|
+
if (debugPropValue && (shouldPrintDebug = debugPropValue), shouldPrintDebug && (logger.info(`
|
369
|
+
`), logger.info(
|
370
|
+
`\x1B[33m%s\x1B[0m ${componentName} | ${codePosition} -------------------`
|
371
|
+
), logger.info(["\x1B[1m", "\x1B[32m", `<${originalNodeName} />`, disableDebugAttr ? "" : "\u{1F41B}"].join(" "))), shouldAddDebugProp && !disableDebugAttr && (res.modified++, node.attributes.unshift(
|
372
|
+
t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(node.name.name))
|
373
|
+
), componentName && node.attributes.unshift(
|
374
|
+
t.jsxAttribute(t.jsxIdentifier("data-in"), t.stringLiteral(componentName))
|
375
|
+
), node.attributes.unshift(
|
376
|
+
t.jsxAttribute(
|
377
|
+
t.jsxIdentifier("data-at"),
|
378
|
+
t.stringLiteral(`${basename(filePath)}:${lineNumbers}`)
|
379
|
+
)
|
380
|
+
)), shouldDisableExtraction) {
|
381
|
+
shouldPrintDebug === "verbose" && console.log(" Extraction disabled");
|
382
|
+
return;
|
383
|
+
}
|
384
|
+
try {
|
385
|
+
let evaluateAttribute2 = function(path) {
|
386
|
+
const attribute = path.node, attr = { type: "attr", value: attribute };
|
387
|
+
if (t.isJSXSpreadAttribute(attribute)) {
|
388
|
+
const arg = attribute.argument, conditional = t.isConditionalExpression(arg) ? (
|
389
|
+
// <YStack {...isSmall ? { color: 'red } : { color: 'blue }}
|
390
|
+
[arg.test, arg.consequent, arg.alternate]
|
391
|
+
) : t.isLogicalExpression(arg) && arg.operator === "&&" ? (
|
392
|
+
// <YStack {...isSmall && { color: 'red }}
|
393
|
+
[arg.left, arg.right, null]
|
394
|
+
) : null;
|
395
|
+
if (conditional) {
|
396
|
+
const [test, alt, cons] = conditional;
|
397
|
+
if (!test)
|
398
|
+
throw new Error("no test");
|
399
|
+
return [alt, cons].some((side) => side && !isStaticObject2(side)) ? (shouldPrintDebug && logger.info(`not extractable ${alt} ${cons}`), attr) : [
|
400
|
+
...createTernariesFromObjectProperties2(test, alt) || [],
|
401
|
+
...cons && createTernariesFromObjectProperties2(
|
402
|
+
t.unaryExpression("!", test),
|
403
|
+
cons
|
404
|
+
) || []
|
405
|
+
].map((ternary) => ({
|
406
|
+
type: "ternary",
|
407
|
+
value: ternary
|
408
|
+
}));
|
409
|
+
}
|
410
|
+
}
|
411
|
+
if (t.isJSXSpreadAttribute(attribute) || !attribute.name || typeof attribute.name.name != "string")
|
412
|
+
return shouldPrintDebug && logger.info(" ! inlining, spread attr"), inlined.set(`${Math.random()}`, "spread"), attr;
|
413
|
+
const name = attribute.name.name;
|
414
|
+
if (excludeProps?.has(name))
|
415
|
+
return shouldPrintDebug && logger.info([" excluding prop", name].join(" ")), null;
|
416
|
+
if (inlineProps.has(name))
|
417
|
+
return inlined.set(name, name), shouldPrintDebug && logger.info([" ! inlining, inline prop", name].join(" ")), attr;
|
418
|
+
if (deoptProps.has(name))
|
419
|
+
return shouldDeopt = !0, inlined.set(name, name), shouldPrintDebug && logger.info([" ! inlining, deopted prop", name].join(" ")), attr;
|
420
|
+
if (UNTOUCHED_PROPS[name])
|
421
|
+
return attr;
|
422
|
+
if (INLINE_EXTRACTABLE[name])
|
423
|
+
return inlined.set(name, INLINE_EXTRACTABLE[name]), attr;
|
424
|
+
if (name.startsWith("data-"))
|
425
|
+
return attr;
|
426
|
+
if (name[0] === "$" && t.isJSXExpressionContainer(attribute?.value)) {
|
427
|
+
const shortname = name.slice(1);
|
428
|
+
if (mediaQueryConfig[shortname]) {
|
429
|
+
if (platform === "native" && (shouldDeopt = !0), disableExtractInlineMedia)
|
430
|
+
return attr;
|
431
|
+
const expression = attribute.value.expression;
|
432
|
+
if (!t.isJSXEmptyExpression(expression)) {
|
433
|
+
const ternaries2 = createTernariesFromObjectProperties2(
|
434
|
+
t.stringLiteral(shortname),
|
435
|
+
expression,
|
436
|
+
{
|
437
|
+
inlineMediaQuery: shortname
|
438
|
+
}
|
439
|
+
);
|
440
|
+
if (ternaries2)
|
441
|
+
return ternaries2.map((value2) => ({
|
442
|
+
type: "ternary",
|
443
|
+
value: value2
|
444
|
+
}));
|
445
|
+
}
|
446
|
+
}
|
447
|
+
}
|
448
|
+
const [value, valuePath] = (() => t.isJSXExpressionContainer(attribute?.value) ? [attribute.value.expression, path.get("value")] : [attribute.value, path.get("value")])(), remove = () => {
|
449
|
+
Array.isArray(valuePath) ? valuePath.map((p) => p.remove()) : valuePath.remove();
|
450
|
+
};
|
451
|
+
if (name === "ref")
|
452
|
+
return shouldPrintDebug && logger.info([" ! inlining, ref", name].join(" ")), inlined.set("ref", "ref"), attr;
|
453
|
+
if (name === "tag")
|
454
|
+
return {
|
455
|
+
type: "attr",
|
456
|
+
value: path.node
|
457
|
+
};
|
458
|
+
if (disableExtractVariables === !0 && value && value.type === "StringLiteral" && value.value[0] === "$")
|
459
|
+
return shouldPrintDebug && logger.info(
|
460
|
+
[
|
461
|
+
` ! inlining, native disable extract: ${name} =`,
|
462
|
+
value.value
|
463
|
+
].join(" ")
|
464
|
+
), inlined.set(name, !0), attr;
|
465
|
+
if (name === "theme")
|
466
|
+
return inlined.set("theme", attr.value), attr;
|
467
|
+
const styleValue = attemptEvalSafe(value);
|
468
|
+
if (!variants[name] && !isValidStyleKey(name, staticConfig)) {
|
469
|
+
let keys = [name], out = null;
|
470
|
+
out = propMapper(name, styleValue, propMapperStyleState), out && (Array.isArray(out) ? (out = Object.fromEntries(out), keys = Object.keys(out)) : (logger.warn("Error expected array but got", out), couldntParse = !0, shouldDeopt = !0)), out && (isTargetingHTML && (out = createDOMProps(isTextView ? "span" : "div", out), delete out.className), keys = Object.keys(out));
|
471
|
+
let didInline = !1;
|
472
|
+
const attributes = keys.map((key) => {
|
473
|
+
const val = out[key];
|
474
|
+
return isValidStyleKey(key, staticConfig) ? {
|
475
|
+
type: "style",
|
476
|
+
value: { [key]: styleValue },
|
477
|
+
name: key,
|
478
|
+
attr: path.node
|
479
|
+
} : validHTMLAttributes[key] || key.startsWith("aria-") || key.startsWith("data-") || // this is debug stuff added by vite / new jsx transform
|
480
|
+
key === "__source" || key === "__self" ? attr : (shouldPrintDebug && logger.info(" ! inlining, non-static " + key), didInline = !0, inlined.set(key, val), val);
|
481
|
+
});
|
482
|
+
return didInline ? (shouldPrintDebug && logger.info(` bailing flattening due to attributes ${attributes}`), attr) : attributes;
|
483
|
+
}
|
484
|
+
if (styleValue !== FAILED_EVAL)
|
485
|
+
return inlineWhenUnflattened.has(name) && (inlineWhenUnflattenedOGVals[name] = { styleValue, attr }), isValidStyleKey(name, staticConfig) ? (shouldPrintDebug && logger.info(` style: ${name} = ${styleValue}`), name in defaultProps || hasSetOptimized || (res.optimized++, hasSetOptimized = !0), {
|
486
|
+
type: "style",
|
487
|
+
value: { [name]: styleValue },
|
488
|
+
name,
|
489
|
+
attr: path.node
|
490
|
+
}) : (variants[name] && variantValues.set(name, styleValue), inlined.set(name, !0), attr);
|
491
|
+
if (t.isBinaryExpression(value)) {
|
492
|
+
shouldPrintDebug && logger.info(` binary expression ${name} = ${value}`);
|
493
|
+
const { operator, left, right } = value, lVal = attemptEvalSafe(left), rVal = attemptEvalSafe(right);
|
494
|
+
if (shouldPrintDebug && logger.info(
|
495
|
+
` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`
|
496
|
+
), lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
|
497
|
+
const ternary = addBinaryConditional(operator, left, right);
|
498
|
+
if (ternary)
|
499
|
+
return ternary;
|
500
|
+
}
|
501
|
+
if (rVal !== FAILED_EVAL && t.isConditionalExpression(left)) {
|
502
|
+
const ternary = addBinaryConditional(operator, right, left);
|
503
|
+
if (ternary)
|
504
|
+
return ternary;
|
505
|
+
}
|
506
|
+
return shouldPrintDebug && logger.info(" evalBinaryExpression cant extract"), inlined.set(name, !0), attr;
|
507
|
+
}
|
508
|
+
const staticConditional = getStaticConditional(value);
|
509
|
+
if (staticConditional)
|
510
|
+
return shouldPrintDebug === "verbose" && logger.info(` static conditional ${name} ${value}`), { type: "ternary", value: staticConditional };
|
511
|
+
const staticLogical = getStaticLogical(value);
|
512
|
+
if (staticLogical)
|
513
|
+
return shouldPrintDebug === "verbose" && logger.info(` static ternary ${name} = ${value}`), { type: "ternary", value: staticLogical };
|
514
|
+
return inlined.set(name, !0), shouldPrintDebug && logger.info(` ! inline no match ${name} ${value}`), attr;
|
515
|
+
function addBinaryConditional(operator, staticExpr, cond) {
|
516
|
+
if (getStaticConditional(cond)) {
|
517
|
+
const alt = attemptEval(
|
518
|
+
t.binaryExpression(operator, staticExpr, cond.alternate)
|
519
|
+
), cons = attemptEval(
|
520
|
+
t.binaryExpression(operator, staticExpr, cond.consequent)
|
521
|
+
);
|
522
|
+
return shouldPrintDebug && logger.info([" binaryConditional", cond.test, cons, alt].join(" ")), {
|
523
|
+
type: "ternary",
|
524
|
+
value: {
|
525
|
+
test: cond.test,
|
526
|
+
remove,
|
527
|
+
alternate: { [name]: alt },
|
528
|
+
consequent: { [name]: cons }
|
529
|
+
}
|
530
|
+
};
|
531
|
+
}
|
532
|
+
return null;
|
533
|
+
}
|
534
|
+
function getStaticConditional(value2) {
|
535
|
+
if (t.isConditionalExpression(value2))
|
536
|
+
try {
|
537
|
+
const aVal = attemptEval(value2.alternate), cVal = attemptEval(value2.consequent);
|
538
|
+
if (shouldPrintDebug) {
|
539
|
+
const type = value2.test.type;
|
540
|
+
logger.info([" static ternary", type, cVal, aVal].join(" "));
|
541
|
+
}
|
542
|
+
return {
|
543
|
+
test: value2.test,
|
544
|
+
remove,
|
545
|
+
consequent: { [name]: cVal },
|
546
|
+
alternate: { [name]: aVal }
|
547
|
+
};
|
548
|
+
} catch (err) {
|
549
|
+
shouldPrintDebug && logger.info([" cant eval ternary", err.message].join(" "));
|
550
|
+
}
|
551
|
+
return null;
|
552
|
+
}
|
553
|
+
function getStaticLogical(value2) {
|
554
|
+
if (t.isLogicalExpression(value2) && value2.operator === "&&")
|
555
|
+
try {
|
556
|
+
const val = attemptEval(value2.right);
|
557
|
+
return shouldPrintDebug && logger.info([" staticLogical", value2.left, name, val].join(" ")), {
|
558
|
+
test: value2.left,
|
559
|
+
remove,
|
560
|
+
consequent: { [name]: val },
|
561
|
+
alternate: null
|
562
|
+
};
|
563
|
+
} catch (err) {
|
564
|
+
shouldPrintDebug && logger.info([" cant static eval logical", err].join(" "));
|
565
|
+
}
|
566
|
+
return null;
|
567
|
+
}
|
568
|
+
}, isStaticObject2 = function(obj) {
|
569
|
+
return t.isObjectExpression(obj) && obj.properties.every((prop) => {
|
570
|
+
if (!t.isObjectProperty(prop))
|
571
|
+
return logger.info(["not object prop", prop].join(" ")), !1;
|
572
|
+
const propName = prop.key.name;
|
573
|
+
return !isValidStyleKey(propName, staticConfig) && propName !== "tag" ? (shouldPrintDebug && logger.info([" not a valid style prop!", propName].join(" ")), !1) : !0;
|
574
|
+
});
|
575
|
+
}, createTernariesFromObjectProperties2 = function(test, side, ternaryPartial = {}) {
|
576
|
+
if (!side)
|
577
|
+
return null;
|
578
|
+
if (!isStaticObject2(side))
|
579
|
+
throw new Error("not extractable");
|
580
|
+
return side.properties.flatMap((property) => {
|
581
|
+
if (!t.isObjectProperty(property))
|
582
|
+
throw new Error("expected object property");
|
583
|
+
if (t.isIdentifier(property.key)) {
|
584
|
+
const key = property.key.name, mediaQueryKey = key.slice(1);
|
585
|
+
if (key[0] === "$" && mediaQueryConfig[mediaQueryKey])
|
586
|
+
if (t.isExpression(property.value)) {
|
587
|
+
const ternaries2 = createTernariesFromObjectProperties2(
|
588
|
+
t.stringLiteral(mediaQueryKey),
|
589
|
+
property.value,
|
590
|
+
{
|
591
|
+
inlineMediaQuery: mediaQueryKey
|
592
|
+
}
|
593
|
+
);
|
594
|
+
if (ternaries2)
|
595
|
+
return ternaries2.map((value) => ({
|
596
|
+
...ternaryPartial,
|
597
|
+
...value,
|
598
|
+
// ensure media query test stays on left side (see getMediaQueryTernary)
|
599
|
+
test: t.logicalExpression("&&", value.test, test)
|
600
|
+
}));
|
601
|
+
logger.info(["\u26A0\uFE0F no ternaries?", property].join(" "));
|
602
|
+
} else
|
603
|
+
logger.info(["\u26A0\uFE0F not expression", property].join(" "));
|
604
|
+
}
|
605
|
+
if (t.isConditionalExpression(property.value)) {
|
606
|
+
const [truthy, falsy] = [
|
607
|
+
t.objectExpression([
|
608
|
+
t.objectProperty(property.key, property.value.consequent)
|
609
|
+
]),
|
610
|
+
t.objectExpression([
|
611
|
+
t.objectProperty(property.key, property.value.alternate)
|
612
|
+
])
|
613
|
+
].map((x) => attemptEval(x));
|
614
|
+
return [
|
615
|
+
createTernary({
|
616
|
+
remove() {
|
617
|
+
},
|
618
|
+
...ternaryPartial,
|
619
|
+
test: t.logicalExpression("&&", test, property.value.test),
|
620
|
+
consequent: truthy,
|
621
|
+
alternate: null
|
622
|
+
}),
|
623
|
+
createTernary({
|
624
|
+
...ternaryPartial,
|
625
|
+
test: t.logicalExpression(
|
626
|
+
"&&",
|
627
|
+
test,
|
628
|
+
t.unaryExpression("!", property.value.test)
|
629
|
+
),
|
630
|
+
consequent: falsy,
|
631
|
+
alternate: null,
|
632
|
+
remove() {
|
633
|
+
}
|
634
|
+
})
|
635
|
+
];
|
636
|
+
}
|
637
|
+
const obj = t.objectExpression([
|
638
|
+
t.objectProperty(property.key, property.value)
|
639
|
+
]), consequent = attemptEval(obj);
|
640
|
+
return createTernary({
|
641
|
+
remove() {
|
642
|
+
},
|
643
|
+
...ternaryPartial,
|
644
|
+
test,
|
645
|
+
consequent,
|
646
|
+
alternate: null
|
647
|
+
});
|
648
|
+
});
|
649
|
+
}, mergeToEnd2 = function(obj, key, val) {
|
650
|
+
key in obj && delete obj[key], obj[key] = val;
|
651
|
+
}, expandStylesAndRemoveNullishValuesWithoutVariants2 = function(style) {
|
652
|
+
let res2 = {};
|
653
|
+
for (const key in style)
|
654
|
+
if (staticConfig.variants && key in staticConfig.variants)
|
655
|
+
mergeToEnd2(res2, key, style[key]);
|
656
|
+
else {
|
657
|
+
const expanded = expandStylesAndRemoveNullishValues({ [key]: style[key] });
|
658
|
+
for (const key2 in expanded)
|
659
|
+
mergeToEnd2(res2, key2, expanded[key2]);
|
660
|
+
}
|
661
|
+
return res2;
|
662
|
+
}, mergeStyles2 = function(prev2, next) {
|
663
|
+
for (const key in next)
|
664
|
+
pseudoDescriptors[key] ? (prev2[key] = prev2[key] || {}, Object.assign(prev2[key], next[key])) : mergeToEnd2(prev2, key, next[key]);
|
665
|
+
};
|
666
|
+
var evaluateAttribute = evaluateAttribute2, isStaticObject = isStaticObject2, createTernariesFromObjectProperties = createTernariesFromObjectProperties2, mergeToEnd = mergeToEnd2, expandStylesAndRemoveNullishValuesWithoutVariants = expandStylesAndRemoveNullishValuesWithoutVariants2, mergeStyles = mergeStyles2;
|
667
|
+
const { staticConfig } = component, defaultProps = { ...staticConfig.defaultProps || {} }, variants = staticConfig.variants || {}, isTextView = staticConfig.isText || !1, validStyles = staticConfig?.validStyles ?? {};
|
668
|
+
let tagName = defaultProps.tag ?? (isTextView ? "span" : "div");
|
669
|
+
traversePath.get("openingElement").get("attributes").forEach((path) => {
|
670
|
+
const attr = path.node;
|
671
|
+
if (t.isJSXSpreadAttribute(attr) || attr.name.name !== "tag")
|
672
|
+
return;
|
673
|
+
const val = attr.value;
|
674
|
+
t.isStringLiteral(val) && (tagName = val.value);
|
675
|
+
}), shouldPrintDebug === "verbose" && console.log(` Start tag ${tagName}`);
|
676
|
+
const flatNode = getFlattenedNode?.({ isTextView, tag: tagName }), inlineProps = /* @__PURE__ */ new Set([
|
677
|
+
// adding some always inline props
|
678
|
+
"dataSet",
|
679
|
+
...restProps.inlineProps || [],
|
680
|
+
...staticConfig.inlineProps || []
|
681
|
+
]), deoptProps = /* @__PURE__ */ new Set([
|
682
|
+
// always de-opt animation these
|
683
|
+
"animation",
|
684
|
+
"disableOptimization",
|
685
|
+
// when using a non-CSS driver, de-opt on enterStyle/exitStyle
|
686
|
+
...tamaguiConfig?.animations.isReactNative ? ["enterStyle", "exitStyle"] : [],
|
687
|
+
...restProps.deoptProps || [],
|
688
|
+
...staticConfig.deoptProps || []
|
689
|
+
]), inlineWhenUnflattened = /* @__PURE__ */ new Set([
|
690
|
+
...staticConfig.inlineWhenUnflattened || []
|
691
|
+
]), staticNamespace = getStaticBindingsForScope(
|
692
|
+
traversePath.scope,
|
693
|
+
importsWhitelist,
|
694
|
+
sourcePath,
|
695
|
+
bindingCache,
|
696
|
+
shouldPrintDebug
|
697
|
+
), attemptEval = evaluateVars ? createEvaluator({
|
698
|
+
props: propsWithFileInfo,
|
699
|
+
staticNamespace,
|
700
|
+
sourcePath,
|
701
|
+
traversePath,
|
702
|
+
shouldPrintDebug
|
703
|
+
}) : evaluateAstNode, attemptEvalSafe = createSafeEvaluator(attemptEval);
|
704
|
+
if (shouldPrintDebug && logger.info(` staticNamespace ${Object.keys(staticNamespace).join(", ")}`), couldntParse)
|
705
|
+
return;
|
706
|
+
tm.mark("jsx-element-flattened", !!shouldPrintDebug);
|
707
|
+
let attrs = [], shouldDeopt = !1;
|
708
|
+
const inlined = /* @__PURE__ */ new Map(), variantValues = /* @__PURE__ */ new Map();
|
709
|
+
let hasSetOptimized = !1;
|
710
|
+
const inlineWhenUnflattenedOGVals = {}, propMapperStyleState = {
|
711
|
+
staticConfig,
|
712
|
+
usedKeys: {},
|
713
|
+
classNames: {},
|
714
|
+
style: {},
|
715
|
+
theme: defaultTheme,
|
716
|
+
viewProps: defaultProps,
|
717
|
+
conf: tamaguiConfig,
|
718
|
+
curProps: defaultProps,
|
719
|
+
props: defaultProps,
|
720
|
+
componentState,
|
721
|
+
styleProps: {
|
722
|
+
...styleProps,
|
723
|
+
resolveValues: "auto"
|
724
|
+
},
|
725
|
+
debug: shouldPrintDebug
|
726
|
+
};
|
727
|
+
if (attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
|
728
|
+
try {
|
729
|
+
const res2 = evaluateAttribute2(path);
|
730
|
+
return tm.mark("jsx-element-evaluate-attr", !!shouldPrintDebug), res2 || path.remove(), res2;
|
731
|
+
} catch (err) {
|
732
|
+
return shouldPrintDebug && (logger.info(
|
733
|
+
[
|
734
|
+
"Recoverable error extracting attribute",
|
735
|
+
err.message,
|
736
|
+
shouldPrintDebug === "verbose" ? err.stack : ""
|
737
|
+
].join(" ")
|
738
|
+
), shouldPrintDebug === "verbose" && logger.info(`node ${path.node?.type}`)), inlined.set(`${Math.random()}`, "spread"), {
|
739
|
+
type: "attr",
|
740
|
+
value: path.node
|
741
|
+
};
|
742
|
+
}
|
743
|
+
}).flat(4).filter(isPresent), shouldPrintDebug && logger.info(
|
744
|
+
[` - attrs (before):
|
745
|
+
`, logLines(attrs.map(attrStr).join(", "))].join(" ")
|
746
|
+
), couldntParse || shouldDeopt) {
|
747
|
+
shouldPrintDebug && logger.info(
|
748
|
+
[" avoid optimizing:", { couldntParse, shouldDeopt }].join(" ")
|
749
|
+
), node.attributes = ogAttributes;
|
750
|
+
return;
|
751
|
+
}
|
752
|
+
const parentFn = findTopmostFunction(traversePath);
|
753
|
+
parentFn && modifiedComponents.add(parentFn);
|
754
|
+
let ternaries = [];
|
755
|
+
attrs = attrs.reduce((out, cur) => {
|
756
|
+
const next = attrs[attrs.indexOf(cur) + 1];
|
757
|
+
if (cur.type === "ternary" && ternaries.push(cur.value), (!next || next.type !== "ternary") && ternaries.length) {
|
758
|
+
const normalized = normalizeTernaries(ternaries).map(
|
759
|
+
({ alternate, consequent, ...rest }) => ({
|
760
|
+
type: "ternary",
|
761
|
+
value: {
|
762
|
+
...rest,
|
763
|
+
alternate: alternate || null,
|
764
|
+
consequent: consequent || null
|
765
|
+
}
|
766
|
+
})
|
767
|
+
);
|
768
|
+
try {
|
769
|
+
return [...out, ...normalized];
|
770
|
+
} finally {
|
771
|
+
shouldPrintDebug && logger.info(
|
772
|
+
` normalizeTernaries (${ternaries.length} => ${normalized.length})`
|
773
|
+
), ternaries = [];
|
774
|
+
}
|
775
|
+
}
|
776
|
+
return cur.type === "ternary" || out.push(cur), out;
|
777
|
+
}, []).flat();
|
778
|
+
const hasSpread = attrs.some(
|
779
|
+
(x) => x.type === "attr" && t.isJSXSpreadAttribute(x.value)
|
780
|
+
), hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText")), themeVal = inlined.get("theme");
|
781
|
+
platform !== "native" && inlined.delete("theme");
|
782
|
+
for (const [key] of [...inlined]) {
|
783
|
+
const isStaticObjectVariant = staticConfig.variants?.[key] && variantValues.has(key);
|
784
|
+
(INLINE_EXTRACTABLE[key] || isStaticObjectVariant) && inlined.delete(key);
|
785
|
+
}
|
786
|
+
const canFlattenProps = inlined.size === 0;
|
787
|
+
let shouldFlatten = !!(flatNode && !shouldDeopt && canFlattenProps && !hasSpread && !staticConfig.isStyledHOC && !staticConfig.isHOC && !staticConfig.isReactNative && staticConfig.neverFlatten !== !0 && (staticConfig.neverFlatten !== "jsx" || hasOnlyStringChildren));
|
788
|
+
const shouldWrapTheme = shouldFlatten && themeVal, usedThemeKeys = /* @__PURE__ */ new Set();
|
789
|
+
if (disableExtractVariables && themeAccessListeners.add((key) => {
|
790
|
+
shouldFlatten = !1, usedThemeKeys.add(key), shouldPrintDebug === "verbose" && logger.info([" ! accessing theme key, avoid flatten", key].join(" "));
|
791
|
+
}), shouldPrintDebug)
|
792
|
+
try {
|
793
|
+
logger.info([" flatten?", shouldFlatten, objToStr({ hasSpread, shouldDeopt, canFlattenProps, shouldWrapTheme, hasOnlyStringChildren }), "inlined", inlined.size, [...inlined]].join(" "));
|
794
|
+
} catch {
|
795
|
+
}
|
796
|
+
if (shouldFlatten && shouldWrapTheme && (programPath ? (shouldPrintDebug && logger.info([" - wrapping theme", themeVal].join(" ")), attrs = attrs.filter(
|
797
|
+
(x) => !(x.type === "attr" && t.isJSXAttribute(x.value) && x.value.name.name === "theme")
|
798
|
+
), hasImportedTheme || (hasImportedTheme = !0, programPath.node.body.push(
|
799
|
+
t.importDeclaration(
|
800
|
+
[
|
801
|
+
t.importSpecifier(
|
802
|
+
t.identifier("_TamaguiTheme"),
|
803
|
+
t.identifier("Theme")
|
804
|
+
)
|
805
|
+
],
|
806
|
+
t.stringLiteral("@tamagui/web")
|
807
|
+
)
|
808
|
+
)), traversePath.replaceWith(
|
809
|
+
t.jsxElement(
|
810
|
+
t.jsxOpeningElement(t.jsxIdentifier("_TamaguiTheme"), [
|
811
|
+
t.jsxAttribute(t.jsxIdentifier("name"), themeVal.value)
|
812
|
+
]),
|
813
|
+
t.jsxClosingElement(t.jsxIdentifier("_TamaguiTheme")),
|
814
|
+
[traversePath.node]
|
815
|
+
)
|
816
|
+
)) : console.warn(
|
817
|
+
`No program path found, avoiding importing flattening / importing theme in ${sourcePath}`
|
818
|
+
)), shouldFlatten) {
|
819
|
+
const defaultStyleAttrs = Object.keys(defaultProps).flatMap((key) => {
|
820
|
+
if (!isValidStyleKey(key, staticConfig))
|
821
|
+
return [];
|
822
|
+
const value = defaultProps[key], name = tamaguiConfig?.shorthands[key] || key;
|
823
|
+
if (value === void 0) {
|
824
|
+
logger.warn(
|
825
|
+
`\u26A0\uFE0F Error evaluating default style for component, prop ${key} ${value}`
|
826
|
+
), shouldDeopt = !0;
|
827
|
+
return;
|
828
|
+
}
|
829
|
+
return {
|
830
|
+
type: "style",
|
831
|
+
name,
|
832
|
+
value: { [name]: value }
|
833
|
+
};
|
834
|
+
});
|
835
|
+
defaultStyleAttrs.length && (attrs = [...defaultStyleAttrs, ...attrs]);
|
836
|
+
}
|
837
|
+
if (shouldDeopt || !shouldFlatten) {
|
838
|
+
shouldPrintDebug && logger.info(`Deopting ${shouldDeopt} ${shouldFlatten}`), node.attributes = ogAttributes;
|
839
|
+
return;
|
840
|
+
}
|
841
|
+
shouldPrintDebug && logger.info(
|
842
|
+
[` - attrs (flattened):
|
843
|
+
`, logLines(attrs.map(attrStr).join(", "))].join(
|
844
|
+
" "
|
845
|
+
)
|
846
|
+
);
|
847
|
+
let foundStaticProps = {};
|
848
|
+
for (const key in attrs) {
|
849
|
+
const cur = attrs[key];
|
850
|
+
if (cur.type === "style") {
|
851
|
+
const expanded = expandStylesAndRemoveNullishValuesWithoutVariants2(
|
852
|
+
cur.value
|
853
|
+
);
|
854
|
+
for (const key2 in expanded)
|
855
|
+
mergeToEnd2(foundStaticProps, key2, expanded[key2]);
|
856
|
+
continue;
|
857
|
+
}
|
858
|
+
if (cur.type === "attr") {
|
859
|
+
if (t.isJSXSpreadAttribute(cur.value) || !t.isJSXIdentifier(cur.value.name))
|
860
|
+
continue;
|
861
|
+
const key2 = cur.value.name.name, value = attemptEvalSafe(cur.value.value || t.booleanLiteral(!0));
|
862
|
+
value !== FAILED_EVAL && mergeToEnd2(foundStaticProps, key2, value);
|
863
|
+
}
|
864
|
+
}
|
865
|
+
const completeProps = {};
|
866
|
+
for (const key in defaultProps)
|
867
|
+
key in foundStaticProps || (completeProps[key] = defaultProps[key]);
|
868
|
+
for (const key in foundStaticProps)
|
869
|
+
completeProps[key] = foundStaticProps[key];
|
870
|
+
attrs = attrs.reduce((acc, cur) => {
|
871
|
+
if (!cur)
|
872
|
+
return acc;
|
873
|
+
if (cur.type === "attr" && !t.isJSXSpreadAttribute(cur.value) && shouldFlatten) {
|
874
|
+
const name = cur.value.name.name;
|
875
|
+
if (typeof name == "string") {
|
876
|
+
if (name === "tag")
|
877
|
+
return acc;
|
878
|
+
if (variants[name] && variantValues.has(name)) {
|
879
|
+
const styleState = {
|
880
|
+
...propMapperStyleState,
|
881
|
+
props: completeProps,
|
882
|
+
curProps: completeProps
|
883
|
+
};
|
884
|
+
let out = Object.fromEntries(
|
885
|
+
propMapper(name, variantValues.get(name), styleState) || []
|
886
|
+
);
|
887
|
+
if (out && isTargetingHTML) {
|
888
|
+
const cn = out.className;
|
889
|
+
out = createDOMProps(isTextView ? "span" : "div", out), out.className = cn;
|
890
|
+
}
|
891
|
+
shouldPrintDebug && logger.info([" - expanded variant", name, out].join(" "));
|
892
|
+
for (const key2 in out) {
|
893
|
+
const value2 = out[key2];
|
894
|
+
isValidStyleKey(key2, staticConfig) ? acc.push({
|
895
|
+
type: "style",
|
896
|
+
value: { [key2]: value2 },
|
897
|
+
name: key2,
|
898
|
+
attr: cur.value
|
899
|
+
}) : acc.push({
|
900
|
+
type: "attr",
|
901
|
+
value: t.jsxAttribute(
|
902
|
+
t.jsxIdentifier(key2),
|
903
|
+
t.jsxExpressionContainer(
|
904
|
+
typeof value2 == "string" ? t.stringLiteral(value2) : literalToAst(value2)
|
905
|
+
)
|
906
|
+
)
|
907
|
+
});
|
908
|
+
}
|
909
|
+
}
|
910
|
+
}
|
911
|
+
}
|
912
|
+
if (cur.type !== "style")
|
913
|
+
return acc.push(cur), acc;
|
914
|
+
let key = Object.keys(cur.value)[0];
|
915
|
+
const value = cur.value[key], fullKey = tamaguiConfig?.shorthands[key];
|
916
|
+
return fullKey && (cur.value = { [fullKey]: value }, key = fullKey), disableExtractVariables && value[0] === "$" && (usedThemeKeys.has(key) || usedThemeKeys.has(fullKey)) ? (shouldPrintDebug && logger.info([` keeping variable inline: ${key} =`, value].join(" ")), acc.push({
|
917
|
+
type: "attr",
|
918
|
+
value: t.jsxAttribute(
|
919
|
+
t.jsxIdentifier(key),
|
920
|
+
t.jsxExpressionContainer(t.stringLiteral(value))
|
921
|
+
)
|
922
|
+
}), acc) : (acc.push(cur), acc);
|
923
|
+
}, []), tm.mark("jsx-element-expanded", !!shouldPrintDebug), shouldPrintDebug && logger.info(
|
924
|
+
[` - attrs (expanded):
|
925
|
+
`, logLines(attrs.map(attrStr).join(", "))].join(
|
926
|
+
" "
|
927
|
+
)
|
928
|
+
);
|
929
|
+
let prev = null;
|
930
|
+
const getProps = (props, includeProps = !1, debugName = "") => {
|
931
|
+
if (!props)
|
932
|
+
return shouldPrintDebug && logger.info([" getProps() no props"].join(" ")), {};
|
933
|
+
if (excludeProps?.size)
|
934
|
+
for (const key in props)
|
935
|
+
excludeProps.has(key) && (shouldPrintDebug && logger.info([" delete excluded", key].join(" ")), delete props[key]);
|
936
|
+
try {
|
937
|
+
const out = getSplitStyles(
|
938
|
+
props,
|
939
|
+
staticConfig,
|
940
|
+
defaultTheme,
|
941
|
+
"",
|
942
|
+
componentState,
|
943
|
+
{
|
944
|
+
...styleProps,
|
945
|
+
noClassNames: !0,
|
946
|
+
fallbackProps: completeProps
|
947
|
+
},
|
948
|
+
void 0,
|
949
|
+
void 0,
|
950
|
+
void 0,
|
951
|
+
debugPropValue || shouldPrintDebug
|
952
|
+
), outProps = {
|
953
|
+
...includeProps ? out.viewProps : {},
|
954
|
+
...out.style,
|
955
|
+
...out.pseudos
|
956
|
+
};
|
957
|
+
return shouldPrintDebug && (logger.info(`(${debugName})`), logger.info(`
|
958
|
+
getProps (props in): ${logLines(objToStr(props))}`), logger.info(`
|
959
|
+
getProps (outProps): ${logLines(objToStr(outProps))}`)), out.fontFamily && (setPropsToFontFamily(outProps, out.fontFamily), shouldPrintDebug && logger.info(`
|
960
|
+
\u{1F4AC} new font fam: ${out.fontFamily}`)), outProps;
|
961
|
+
} catch (err) {
|
962
|
+
return logger.info(["error", err.message, err.stack].join(" ")), {};
|
963
|
+
}
|
964
|
+
};
|
965
|
+
shouldFlatten && attrs.unshift({
|
966
|
+
type: "style",
|
967
|
+
value: defaultProps
|
968
|
+
}), attrs = attrs.reduce((acc, cur) => {
|
969
|
+
if (cur.type === "style") {
|
970
|
+
const key = Object.keys(cur.value)[0], value = cur.value[key];
|
971
|
+
if (
|
972
|
+
// !isStyleAndAttr[key] &&
|
973
|
+
!shouldFlatten && // de-opt if non-style
|
974
|
+
!validStyles[key] && !pseudoDescriptors[key] && !(key.startsWith("data-") || key.startsWith("aria-"))
|
975
|
+
)
|
976
|
+
return shouldPrintDebug && logger.info([" - keeping as non-style", key].join(" ")), prev = cur, acc.push({
|
977
|
+
type: "attr",
|
978
|
+
value: t.jsxAttribute(
|
979
|
+
t.jsxIdentifier(key),
|
980
|
+
t.jsxExpressionContainer(
|
981
|
+
typeof value == "string" ? t.stringLiteral(value) : literalToAst(value)
|
982
|
+
)
|
983
|
+
)
|
984
|
+
}), acc.push(cur), acc;
|
985
|
+
if (prev?.type === "style")
|
986
|
+
return mergeStyles2(prev.value, cur.value), acc;
|
987
|
+
}
|
988
|
+
return prev = cur, acc.push(cur), acc;
|
989
|
+
}, []), shouldPrintDebug && logger.info(
|
990
|
+
[
|
991
|
+
` - attrs (combined \u{1F500}):
|
992
|
+
`,
|
993
|
+
logLines(attrs.map(attrStr).join(", "))
|
994
|
+
].join(" ")
|
995
|
+
);
|
996
|
+
let getStyleError = null;
|
997
|
+
for (const attr of attrs)
|
998
|
+
try {
|
999
|
+
switch (shouldPrintDebug && console.log(` Processing ${attr.type}:`), attr.type) {
|
1000
|
+
case "ternary": {
|
1001
|
+
const a = getProps(attr.value.alternate, !1, "ternary.alternate"), c = getProps(attr.value.consequent, !1, "ternary.consequent");
|
1002
|
+
a && (attr.value.alternate = a), c && (attr.value.consequent = c), shouldPrintDebug && logger.info([" => tern ", attrStr(attr)].join(" "));
|
1003
|
+
continue;
|
1004
|
+
}
|
1005
|
+
case "style": {
|
1006
|
+
const styles = getProps(attr.value, !1, "style");
|
1007
|
+
styles && (attr.value = styles), shouldPrintDebug && logger.info([" * styles (in)", logLines(objToStr(attr.value))].join(" ")), shouldPrintDebug && logger.info([" * styles (out)", logLines(objToStr(styles))].join(" "));
|
1008
|
+
continue;
|
1009
|
+
}
|
1010
|
+
case "attr":
|
1011
|
+
if (shouldFlatten && t.isJSXAttribute(attr.value)) {
|
1012
|
+
const key = attr.value.name.name;
|
1013
|
+
if (key === "style" || key === "className" || key === "tag")
|
1014
|
+
continue;
|
1015
|
+
const value = attemptEvalSafe(
|
1016
|
+
attr.value.value || t.booleanLiteral(!0)
|
1017
|
+
);
|
1018
|
+
if (value !== FAILED_EVAL) {
|
1019
|
+
const outProps = getProps({ [key]: value }, !0, `attr.${key}`), outKey = Object.keys(outProps)[0];
|
1020
|
+
if (outKey) {
|
1021
|
+
const outVal = outProps[outKey];
|
1022
|
+
attr.value = t.jsxAttribute(
|
1023
|
+
t.jsxIdentifier(outKey),
|
1024
|
+
t.jsxExpressionContainer(
|
1025
|
+
typeof outVal == "string" ? t.stringLiteral(outVal) : literalToAst(outVal)
|
1026
|
+
)
|
1027
|
+
);
|
1028
|
+
}
|
1029
|
+
}
|
1030
|
+
}
|
1031
|
+
}
|
1032
|
+
} catch (err) {
|
1033
|
+
getStyleError = err;
|
1034
|
+
}
|
1035
|
+
if (shouldPrintDebug && logger.info([` - attrs (ternaries/combined):
|
1036
|
+
`, logLines(attrs.map(attrStr).join(", "))].join(" ")), tm.mark("jsx-element-styles", !!shouldPrintDebug), getStyleError)
|
1037
|
+
return logger.info([" \u26A0\uFE0F postprocessing error, deopt", getStyleError].join(" ")), node.attributes = ogAttributes, null;
|
1038
|
+
const existingStyleKeys = /* @__PURE__ */ new Set();
|
1039
|
+
for (let i = attrs.length - 1; i >= 0; i--) {
|
1040
|
+
const attr = attrs[i];
|
1041
|
+
if (shouldFlatten && attr.type === "attr" && t.isJSXAttribute(attr.value) && t.isJSXIdentifier(attr.value.name)) {
|
1042
|
+
const name = attr.value.name.name;
|
1043
|
+
INLINE_EXTRACTABLE[name] && (attr.value.name.name = INLINE_EXTRACTABLE[name]);
|
1044
|
+
}
|
1045
|
+
if (attr.type === "style")
|
1046
|
+
for (const key in attr.value)
|
1047
|
+
existingStyleKeys.has(key) ? (shouldPrintDebug && logger.info([` >> delete existing ${key}`].join(" ")), delete attr.value[key]) : existingStyleKeys.add(key);
|
1048
|
+
}
|
1049
|
+
if (!shouldFlatten && inlineWhenUnflattened.size) {
|
1050
|
+
for (const [index, attr] of attrs.entries())
|
1051
|
+
if (attr.type === "style")
|
1052
|
+
for (const key in attr.value) {
|
1053
|
+
if (!inlineWhenUnflattened.has(key))
|
1054
|
+
continue;
|
1055
|
+
const val = inlineWhenUnflattenedOGVals[key];
|
1056
|
+
val ? (delete attr.value[key], attrs.splice(index - 1, 0, val.attr)) : delete attr.value[key];
|
1057
|
+
}
|
1058
|
+
}
|
1059
|
+
if (shouldFlatten && (shouldPrintDebug && logger.info([" [\u2705] flattening", originalNodeName, flatNode].join(" ")), node.name.name = flatNode, res.flattened++, closingElement && (closingElement.name.name = flatNode)), !shouldFlatten && platform === "native")
|
1060
|
+
return shouldPrintDebug && logger.info("Disabled flattening except for simple cases on native for now"), node.attributes = ogAttributes, null;
|
1061
|
+
shouldPrintDebug && (logger.info([` - inlined props (${inlined.size}):`, shouldDeopt ? " deopted" : "", hasSpread ? " has spread" : "", staticConfig.neverFlatten ? "neverFlatten" : ""].join(" ")), logger.info(` - shouldFlatten/isFlattened: ${shouldFlatten}`), logger.info(` - attrs (end):
|
1062
|
+
${logLines(attrs.map(attrStr).join(", "))}`)), onExtractTag({
|
1063
|
+
parserProps: propsWithFileInfo,
|
1064
|
+
attrs,
|
1065
|
+
node,
|
1066
|
+
lineNumbers,
|
1067
|
+
filePath,
|
1068
|
+
config: tamaguiConfig,
|
1069
|
+
attemptEval,
|
1070
|
+
jsxPath: traversePath,
|
1071
|
+
originalNodeName,
|
1072
|
+
isFlattened: shouldFlatten,
|
1073
|
+
programPath,
|
1074
|
+
completeProps,
|
1075
|
+
staticConfig
|
1076
|
+
});
|
1077
|
+
} catch (err) {
|
1078
|
+
node.attributes = ogAttributes, console.error(`@tamagui/static Error: ${err.message} ${err.stack}`);
|
1079
|
+
} finally {
|
1080
|
+
debugPropValue && (shouldPrintDebug = ogDebug);
|
1081
|
+
}
|
1082
|
+
}
|
1083
|
+
}), tm.mark("jsx-done", !!shouldPrintDebug), modifiedComponents.size) {
|
1084
|
+
const all = Array.from(modifiedComponents);
|
1085
|
+
shouldPrintDebug && logger.info(` [\u{1FA9D}] hook check ${all.length}`);
|
1086
|
+
for (const comp of all)
|
1087
|
+
removeUnusedHooks(comp, shouldPrintDebug);
|
1088
|
+
}
|
1089
|
+
return tm.done(shouldPrintDebug === "verbose"), res;
|
1090
|
+
}
|
1091
|
+
}
|
1092
|
+
export {
|
1093
|
+
createExtractor
|
1094
|
+
};
|
1095
|
+
//# sourceMappingURL=createExtractor.js.map
|