@tamagui/static 1.0.0-beta.179 → 1.0.0-beta.181

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/cjs/constants.js +8 -0
  2. package/dist/cjs/extractor/accessSafe.js +4 -0
  3. package/dist/cjs/extractor/babelParse.js +5 -0
  4. package/dist/cjs/extractor/babelParse.js.map +2 -2
  5. package/dist/cjs/extractor/buildClassName.js +4 -0
  6. package/dist/cjs/extractor/createEvaluator.js +5 -0
  7. package/dist/cjs/extractor/createExtractor.js +857 -730
  8. package/dist/cjs/extractor/createExtractor.js.map +2 -2
  9. package/dist/cjs/extractor/ensureImportingConcat.js +4 -0
  10. package/dist/cjs/extractor/evaluateAstNode.js +4 -0
  11. package/dist/cjs/extractor/extractHelpers.js +11 -0
  12. package/dist/cjs/extractor/extractMediaStyle.js +7 -2
  13. package/dist/cjs/extractor/extractMediaStyle.js.map +2 -2
  14. package/dist/cjs/extractor/extractToClassNames.js +12 -6
  15. package/dist/cjs/extractor/extractToClassNames.js.map +2 -2
  16. package/dist/cjs/extractor/findTopmostFunction.js +4 -0
  17. package/dist/cjs/extractor/generatedUid.js +4 -0
  18. package/dist/cjs/extractor/getPrefixLogs.js +4 -0
  19. package/dist/cjs/extractor/getPropValueFromAttributes.js +4 -0
  20. package/dist/cjs/extractor/getSourceModule.js +4 -0
  21. package/dist/cjs/extractor/getStaticBindingsForScope.js +4 -0
  22. package/dist/cjs/extractor/hoistClassNames.js +4 -0
  23. package/dist/cjs/extractor/literalToAst.js +5 -0
  24. package/dist/cjs/extractor/loadTamagui.js +67 -47
  25. package/dist/cjs/extractor/loadTamagui.js.map +2 -2
  26. package/dist/cjs/extractor/logLines.js +4 -0
  27. package/dist/cjs/extractor/normalizeTernaries.js +4 -0
  28. package/dist/cjs/extractor/removeUnusedHooks.js +4 -0
  29. package/dist/cjs/extractor/timer.js +46 -0
  30. package/dist/cjs/extractor/timer.js.map +7 -0
  31. package/dist/cjs/extractor/validHTMLAttributes.js +4 -0
  32. package/dist/cjs/index.js +6 -0
  33. package/dist/cjs/patchReactNativeWeb.js +23 -7
  34. package/dist/cjs/patchReactNativeWeb.js.map +2 -2
  35. package/dist/esm/extractor/babelParse.js.map +2 -2
  36. package/dist/esm/extractor/createExtractor.js +863 -740
  37. package/dist/esm/extractor/createExtractor.js.map +2 -2
  38. package/dist/esm/extractor/extractMediaStyle.js +2 -2
  39. package/dist/esm/extractor/extractMediaStyle.js.map +2 -2
  40. package/dist/esm/extractor/extractToClassNames.js +8 -6
  41. package/dist/esm/extractor/extractToClassNames.js.map +2 -2
  42. package/dist/esm/extractor/loadTamagui.js +63 -47
  43. package/dist/esm/extractor/loadTamagui.js.map +2 -2
  44. package/dist/esm/extractor/timer.js +24 -0
  45. package/dist/esm/extractor/timer.js.map +7 -0
  46. package/dist/esm/patchReactNativeWeb.js +19 -7
  47. package/dist/esm/patchReactNativeWeb.js.map +2 -2
  48. package/dist/jsx/extractor/createExtractor.js +863 -740
  49. package/dist/jsx/extractor/extractMediaStyle.js +2 -2
  50. package/dist/jsx/extractor/extractToClassNames.js +8 -6
  51. package/dist/jsx/extractor/loadTamagui.js +63 -47
  52. package/dist/jsx/extractor/timer.js +23 -0
  53. package/dist/jsx/patchReactNativeWeb.js +19 -7
  54. package/package.json +23 -23
  55. package/types/extractor/babelParse.d.ts.map +1 -1
  56. package/types/extractor/createExtractor.d.ts.map +1 -1
  57. package/types/extractor/extractMediaStyle.d.ts +1 -1
  58. package/types/extractor/extractMediaStyle.d.ts.map +1 -1
  59. package/types/extractor/extractToClassNames.d.ts +2 -1
  60. package/types/extractor/extractToClassNames.d.ts.map +1 -1
  61. package/types/extractor/loadTamagui.d.ts +2 -2
  62. package/types/extractor/loadTamagui.d.ts.map +1 -1
  63. package/types/extractor/timer.d.ts +5 -0
  64. package/types/extractor/timer.d.ts.map +1 -0
  65. package/types/patchReactNativeWeb.d.ts.map +1 -1
@@ -5,6 +5,7 @@ import * as t from "@babel/types";
5
5
  import {
6
6
  getSplitStyles,
7
7
  mediaQueryConfig,
8
+ normalizeStyleObject,
8
9
  pseudos,
9
10
  rnw
10
11
  } from "@tamagui/core-node";
@@ -20,12 +21,22 @@ import { loadTamagui } from "./loadTamagui";
20
21
  import { logLines } from "./logLines";
21
22
  import { normalizeTernaries } from "./normalizeTernaries";
22
23
  import { removeUnusedHooks } from "./removeUnusedHooks";
24
+ import { timer } from "./timer";
23
25
  import { validHTMLAttributes } from "./validHTMLAttributes";
24
26
  const UNTOUCHED_PROPS = {
25
27
  key: true,
26
28
  style: true,
27
29
  className: true
28
30
  };
31
+ const INLINE_EXTRACTABLE = {
32
+ ref: "ref",
33
+ key: "key",
34
+ onPress: "onClick",
35
+ onHoverIn: "onMouseEnter",
36
+ onHoverOut: "onMouseLeave",
37
+ onPressIn: "onMouseDown",
38
+ onPressOut: "onMouseUp"
39
+ };
29
40
  const isAttr = /* @__PURE__ */ __name((x) => x.type === "attr", "isAttr");
30
41
  const validHooks = {
31
42
  useMedia: true,
@@ -33,11 +44,11 @@ const validHooks = {
33
44
  };
34
45
  const createTernary = /* @__PURE__ */ __name((x) => x, "createTernary");
35
46
  function createExtractor() {
47
+ if (!process.env.TAMAGUI_TARGET) {
48
+ console.log('\u26A0\uFE0F Please set process.env.TAMAGUI_TARGET to either "web" or "native"');
49
+ process.exit(1);
50
+ }
36
51
  const shouldAddDebugProp = !process.env.npm_package_dependencies_next && process.env.TAMAGUI_TARGET !== "native" && process.env.IDENTIFY_TAGS !== "false" && (process.env.NODE_ENV === "development" || process.env.DEBUG || process.env.IDENTIFY_TAGS);
37
- require("esbuild-register/dist/node").register({
38
- target: "es2019",
39
- format: "cjs"
40
- });
41
52
  let loadedTamaguiConfig;
42
53
  let hasLogged = false;
43
54
  return {
@@ -70,10 +81,15 @@ function createExtractor() {
70
81
  if (!Array.isArray(props.components)) {
71
82
  throw new Error(`Must provide components array with list of Tamagui component modules`);
72
83
  }
84
+ const tm = timer();
73
85
  const { components, tamaguiConfig } = loadTamagui({
74
86
  config,
75
87
  components: props.components || ["tamagui"]
76
88
  });
89
+ if (shouldPrintDebug === "verbose") {
90
+ console.log("tamagui.config.ts:", { components, config });
91
+ }
92
+ tm.mark("load-tamagui", shouldPrintDebug === "verbose");
77
93
  loadedTamaguiConfig = tamaguiConfig;
78
94
  const defaultTheme = tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]];
79
95
  const body = fileOrPath.type === "Program" ? fileOrPath.get("body") : fileOrPath.program.body;
@@ -87,17 +103,26 @@ function createExtractor() {
87
103
  obj[name] = components[name];
88
104
  return obj;
89
105
  }, {});
106
+ if (shouldPrintDebug === "verbose") {
107
+ console.log("validComponents", Object.keys(validComponents));
108
+ }
90
109
  let doesUseValidImport = false;
110
+ let hasImportedTheme = false;
91
111
  for (const bodyPath of body) {
92
112
  if (bodyPath.type !== "ImportDeclaration")
93
113
  continue;
94
114
  const node = "node" in bodyPath ? bodyPath.node : bodyPath;
95
115
  const from = node.source.value;
96
- if (props.components.includes(from) || isInternalImport(from)) {
97
- if (node.specifiers.some((specifier) => {
116
+ const isValidImport = props.components.includes(from) || isInternalImport(from);
117
+ if (isValidImport) {
118
+ const isValidComponent = node.specifiers.some((specifier) => {
98
119
  const name = specifier.local.name;
99
120
  return validComponents[name] || validHooks[name];
100
- })) {
121
+ });
122
+ if (shouldPrintDebug === "verbose") {
123
+ console.log("import from", from, { isValidComponent });
124
+ }
125
+ if (isValidComponent) {
101
126
  doesUseValidImport = true;
102
127
  break;
103
128
  }
@@ -109,6 +134,7 @@ function createExtractor() {
109
134
  if (!doesUseValidImport) {
110
135
  return null;
111
136
  }
137
+ tm.mark("import-check", shouldPrintDebug === "verbose");
112
138
  let couldntParse = false;
113
139
  const modifiedComponents = /* @__PURE__ */ new Set();
114
140
  const bindingCache = {};
@@ -130,6 +156,7 @@ function createExtractor() {
130
156
  },
131
157
  JSXElement(traversePath) {
132
158
  var _a, _b, _c, _d, _e;
159
+ tm.mark("jsx-element", shouldPrintDebug === "verbose");
133
160
  const node = traversePath.node.openingElement;
134
161
  const ogAttributes = node.attributes;
135
162
  const componentName = findComponentName(traversePath.scope);
@@ -156,579 +183,771 @@ function createExtractor() {
156
183
  }
157
184
  const originalNodeName = node.name.name;
158
185
  res.found++;
159
- if (shouldPrintDebug) {
160
- console.log(`
161
- <${originalNodeName} />`);
162
- }
163
186
  const filePath = sourcePath.replace(process.cwd(), ".");
164
187
  const lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "";
165
- if (shouldAddDebugProp && !disableDebugAttr) {
166
- const preName = componentName ? `${componentName}.` : "";
167
- res.modified++;
168
- node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(`${preName}${node.name.name}@${filePath.replace("./", "")}:${lineNumbers}`)));
169
- }
170
- const shouldLog = !hasLogged;
171
- if (shouldLog) {
172
- const prefix = " |";
173
- console.log(prefixLogs || prefix, " total \xB7 optimized \xB7 flattened ");
174
- hasLogged = true;
188
+ const preName = componentName ? `${componentName}.` : "";
189
+ const tagId = `${preName}${node.name.name}@${filePath.replace("./", "")}:${lineNumbers}`;
190
+ const hasBooleanDebugTrue = node.attributes.some((n) => t.isJSXAttribute(n) && t.isJSXIdentifier(n.name) && n.name.name === "debug" && n.value === null);
191
+ if (hasBooleanDebugTrue) {
192
+ shouldPrintDebug = true;
175
193
  }
176
- if (disableExtraction) {
177
- return;
178
- }
179
- const { staticConfig } = component;
180
- const isTextView = staticConfig.isText || false;
181
- const validStyles = (_a = staticConfig == null ? void 0 : staticConfig.validStyles) != null ? _a : {};
182
- function isValidStyleKey(name) {
183
- var _a2;
184
- return !!(!!validStyles[name] || !!pseudos[name] || ((_a2 = staticConfig.variants) == null ? void 0 : _a2[name]) || tamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
185
- }
186
- __name(isValidStyleKey, "isValidStyleKey");
187
- let tagName = (_c = (_b = staticConfig.defaultProps) == null ? void 0 : _b.tag) != null ? _c : isTextView ? "span" : "div";
188
- traversePath.get("openingElement").get("attributes").forEach((path) => {
189
- const attr = path.node;
190
- if (t.isJSXSpreadAttribute(attr))
191
- return;
192
- if (attr.name.name !== "tag")
193
- return;
194
- const val = attr.value;
195
- if (!t.isStringLiteral(val))
196
- return;
197
- tagName = val.value;
198
- });
199
- const flatNode = getFlattenedNode({ isTextView, tag: tagName });
200
- const inlineProps = /* @__PURE__ */ new Set([
201
- ...props.inlineProps || [],
202
- ...staticConfig.inlineProps || []
203
- ]);
204
- const deoptProps = /* @__PURE__ */ new Set([
205
- ...props.deoptProps || [],
206
- ...staticConfig.deoptProps || []
207
- ]);
208
- const staticNamespace = getStaticBindingsForScope(traversePath.scope, importsWhitelist, sourcePath, bindingCache, shouldPrintDebug);
209
- const attemptEval = !evaluateVars ? evaluateAstNode : createEvaluator({
210
- tamaguiConfig,
211
- staticNamespace,
212
- sourcePath,
213
- traversePath,
214
- shouldPrintDebug
215
- });
216
- const attemptEvalSafe = createSafeEvaluator(attemptEval);
217
- if (shouldPrintDebug) {
218
- console.log(" staticNamespace", Object.keys(staticNamespace).join(", "));
219
- }
220
- const flattenedAttrs = [];
221
- traversePath.get("openingElement").get("attributes").forEach((path) => {
222
- const attr = path.node;
223
- if (!t.isJSXSpreadAttribute(attr)) {
224
- flattenedAttrs.push(attr);
225
- return;
226
- }
227
- let arg;
228
- try {
229
- arg = attemptEval(attr.argument);
230
- } catch (e) {
231
- if (shouldPrintDebug) {
232
- console.log(" couldnt parse spread", e.message);
233
- }
234
- flattenedAttrs.push(attr);
235
- return;
236
- }
237
- if (arg !== void 0) {
238
- try {
239
- if (typeof arg !== "object" || arg == null) {
240
- if (shouldPrintDebug) {
241
- console.log(" non object or null arg", arg);
242
- }
243
- flattenedAttrs.push(attr);
244
- } else {
245
- for (const k in arg) {
246
- const value = arg[k];
247
- if (!value && typeof value === "object") {
248
- console.log("shouldnt we handle this?", k, value, arg);
249
- continue;
194
+ try {
195
+ let isValidStyleKey = function(name) {
196
+ var _a2;
197
+ return !!(!!validStyles[name] || !!pseudos[name] || ((_a2 = staticConfig.variants) == null ? void 0 : _a2[name]) || tamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
198
+ }, evaluateAttribute = function(path) {
199
+ const attribute = path.node;
200
+ const attr = { type: "attr", value: attribute };
201
+ if (t.isJSXSpreadAttribute(attribute)) {
202
+ const arg = attribute.argument;
203
+ const conditional = t.isConditionalExpression(arg) ? [arg.test, arg.consequent, arg.alternate] : t.isLogicalExpression(arg) && arg.operator === "&&" ? [arg.left, arg.right, null] : null;
204
+ if (conditional) {
205
+ const [test, alt, cons] = conditional;
206
+ if (!test)
207
+ throw new Error(`no test`);
208
+ if ([alt, cons].some((side) => side && !isExtractable(side))) {
209
+ if (shouldPrintDebug) {
210
+ console.log("not extractable", alt, cons);
250
211
  }
251
- flattenedAttrs.push(t.jsxAttribute(t.jsxIdentifier(k), t.jsxExpressionContainer(literalToAst(value))));
212
+ return attr;
252
213
  }
214
+ return [
215
+ ...createTernariesFromObjectProperties(test, alt) || [],
216
+ ...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
217
+ ].map((ternary) => ({
218
+ type: "ternary",
219
+ value: ternary
220
+ }));
253
221
  }
254
- } catch (err) {
255
- console.warn("cant parse spread, caught err", err);
256
- couldntParse = true;
257
- }
258
- }
259
- });
260
- if (couldntParse) {
261
- return;
262
- }
263
- node.attributes = flattenedAttrs;
264
- if (staticConfig.defaultProps) {
265
- for (const key in staticConfig.defaultProps) {
266
- if (isValidStyleKey(key)) {
267
- continue;
268
- }
269
- const serialize = require("babel-literal-to-ast");
270
- const val = staticConfig.defaultProps[key];
271
- try {
272
- const serializedDefaultProp = serialize(val);
273
- node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(key), typeof val === "string" ? t.stringLiteral(val) : t.jsxExpressionContainer(serializedDefaultProp)));
274
- } catch (err) {
275
- console.warn(`\u26A0\uFE0F Error evaluating default prop for component ${node.name.name}, prop ${key}
276
- error: ${err}
277
- value:`, val, "\n defaultProps:", staticConfig.defaultProps);
278
- }
279
- }
280
- }
281
- let attrs = [];
282
- let shouldDeopt = false;
283
- let inlinePropCount = 0;
284
- let hasSetOptimized = false;
285
- attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
286
- try {
287
- const res2 = evaluateAttribute(path);
288
- if (!res2) {
289
- path.remove();
290
222
  }
291
- return res2;
292
- } catch (err) {
293
- if (shouldPrintDebug) {
294
- console.log("Error extracting attribute", err.message, err.stack);
295
- console.log("node", path.node);
223
+ if (t.isJSXSpreadAttribute(attribute) || !attribute.name || typeof attribute.name.name !== "string") {
224
+ if (shouldPrintDebug) {
225
+ console.log(" ! inlining, spread attr");
226
+ }
227
+ inlined.set(`${Math.random()}`, "spread");
228
+ return attr;
296
229
  }
297
- return {
298
- type: "attr",
299
- value: path.node
300
- };
301
- }
302
- }).flat(4).filter(isPresent);
303
- if (shouldPrintDebug) {
304
- console.log(" - attrs (before):\n", logLines(attrs.map(attrStr).join(", ")));
305
- }
306
- function evaluateAttribute(path) {
307
- const attribute = path.node;
308
- const attr = { type: "attr", value: attribute };
309
- if (t.isJSXSpreadAttribute(attribute)) {
310
- const arg = attribute.argument;
311
- const conditional = t.isConditionalExpression(arg) ? [arg.test, arg.consequent, arg.alternate] : t.isLogicalExpression(arg) && arg.operator === "&&" ? [arg.left, arg.right, null] : null;
312
- if (conditional) {
313
- const [test, alt, cons] = conditional;
314
- if (!test)
315
- throw new Error(`no test`);
316
- if ([alt, cons].some((side) => side && !isExtractable(side))) {
317
- if (shouldPrintDebug) {
318
- console.log("not extractable", alt, cons);
319
- }
320
- return attr;
230
+ const name = attribute.name.name;
231
+ if (excludeProps == null ? void 0 : excludeProps.has(name)) {
232
+ if (shouldPrintDebug) {
233
+ console.log(" excluding prop", name);
321
234
  }
322
- return [
323
- ...createTernariesFromObjectProperties(test, alt) || [],
324
- ...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
325
- ].map((ternary) => ({
326
- type: "ternary",
327
- value: ternary
328
- }));
235
+ return null;
329
236
  }
330
- }
331
- if (t.isJSXSpreadAttribute(attribute) || !attribute.name || typeof attribute.name.name !== "string") {
332
- if (shouldPrintDebug) {
333
- console.log(" ! inlining, spread attr");
237
+ if (inlineProps.has(name)) {
238
+ inlined.set(name, name);
239
+ if (shouldPrintDebug) {
240
+ console.log(" ! inlining, inline prop", name);
241
+ }
242
+ return attr;
334
243
  }
335
- inlinePropCount++;
336
- return attr;
337
- }
338
- const name = attribute.name.name;
339
- if (excludeProps == null ? void 0 : excludeProps.has(name)) {
340
- if (shouldPrintDebug) {
341
- console.log(" excluding prop", name);
244
+ if (deoptProps.has(name)) {
245
+ shouldDeopt = true;
246
+ inlined.set(name, name);
247
+ if (shouldPrintDebug) {
248
+ console.log(" ! inlining, deopted prop", name);
249
+ }
250
+ return attr;
342
251
  }
343
- return null;
344
- }
345
- if (inlineProps.has(name)) {
346
- inlinePropCount++;
347
- if (shouldPrintDebug) {
348
- console.log(" ! inlining, inline prop", name);
252
+ if (UNTOUCHED_PROPS[name]) {
253
+ return attr;
349
254
  }
350
- return attr;
351
- }
352
- if (deoptProps.has(name)) {
353
- shouldDeopt = true;
354
- inlinePropCount++;
355
- if (shouldPrintDebug) {
356
- console.log(" ! inlining, deopted prop", name);
255
+ if (INLINE_EXTRACTABLE[name]) {
256
+ inlined.set(name, INLINE_EXTRACTABLE[name]);
257
+ return attr;
357
258
  }
358
- return attr;
359
- }
360
- if (UNTOUCHED_PROPS[name]) {
361
- return attr;
362
- }
363
- if (name.startsWith("data-")) {
364
- return attr;
365
- }
366
- if (name[0] === "$" && t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
367
- if (disableExtractInlineMedia) {
259
+ if (name.startsWith("data-")) {
368
260
  return attr;
369
261
  }
370
- const shortname = name.slice(1);
371
- if (mediaQueryConfig[shortname]) {
372
- const expression = attribute.value.expression;
373
- if (!t.isJSXEmptyExpression(expression)) {
374
- const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(shortname), expression, {
375
- inlineMediaQuery: shortname
376
- });
377
- if (ternaries2) {
378
- return ternaries2.map((value2) => ({
379
- type: "ternary",
380
- value: value2
381
- }));
262
+ if (name[0] === "$" && t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
263
+ if (disableExtractInlineMedia) {
264
+ return attr;
265
+ }
266
+ const shortname = name.slice(1);
267
+ if (mediaQueryConfig[shortname]) {
268
+ const expression = attribute.value.expression;
269
+ if (!t.isJSXEmptyExpression(expression)) {
270
+ const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(shortname), expression, {
271
+ inlineMediaQuery: shortname
272
+ });
273
+ if (ternaries2) {
274
+ return ternaries2.map((value2) => ({
275
+ type: "ternary",
276
+ value: value2
277
+ }));
278
+ }
382
279
  }
383
280
  }
384
281
  }
385
- }
386
- const [value, valuePath] = (() => {
387
- if (t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
388
- return [attribute.value.expression, path.get("value")];
389
- } else {
390
- return [attribute.value, path.get("value")];
282
+ const [value, valuePath] = (() => {
283
+ if (t.isJSXExpressionContainer(attribute == null ? void 0 : attribute.value)) {
284
+ return [attribute.value.expression, path.get("value")];
285
+ } else {
286
+ return [attribute.value, path.get("value")];
287
+ }
288
+ })();
289
+ const remove = /* @__PURE__ */ __name(() => {
290
+ Array.isArray(valuePath) ? valuePath.map((p) => p.remove()) : valuePath.remove();
291
+ }, "remove");
292
+ if (name === "ref") {
293
+ if (shouldPrintDebug) {
294
+ console.log(" ! inlining, ref", name);
295
+ }
296
+ inlined.set("ref", "ref");
297
+ return attr;
391
298
  }
392
- })();
393
- const remove = /* @__PURE__ */ __name(() => {
394
- Array.isArray(valuePath) ? valuePath.map((p) => p.remove()) : valuePath.remove();
395
- }, "remove");
396
- if (name === "ref") {
397
- if (shouldPrintDebug) {
398
- console.log(" ! inlining, ref", name);
299
+ if (name === "tag") {
300
+ return {
301
+ type: "attr",
302
+ value: path.node
303
+ };
399
304
  }
400
- inlinePropCount++;
401
- return attr;
402
- }
403
- if (name === "tag") {
404
- return {
405
- type: "attr",
406
- value: path.node
407
- };
408
- }
409
- if (disableExtractVariables) {
410
- if (value) {
411
- if (value.type === "StringLiteral" && value.value[0] === "$") {
412
- if (shouldPrintDebug) {
413
- console.log(` ! inlining, native disable extract: ${name} =`, value.value);
305
+ if (disableExtractVariables) {
306
+ if (value) {
307
+ if (value.type === "StringLiteral" && value.value[0] === "$") {
308
+ if (shouldPrintDebug) {
309
+ console.log(` ! inlining, native disable extract: ${name} =`, value.value);
310
+ }
311
+ inlined.set(name, true);
312
+ return attr;
414
313
  }
415
- inlinePropCount++;
416
- return attr;
417
314
  }
418
315
  }
419
- }
420
- const styleValue = attemptEvalSafe(value);
421
- if (!isValidStyleKey(name)) {
422
- let keys = [name];
423
- let out = null;
424
- if (staticConfig.propMapper) {
425
- out = staticConfig.propMapper(name, styleValue, defaultTheme, staticConfig.defaultProps, staticConfig);
426
- if (out) {
427
- out = rnw.createDOMProps(isTextView ? "span" : "div", out);
428
- delete out.className;
429
- keys = Object.keys(out);
316
+ const styleValue = attemptEvalSafe(value);
317
+ if (!isValidStyleKey(name)) {
318
+ let keys = [name];
319
+ let out = null;
320
+ if (staticConfig.propMapper) {
321
+ out = staticConfig.propMapper(name, styleValue, defaultTheme, staticConfig.defaultProps, "auto");
322
+ if (out) {
323
+ out = rnw.createDOMProps(isTextView ? "span" : "div", out);
324
+ delete out.className;
325
+ keys = Object.keys(out);
326
+ }
430
327
  }
431
- }
432
- const attributes = keys.map((key) => {
433
- const val = out[key];
434
- if (!isValidStyleKey(key)) {
435
- if (validHTMLAttributes[key]) {
328
+ let didInline = false;
329
+ const attributes = keys.map((key) => {
330
+ const val = out[key];
331
+ if (key === "theme") {
332
+ inlined.set(key, val);
333
+ return attr;
334
+ }
335
+ if (isValidStyleKey(key)) {
436
336
  return {
437
- type: "attr",
438
- value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(literalToAst(val)))
337
+ type: "style",
338
+ value: { [name]: styleValue },
339
+ name,
340
+ attr: path.node
439
341
  };
440
342
  }
343
+ if (validHTMLAttributes[key]) {
344
+ return attr;
345
+ }
441
346
  if (shouldPrintDebug) {
442
347
  console.log(" ! inlining, non-static", key);
443
348
  }
444
- inlinePropCount++;
349
+ didInline = true;
350
+ inlined.set(key, val);
351
+ return val;
352
+ });
353
+ if (didInline) {
354
+ if (shouldPrintDebug) {
355
+ console.log(" bailing flattening due to attributes", attributes);
356
+ }
357
+ return attr;
445
358
  }
446
- return {
447
- type: "style",
448
- value: { [name]: styleValue },
449
- name,
450
- attr: path.node
451
- };
452
- });
453
- if (inlinePropCount) {
454
- return attr;
455
- }
456
- return attributes;
457
- }
458
- if (styleValue !== FAILED_EVAL) {
459
- if (shouldPrintDebug) {
460
- console.log(` style: ${name} =`, styleValue);
359
+ return attributes;
461
360
  }
462
- if (!(name in staticConfig.defaultProps)) {
463
- if (!hasSetOptimized) {
464
- res.optimized++;
465
- hasSetOptimized = true;
361
+ if (styleValue !== FAILED_EVAL) {
362
+ if (isValidStyleKey(name)) {
363
+ if (shouldPrintDebug) {
364
+ console.log(` style: ${name} =`, styleValue);
365
+ }
366
+ if (!(name in staticConfig.defaultProps)) {
367
+ if (!hasSetOptimized) {
368
+ res.optimized++;
369
+ hasSetOptimized = true;
370
+ }
371
+ }
372
+ return {
373
+ type: "style",
374
+ value: { [name]: styleValue },
375
+ name,
376
+ attr: path.node
377
+ };
378
+ } else {
379
+ inlined.set(name, true);
380
+ return attr;
466
381
  }
467
382
  }
468
- return {
469
- type: "style",
470
- value: { [name]: styleValue },
471
- name,
472
- attr: path.node
473
- };
474
- }
475
- if (t.isBinaryExpression(value)) {
476
- if (shouldPrintDebug) {
477
- console.log(` binary expression ${name} = `, value);
478
- }
479
- const { operator, left, right } = value;
480
- const lVal = attemptEvalSafe(left);
481
- const rVal = attemptEvalSafe(right);
482
- if (shouldPrintDebug) {
483
- console.log(` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`);
383
+ if (t.isBinaryExpression(value)) {
384
+ if (shouldPrintDebug) {
385
+ console.log(` binary expression ${name} = `, value);
386
+ }
387
+ const { operator, left, right } = value;
388
+ const lVal = attemptEvalSafe(left);
389
+ const rVal = attemptEvalSafe(right);
390
+ if (shouldPrintDebug) {
391
+ console.log(` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`);
392
+ }
393
+ if (lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
394
+ const ternary = addBinaryConditional(operator, left, right);
395
+ if (ternary)
396
+ return ternary;
397
+ }
398
+ if (rVal !== FAILED_EVAL && t.isConditionalExpression(left)) {
399
+ const ternary = addBinaryConditional(operator, right, left);
400
+ if (ternary)
401
+ return ternary;
402
+ }
403
+ if (shouldPrintDebug) {
404
+ console.log(` evalBinaryExpression cant extract`);
405
+ }
406
+ inlined.set(name, true);
407
+ return attr;
484
408
  }
485
- if (lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
486
- const ternary = addBinaryConditional(operator, left, right);
487
- if (ternary)
488
- return ternary;
409
+ const staticConditional = getStaticConditional(value);
410
+ if (staticConditional) {
411
+ if (shouldPrintDebug) {
412
+ console.log(` static conditional ${name} = `, value);
413
+ }
414
+ return { type: "ternary", value: staticConditional };
489
415
  }
490
- if (rVal !== FAILED_EVAL && t.isConditionalExpression(left)) {
491
- const ternary = addBinaryConditional(operator, right, left);
492
- if (ternary)
493
- return ternary;
416
+ const staticLogical = getStaticLogical(value);
417
+ if (staticLogical) {
418
+ if (shouldPrintDebug) {
419
+ console.log(` static ternary ${name} = `, value);
420
+ }
421
+ return { type: "ternary", value: staticLogical };
494
422
  }
423
+ inlined.set(name, true);
495
424
  if (shouldPrintDebug) {
496
- console.log(` evalBinaryExpression cant extract`);
425
+ console.log(` ! inline no match ${name}`, value);
497
426
  }
498
- inlinePropCount++;
499
427
  return attr;
500
- }
501
- const staticConditional = getStaticConditional(value);
502
- if (staticConditional) {
503
- if (shouldPrintDebug) {
504
- console.log(` static conditional ${name} = `, value);
505
- }
506
- return { type: "ternary", value: staticConditional };
507
- }
508
- const staticLogical = getStaticLogical(value);
509
- if (staticLogical) {
510
- if (shouldPrintDebug) {
511
- console.log(` static ternary ${name} = `, value);
512
- }
513
- return { type: "ternary", value: staticLogical };
514
- }
515
- if (shouldPrintDebug) {
516
- console.log(" ! inline prop via no match", name, value.type);
517
- }
518
- inlinePropCount++;
519
- if (shouldPrintDebug) {
520
- console.log(` inlining ${name} = `, value);
521
- }
522
- return attr;
523
- function addBinaryConditional(operator, staticExpr, cond) {
524
- if (getStaticConditional(cond)) {
525
- const alt = attemptEval(t.binaryExpression(operator, staticExpr, cond.alternate));
526
- const cons = attemptEval(t.binaryExpression(operator, staticExpr, cond.consequent));
527
- if (shouldPrintDebug) {
528
- console.log(" binaryConditional", cond.test, cons, alt);
529
- }
530
- return {
531
- type: "ternary",
532
- value: {
533
- test: cond.test,
534
- remove,
535
- alternate: { [name]: alt },
536
- consequent: { [name]: cons }
537
- }
538
- };
539
- }
540
- return null;
541
- }
542
- __name(addBinaryConditional, "addBinaryConditional");
543
- function getStaticConditional(value2) {
544
- if (t.isConditionalExpression(value2)) {
545
- try {
428
+ function addBinaryConditional(operator, staticExpr, cond) {
429
+ if (getStaticConditional(cond)) {
430
+ const alt = attemptEval(t.binaryExpression(operator, staticExpr, cond.alternate));
431
+ const cons = attemptEval(t.binaryExpression(operator, staticExpr, cond.consequent));
546
432
  if (shouldPrintDebug) {
547
- console.log("attempt", value2.alternate, value2.consequent);
548
- }
549
- const aVal = attemptEval(value2.alternate);
550
- const cVal = attemptEval(value2.consequent);
551
- if (shouldPrintDebug) {
552
- const type = value2.test.type;
553
- console.log(" static ternary", type, cVal, aVal);
433
+ console.log(" binaryConditional", cond.test, cons, alt);
554
434
  }
555
435
  return {
556
- test: value2.test,
557
- remove,
558
- consequent: { [name]: cVal },
559
- alternate: { [name]: aVal }
436
+ type: "ternary",
437
+ value: {
438
+ test: cond.test,
439
+ remove,
440
+ alternate: { [name]: alt },
441
+ consequent: { [name]: cons }
442
+ }
560
443
  };
561
- } catch (err) {
562
- if (shouldPrintDebug) {
563
- console.log(" cant eval ternary", err.message);
564
- }
565
444
  }
445
+ return null;
566
446
  }
567
- return null;
568
- }
569
- __name(getStaticConditional, "getStaticConditional");
570
- function getStaticLogical(value2) {
571
- if (t.isLogicalExpression(value2)) {
572
- if (value2.operator === "&&") {
447
+ __name(addBinaryConditional, "addBinaryConditional");
448
+ function getStaticConditional(value2) {
449
+ if (t.isConditionalExpression(value2)) {
573
450
  try {
574
- const val = attemptEval(value2.right);
575
451
  if (shouldPrintDebug) {
576
- console.log(" staticLogical", value2.left, name, val);
452
+ console.log("attempt", value2.alternate, value2.consequent);
453
+ }
454
+ const aVal = attemptEval(value2.alternate);
455
+ const cVal = attemptEval(value2.consequent);
456
+ if (shouldPrintDebug) {
457
+ const type = value2.test.type;
458
+ console.log(" static ternary", type, cVal, aVal);
577
459
  }
578
460
  return {
579
- test: value2.left,
461
+ test: value2.test,
580
462
  remove,
581
- consequent: { [name]: val },
582
- alternate: null
463
+ consequent: { [name]: cVal },
464
+ alternate: { [name]: aVal }
583
465
  };
584
466
  } catch (err) {
585
467
  if (shouldPrintDebug) {
586
- console.log(" cant static eval logical", err);
468
+ console.log(" cant eval ternary", err.message);
587
469
  }
588
470
  }
589
471
  }
472
+ return null;
590
473
  }
591
- return null;
474
+ __name(getStaticConditional, "getStaticConditional");
475
+ function getStaticLogical(value2) {
476
+ if (t.isLogicalExpression(value2)) {
477
+ if (value2.operator === "&&") {
478
+ try {
479
+ const val = attemptEval(value2.right);
480
+ if (shouldPrintDebug) {
481
+ console.log(" staticLogical", value2.left, name, val);
482
+ }
483
+ return {
484
+ test: value2.left,
485
+ remove,
486
+ consequent: { [name]: val },
487
+ alternate: null
488
+ };
489
+ } catch (err) {
490
+ if (shouldPrintDebug) {
491
+ console.log(" cant static eval logical", err);
492
+ }
493
+ }
494
+ }
495
+ }
496
+ return null;
497
+ }
498
+ __name(getStaticLogical, "getStaticLogical");
499
+ }, isExtractable = function(obj) {
500
+ return t.isObjectExpression(obj) && obj.properties.every((prop) => {
501
+ if (!t.isObjectProperty(prop)) {
502
+ console.log("not object prop", prop);
503
+ return false;
504
+ }
505
+ const propName = prop.key["name"];
506
+ if (!isValidStyleKey(propName) && propName !== "tag") {
507
+ if (shouldPrintDebug) {
508
+ console.log(" not a valid style prop!", propName);
509
+ }
510
+ return false;
511
+ }
512
+ return true;
513
+ });
514
+ }, createTernariesFromObjectProperties = function(test, side, ternaryPartial = {}) {
515
+ if (!side) {
516
+ return null;
517
+ }
518
+ if (!isExtractable(side)) {
519
+ throw new Error("not extractable");
520
+ }
521
+ return side.properties.flatMap((property) => {
522
+ if (!t.isObjectProperty(property)) {
523
+ throw new Error("expected object property");
524
+ }
525
+ if (t.isIdentifier(property.key)) {
526
+ const key = property.key.name;
527
+ const mediaQueryKey = key.slice(1);
528
+ const isMediaQuery = key[0] === "$" && mediaQueryConfig[mediaQueryKey];
529
+ if (isMediaQuery) {
530
+ if (t.isExpression(property.value)) {
531
+ const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(mediaQueryKey), property.value, {
532
+ inlineMediaQuery: mediaQueryKey
533
+ });
534
+ if (ternaries2) {
535
+ return ternaries2.map((value) => ({
536
+ ...ternaryPartial,
537
+ ...value,
538
+ test: t.logicalExpression("&&", value.test, test)
539
+ }));
540
+ } else {
541
+ console.log("\u26A0\uFE0F no ternaries?", property);
542
+ }
543
+ } else {
544
+ console.log("\u26A0\uFE0F not expression", property);
545
+ }
546
+ }
547
+ }
548
+ if (t.isConditionalExpression(property.value)) {
549
+ const [truthy, falsy] = [
550
+ t.objectExpression([t.objectProperty(property.key, property.value.consequent)]),
551
+ t.objectExpression([t.objectProperty(property.key, property.value.alternate)])
552
+ ].map((x) => attemptEval(x));
553
+ return [
554
+ createTernary({
555
+ remove() {
556
+ },
557
+ ...ternaryPartial,
558
+ test: t.logicalExpression("&&", test, property.value.test),
559
+ consequent: truthy,
560
+ alternate: null
561
+ }),
562
+ createTernary({
563
+ ...ternaryPartial,
564
+ test: t.logicalExpression("&&", test, t.unaryExpression("!", property.value.test)),
565
+ consequent: falsy,
566
+ alternate: null,
567
+ remove() {
568
+ }
569
+ })
570
+ ];
571
+ }
572
+ const obj = t.objectExpression([t.objectProperty(property.key, property.value)]);
573
+ const consequent = attemptEval(obj);
574
+ return createTernary({
575
+ remove() {
576
+ },
577
+ ...ternaryPartial,
578
+ test,
579
+ consequent,
580
+ alternate: null
581
+ });
582
+ });
583
+ }, mergeStyles = function(prev2, next) {
584
+ for (const key in next) {
585
+ if (pseudos[key]) {
586
+ prev2[key] = prev2[key] || {};
587
+ if (shouldPrintDebug) {
588
+ if (!next[key] || !prev2[key]) {
589
+ console.log("warn: missing", key, prev2, next);
590
+ }
591
+ }
592
+ Object.assign(prev2[key], next[key]);
593
+ } else {
594
+ prev2[key] = next[key];
595
+ }
596
+ }
597
+ };
598
+ __name(isValidStyleKey, "isValidStyleKey");
599
+ __name(evaluateAttribute, "evaluateAttribute");
600
+ __name(isExtractable, "isExtractable");
601
+ __name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
602
+ __name(mergeStyles, "mergeStyles");
603
+ node.attributes.find((n) => t.isJSXAttribute(n) && t.isJSXIdentifier(n.name) && n.name.name === "debug" && n.value === null);
604
+ if (shouldPrintDebug) {
605
+ console.log(`
606
+ <${originalNodeName} /> (${tagId})`);
592
607
  }
593
- __name(getStaticLogical, "getStaticLogical");
594
- }
595
- __name(evaluateAttribute, "evaluateAttribute");
596
- function isExtractable(obj) {
597
- return t.isObjectExpression(obj) && obj.properties.every((prop) => {
598
- if (!t.isObjectProperty(prop)) {
599
- console.log("not object prop", prop);
600
- return false;
608
+ if (shouldAddDebugProp && !disableDebugAttr) {
609
+ res.modified++;
610
+ node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(tagId)));
611
+ }
612
+ const shouldLog = !hasLogged;
613
+ if (shouldLog) {
614
+ const prefix = " |";
615
+ console.log(prefixLogs || prefix, " total \xB7 optimized \xB7 flattened ");
616
+ hasLogged = true;
617
+ }
618
+ if (disableExtraction) {
619
+ return;
620
+ }
621
+ const { staticConfig } = component;
622
+ const isTextView = staticConfig.isText || false;
623
+ const validStyles = (_a = staticConfig == null ? void 0 : staticConfig.validStyles) != null ? _a : {};
624
+ let tagName = (_c = (_b = staticConfig.defaultProps) == null ? void 0 : _b.tag) != null ? _c : isTextView ? "span" : "div";
625
+ traversePath.get("openingElement").get("attributes").forEach((path) => {
626
+ const attr = path.node;
627
+ if (t.isJSXSpreadAttribute(attr))
628
+ return;
629
+ if (attr.name.name !== "tag")
630
+ return;
631
+ const val = attr.value;
632
+ if (!t.isStringLiteral(val))
633
+ return;
634
+ tagName = val.value;
635
+ });
636
+ const flatNode = getFlattenedNode({ isTextView, tag: tagName });
637
+ const inlineProps = /* @__PURE__ */ new Set([
638
+ ...props.inlineProps || [],
639
+ ...staticConfig.inlineProps || []
640
+ ]);
641
+ const deoptProps = /* @__PURE__ */ new Set([
642
+ "animation",
643
+ ...props.deoptProps || [],
644
+ ...staticConfig.deoptProps || []
645
+ ]);
646
+ const staticNamespace = getStaticBindingsForScope(traversePath.scope, importsWhitelist, sourcePath, bindingCache, shouldPrintDebug);
647
+ const attemptEval = !evaluateVars ? evaluateAstNode : createEvaluator({
648
+ tamaguiConfig,
649
+ staticNamespace,
650
+ sourcePath,
651
+ traversePath,
652
+ shouldPrintDebug
653
+ });
654
+ const attemptEvalSafe = createSafeEvaluator(attemptEval);
655
+ if (shouldPrintDebug) {
656
+ console.log(" staticNamespace", Object.keys(staticNamespace).join(", "));
657
+ }
658
+ const flattenedAttrs = [];
659
+ traversePath.get("openingElement").get("attributes").forEach((path) => {
660
+ const attr = path.node;
661
+ if (!t.isJSXSpreadAttribute(attr)) {
662
+ flattenedAttrs.push(attr);
663
+ return;
601
664
  }
602
- const propName = prop.key["name"];
603
- if (!isValidStyleKey(propName) && propName !== "tag") {
665
+ let arg;
666
+ try {
667
+ arg = attemptEval(attr.argument);
668
+ } catch (e) {
604
669
  if (shouldPrintDebug) {
605
- console.log(" not a valid style prop!", propName);
670
+ console.log(" couldnt parse spread", e.message);
671
+ }
672
+ flattenedAttrs.push(attr);
673
+ return;
674
+ }
675
+ if (arg !== void 0) {
676
+ try {
677
+ if (typeof arg !== "object" || arg == null) {
678
+ if (shouldPrintDebug) {
679
+ console.log(" non object or null arg", arg);
680
+ }
681
+ flattenedAttrs.push(attr);
682
+ } else {
683
+ for (const k in arg) {
684
+ const value = arg[k];
685
+ if (!value && typeof value === "object") {
686
+ console.log("shouldnt we handle this?", k, value, arg);
687
+ continue;
688
+ }
689
+ flattenedAttrs.push(t.jsxAttribute(t.jsxIdentifier(k), t.jsxExpressionContainer(literalToAst(value))));
690
+ }
691
+ }
692
+ } catch (err) {
693
+ console.warn("cant parse spread, caught err", err);
694
+ couldntParse = true;
606
695
  }
607
- return false;
608
696
  }
609
- return true;
610
697
  });
611
- }
612
- __name(isExtractable, "isExtractable");
613
- function createTernariesFromObjectProperties(test, side, ternaryPartial = {}) {
614
- if (!side) {
615
- return null;
698
+ if (couldntParse) {
699
+ return;
700
+ }
701
+ tm.mark("jsx-element-flattened", shouldPrintDebug === "verbose");
702
+ node.attributes = flattenedAttrs;
703
+ if (staticConfig.defaultProps) {
704
+ for (const key in staticConfig.defaultProps) {
705
+ if (isValidStyleKey(key)) {
706
+ continue;
707
+ }
708
+ const serialize = require("babel-literal-to-ast");
709
+ const val = staticConfig.defaultProps[key];
710
+ try {
711
+ const serializedDefaultProp = serialize(val);
712
+ node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(key), typeof val === "string" ? t.stringLiteral(val) : t.jsxExpressionContainer(serializedDefaultProp)));
713
+ } catch (err) {
714
+ console.warn(`\u26A0\uFE0F Error evaluating default prop for component ${node.name.name}, prop ${key}
715
+ error: ${err}
716
+ value:`, val, "\n defaultProps:", staticConfig.defaultProps);
717
+ }
718
+ }
616
719
  }
617
- if (!isExtractable(side)) {
618
- throw new Error("not extractable");
720
+ let attrs = [];
721
+ let shouldDeopt = false;
722
+ const inlined = /* @__PURE__ */ new Map();
723
+ let hasSetOptimized = false;
724
+ attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
725
+ try {
726
+ const res2 = evaluateAttribute(path);
727
+ tm.mark("jsx-element-evaluate-attr", shouldPrintDebug === "verbose");
728
+ if (!res2) {
729
+ path.remove();
730
+ }
731
+ return res2;
732
+ } catch (err) {
733
+ if (shouldPrintDebug) {
734
+ console.log("Error extracting attribute", err.message, err.stack);
735
+ console.log("node", path.node);
736
+ }
737
+ return {
738
+ type: "attr",
739
+ value: path.node
740
+ };
741
+ }
742
+ }).flat(4).filter(isPresent);
743
+ if (shouldPrintDebug) {
744
+ console.log(" - attrs (before):\n", logLines(attrs.map(attrStr).join(", ")));
619
745
  }
620
- return side.properties.flatMap((property) => {
621
- if (!t.isObjectProperty(property)) {
622
- throw new Error("expected object property");
746
+ node.attributes = attrs.filter(isAttr).map((x) => x.value);
747
+ if (couldntParse || shouldDeopt) {
748
+ if (shouldPrintDebug) {
749
+ console.log(` avoid optimizing:`, { couldntParse, shouldDeopt });
623
750
  }
624
- if (t.isIdentifier(property.key)) {
625
- const key = property.key.name;
626
- const mediaQueryKey = key.slice(1);
627
- const isMediaQuery = key[0] === "$" && mediaQueryConfig[mediaQueryKey];
628
- if (isMediaQuery) {
629
- if (t.isExpression(property.value)) {
630
- const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(mediaQueryKey), property.value, {
631
- inlineMediaQuery: mediaQueryKey
632
- });
633
- if (ternaries2) {
634
- return ternaries2.map((value) => ({
635
- ...ternaryPartial,
636
- ...value,
637
- test: t.logicalExpression("&&", value.test, test)
638
- }));
639
- } else {
640
- console.log("\u26A0\uFE0F no ternaries?", property);
751
+ node.attributes = ogAttributes;
752
+ return;
753
+ }
754
+ const parentFn = findTopmostFunction(traversePath);
755
+ if (parentFn) {
756
+ modifiedComponents.add(parentFn);
757
+ }
758
+ let ternaries = [];
759
+ attrs = attrs.reduce((out, cur) => {
760
+ const next = attrs[attrs.indexOf(cur) + 1];
761
+ if (cur.type === "ternary") {
762
+ ternaries.push(cur.value);
763
+ }
764
+ if ((!next || next.type !== "ternary") && ternaries.length) {
765
+ const normalized = normalizeTernaries(ternaries).map(({ alternate, consequent, ...rest }) => {
766
+ return {
767
+ type: "ternary",
768
+ value: {
769
+ ...rest,
770
+ alternate: alternate || null,
771
+ consequent: consequent || null
641
772
  }
642
- } else {
643
- console.log("\u26A0\uFE0F not expression", property);
773
+ };
774
+ });
775
+ try {
776
+ return [...out, ...normalized];
777
+ } finally {
778
+ if (shouldPrintDebug) {
779
+ console.log(` normalizeTernaries (${ternaries.length} => ${normalized.length})`);
644
780
  }
781
+ ternaries = [];
645
782
  }
646
783
  }
647
- if (t.isConditionalExpression(property.value)) {
648
- const [truthy, falsy] = [
649
- t.objectExpression([t.objectProperty(property.key, property.value.consequent)]),
650
- t.objectExpression([t.objectProperty(property.key, property.value.alternate)])
651
- ].map((x) => attemptEval(x));
652
- return [
653
- createTernary({
654
- remove() {
655
- },
656
- ...ternaryPartial,
657
- test: t.logicalExpression("&&", test, property.value.test),
658
- consequent: truthy,
659
- alternate: null
660
- }),
661
- createTernary({
662
- ...ternaryPartial,
663
- test: t.logicalExpression("&&", test, t.unaryExpression("!", property.value.test)),
664
- consequent: falsy,
665
- alternate: null,
666
- remove() {
667
- }
668
- })
669
- ];
784
+ if (cur.type === "ternary") {
785
+ return out;
670
786
  }
671
- const obj = t.objectExpression([t.objectProperty(property.key, property.value)]);
672
- const consequent = attemptEval(obj);
673
- return createTernary({
674
- remove() {
675
- },
676
- ...ternaryPartial,
677
- test,
678
- consequent,
679
- alternate: null
680
- });
681
- });
682
- }
683
- __name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
684
- node.attributes = attrs.filter(isAttr).map((x) => x.value);
685
- if (couldntParse || shouldDeopt) {
787
+ out.push(cur);
788
+ return out;
789
+ }, []).flat();
790
+ const hasSpread = node.attributes.some((x) => t.isJSXSpreadAttribute(x));
791
+ const hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText"));
792
+ const themeVal = inlined.get("theme");
793
+ inlined.delete("theme");
794
+ const allOtherPropsExtractable = [...inlined].every(([k, v]) => INLINE_EXTRACTABLE[k]);
795
+ const shouldWrapThme = allOtherPropsExtractable && !!themeVal;
796
+ const canFlattenProps = inlined.size === 0 || shouldWrapThme || allOtherPropsExtractable;
797
+ let shouldFlatten = !shouldDeopt && canFlattenProps && !hasSpread && staticConfig.neverFlatten !== true && (staticConfig.neverFlatten === "jsx" ? hasOnlyStringChildren : true);
686
798
  if (shouldPrintDebug) {
687
- console.log(` avoid optimizing:`, { couldntParse, shouldDeopt });
799
+ console.log(" >>", { hasSpread, shouldDeopt, shouldFlatten, canFlattenProps, shouldWrapThme, allOtherPropsExtractable, hasOnlyStringChildren });
688
800
  }
689
- node.attributes = ogAttributes;
690
- return;
691
- }
692
- const parentFn = findTopmostFunction(traversePath);
693
- if (parentFn) {
694
- modifiedComponents.add(parentFn);
695
- }
696
- let ternaries = [];
697
- attrs = attrs.reduce((out, cur) => {
698
- const next = attrs[attrs.indexOf(cur) + 1];
699
- if (cur.type === "ternary") {
700
- ternaries.push(cur.value);
801
+ if (shouldFlatten && shouldWrapThme) {
802
+ if (typeof themeVal !== "string") {
803
+ return;
804
+ }
805
+ if (shouldPrintDebug) {
806
+ console.log(" - wrapping theme", allOtherPropsExtractable, themeVal);
807
+ }
808
+ attrs = attrs.filter((x) => x.type === "attr" && t.isJSXAttribute(x.value) && x.value.name.name === "theme" ? false : true);
809
+ if (!hasImportedTheme) {
810
+ hasImportedTheme = true;
811
+ programPath.node.body.push(t.importDeclaration([t.importSpecifier(t.identifier("_TamaguiTheme"), t.identifier("Theme"))], t.stringLiteral("@tamagui/core")));
812
+ }
813
+ traversePath.replaceWith(t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier("_TamaguiTheme"), [
814
+ t.jsxAttribute(t.jsxIdentifier("name"), t.stringLiteral(`${themeVal}`))
815
+ ]), t.jsxClosingElement(t.jsxIdentifier("_TamaguiTheme")), [traversePath.node]));
701
816
  }
702
- if ((!next || next.type !== "ternary") && ternaries.length) {
703
- const normalized = normalizeTernaries(ternaries).map(({ alternate, consequent, ...rest }) => {
704
- return {
705
- type: "ternary",
706
- value: {
707
- ...rest,
708
- alternate: alternate || null,
709
- consequent: consequent || null
710
- }
817
+ if (shouldFlatten && staticConfig.defaultProps) {
818
+ const defaultStyleAttrs = Object.keys(staticConfig.defaultProps).flatMap((key) => {
819
+ if (!isValidStyleKey(key)) {
820
+ return [];
821
+ }
822
+ const value = staticConfig.defaultProps[key];
823
+ const name = tamaguiConfig.shorthands[key] || key;
824
+ if (value === void 0) {
825
+ console.warn(`\u26A0\uFE0F Error evaluating default style for component, prop ${key} ${value}`);
826
+ shouldDeopt = true;
827
+ return;
828
+ }
829
+ const attr = {
830
+ type: "style",
831
+ name,
832
+ value: { [name]: value }
711
833
  };
834
+ return attr;
712
835
  });
713
- try {
714
- return [...out, ...normalized];
715
- } finally {
716
- if (shouldPrintDebug) {
717
- console.log(` normalizeTernaries (${ternaries.length} => ${normalized.length})`);
836
+ if (defaultStyleAttrs.length) {
837
+ attrs = [...defaultStyleAttrs, ...attrs];
838
+ }
839
+ }
840
+ if (shouldDeopt) {
841
+ node.attributes = ogAttributes;
842
+ return;
843
+ }
844
+ const ensureOverridden = {};
845
+ if (!shouldFlatten) {
846
+ for (const cur of attrs) {
847
+ if (cur.type === "style") {
848
+ for (const key in cur.value) {
849
+ const shouldEnsureOverridden = !!((_d = staticConfig.ensureOverriddenProp) == null ? void 0 : _d[key]);
850
+ const isSetInAttrsAlready = attrs.some((x) => x.type === "attr" && x.value.type === "JSXAttribute" && x.value.name.name === key);
851
+ if (!isSetInAttrsAlready) {
852
+ const isVariant = !!((_e = staticConfig.variants) == null ? void 0 : _e[cur.name || ""]);
853
+ if (isVariant || shouldEnsureOverridden) {
854
+ ensureOverridden[key] = true;
855
+ }
856
+ }
857
+ }
718
858
  }
719
- ternaries = [];
720
859
  }
721
860
  }
722
- if (cur.type === "ternary") {
723
- return out;
861
+ if (shouldPrintDebug) {
862
+ console.log(" - attrs (flattened): \n", logLines(attrs.map(attrStr).join(", ")));
863
+ console.log(" - ensureOverriden:", Object.keys(ensureOverridden).join(", "));
864
+ }
865
+ attrs = attrs.reduce((acc, cur) => {
866
+ if (!cur) {
867
+ return acc;
868
+ }
869
+ if (cur.type === "attr" && !t.isJSXSpreadAttribute(cur.value)) {
870
+ if (shouldFlatten) {
871
+ if (cur.value.name.name === "tag") {
872
+ return acc;
873
+ }
874
+ }
875
+ }
876
+ if (cur.type !== "style") {
877
+ acc.push(cur);
878
+ return acc;
879
+ }
880
+ let key = Object.keys(cur.value)[0];
881
+ const value = cur.value[key];
882
+ const fullKey = tamaguiConfig.shorthands[key];
883
+ if (fullKey) {
884
+ cur.value = { [fullKey]: value };
885
+ key = fullKey;
886
+ }
887
+ if (disableExtractVariables) {
888
+ if (value[0] === "$") {
889
+ if (shouldPrintDebug) {
890
+ console.log(` keeping variable inline: ${key} =`, value);
891
+ }
892
+ acc.push({
893
+ type: "attr",
894
+ value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.stringLiteral(value)))
895
+ });
896
+ return acc;
897
+ }
898
+ }
899
+ acc.push(cur);
900
+ return acc;
901
+ }, []);
902
+ tm.mark("jsx-element-expanded", shouldPrintDebug === "verbose");
903
+ if (shouldPrintDebug) {
904
+ console.log(" - attrs (expanded): \n", logLines(attrs.map(attrStr).join(", ")));
724
905
  }
725
- out.push(cur);
726
- return out;
727
- }, []).flat();
728
- const completeStaticProps = {
729
- ...Object.keys(attrs).reduce((acc, index) => {
906
+ let prev = null;
907
+ attrs = attrs.reduce((acc, cur) => {
908
+ if (cur.type === "style") {
909
+ const key = Object.keys(cur.value)[0];
910
+ const value = cur.value[key];
911
+ const shouldKeepOriginalAttr = !shouldFlatten && !validStyles[key] && !pseudos[key] && !key.startsWith("data-");
912
+ if (shouldKeepOriginalAttr) {
913
+ if (shouldPrintDebug) {
914
+ console.log(" - keeping as non-style", key);
915
+ }
916
+ prev = cur;
917
+ acc.push({
918
+ type: "attr",
919
+ value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(typeof value === "string" ? t.stringLiteral(value) : literalToAst(value)))
920
+ });
921
+ acc.push(cur);
922
+ return acc;
923
+ }
924
+ if (ensureOverridden[key]) {
925
+ acc.push({
926
+ type: "attr",
927
+ value: cur.attr || t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.nullLiteral()))
928
+ });
929
+ }
930
+ if ((prev == null ? void 0 : prev.type) === "style") {
931
+ mergeStyles(prev.value, cur.value);
932
+ return acc;
933
+ }
934
+ }
935
+ prev = cur;
936
+ acc.push(cur);
937
+ return acc;
938
+ }, []);
939
+ const state = {
940
+ noClassNames: true,
941
+ focus: false,
942
+ hover: false,
943
+ mounted: true,
944
+ press: false,
945
+ pressIn: false
946
+ };
947
+ const completeStaticProps = Object.keys(attrs).reduce((acc, index) => {
730
948
  const cur = attrs[index];
731
949
  if (cur.type === "style") {
950
+ normalizeStyleObject(cur.value);
732
951
  Object.assign(acc, cur.value);
733
952
  }
734
953
  if (cur.type === "attr") {
@@ -746,282 +965,185 @@ function createExtractor() {
746
965
  acc[key] = value;
747
966
  }
748
967
  return acc;
749
- }, {})
750
- };
751
- const hasSpread = node.attributes.some((x) => t.isJSXSpreadAttribute(x));
752
- const hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText"));
753
- const shouldFlatten = !shouldDeopt && inlinePropCount === 0 && !hasSpread && staticConfig.neverFlatten !== true && (staticConfig.neverFlatten === "jsx" ? hasOnlyStringChildren : true);
754
- if (shouldFlatten && staticConfig.defaultProps) {
755
- const defaultStyleAttrs = Object.keys(staticConfig.defaultProps).flatMap((key) => {
756
- if (!isValidStyleKey(key)) {
757
- return;
968
+ }, {});
969
+ const completeProps = {
970
+ ...staticConfig.defaultProps,
971
+ ...completeStaticProps
972
+ };
973
+ if (shouldPrintDebug) {
974
+ console.log(" - attrs (combined \u{1F500}): \n", logLines(attrs.map(attrStr).join(", ")));
975
+ console.log(" - defaultProps: \n", logLines(objToStr(staticConfig.defaultProps)));
976
+ console.log(" - completeStaticProps: \n", logLines(objToStr(completeStaticProps)));
977
+ }
978
+ const getStyles = /* @__PURE__ */ __name((props2, debugName = "") => {
979
+ if (!props2 || !Object.keys(props2).length) {
980
+ if (shouldPrintDebug)
981
+ console.log(" getStyles() no props");
982
+ return {};
983
+ }
984
+ if (excludeProps && !!excludeProps.size) {
985
+ for (const key in props2) {
986
+ if (excludeProps.has(key)) {
987
+ if (shouldPrintDebug)
988
+ console.log(" delete excluded", key);
989
+ delete props2[key];
990
+ }
991
+ }
758
992
  }
759
993
  try {
760
- const serialize = require("babel-literal-to-ast");
761
- const val = staticConfig.defaultProps[key];
762
- const value = serialize(val);
763
- const name = tamaguiConfig.shorthands[key] || key;
764
- return {
765
- type: "style",
766
- name,
767
- value
994
+ const out = getSplitStyles(props2, staticConfig, defaultTheme, {
995
+ ...state,
996
+ fallbackProps: completeProps
997
+ });
998
+ const outStyle = {
999
+ ...out.style,
1000
+ ...out.pseudos
768
1001
  };
769
- } catch (err) {
770
- console.warn(`\u26A0\uFE0F Error evaluating default style for component, prop ${key}
771
- error: ${err}`);
772
- shouldDeopt = true;
773
- }
774
- });
775
- if (defaultStyleAttrs.length) {
776
- attrs = [...defaultStyleAttrs, ...attrs];
777
- }
778
- }
779
- if (shouldDeopt) {
780
- node.attributes = ogAttributes;
781
- return;
782
- }
783
- const ensureOverridden = {};
784
- if (!shouldFlatten) {
785
- for (const cur of attrs) {
786
- if (cur.type === "style") {
787
- for (const key in cur.value) {
788
- const shouldEnsureOverridden = !!((_d = staticConfig.ensureOverriddenProp) == null ? void 0 : _d[key]);
789
- const isSetInAttrsAlready = attrs.some((x) => x.type === "attr" && x.value.type === "JSXAttribute" && x.value.name.name === key);
790
- if (!isSetInAttrsAlready) {
791
- const isVariant = !!((_e = staticConfig.variants) == null ? void 0 : _e[cur.name || ""]);
792
- if (isVariant || shouldEnsureOverridden) {
793
- ensureOverridden[key] = true;
1002
+ if (staticConfig.validStyles) {
1003
+ for (const key in outStyle) {
1004
+ if (!staticConfig.validStyles[key] && !pseudos[key] && !/(hover|focus|press)Style$/.test(key)) {
1005
+ if (shouldPrintDebug)
1006
+ console.log(" delete invalid style", key);
1007
+ delete outStyle[key];
794
1008
  }
795
1009
  }
796
1010
  }
797
- }
798
- }
799
- }
800
- if (shouldPrintDebug) {
801
- console.log(" - attrs (flattened): \n", logLines(attrs.map(attrStr).join(", ")));
802
- console.log(" - ensureOverriden:", Object.keys(ensureOverridden).join(", "));
803
- }
804
- attrs = attrs.reduce((acc, cur) => {
805
- if (!cur) {
806
- return acc;
807
- }
808
- if (shouldFlatten && cur.type === "attr" && !t.isJSXSpreadAttribute(cur.value) && cur.value.name.name === "tag") {
809
- return acc;
810
- }
811
- if (cur.type !== "style") {
812
- acc.push(cur);
813
- return acc;
814
- }
815
- let key = Object.keys(cur.value)[0];
816
- const value = cur.value[key];
817
- const fullKey = tamaguiConfig.shorthands[key];
818
- if (fullKey) {
819
- cur.value = { [fullKey]: value };
820
- key = fullKey;
821
- }
822
- if (disableExtractVariables) {
823
- if (value[0] === "$") {
824
1011
  if (shouldPrintDebug) {
825
- console.log(` keeping variable inline: ${key} =`, value);
1012
+ console.log(` getStyles ${debugName} (props):
1013
+ `, logLines(objToStr(props2)));
1014
+ console.log(` getStyles ${debugName} (out.viewProps):
1015
+ `, logLines(objToStr(out.viewProps)));
1016
+ console.log(` getStyles ${debugName} (out.style):
1017
+ `, logLines(objToStr(outStyle || {}), true));
826
1018
  }
827
- acc.push({
828
- type: "attr",
829
- value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.stringLiteral(value)))
830
- });
831
- return acc;
1019
+ return outStyle;
1020
+ } catch (err) {
1021
+ console.log("error", err.message, err.stack);
1022
+ return {};
832
1023
  }
1024
+ }, "getStyles");
1025
+ const completeStylesProcessed = getStyles(completeProps, "completeStylesProcessed");
1026
+ if (!completeStylesProcessed) {
1027
+ throw new Error(`Impossible, no styles`);
833
1028
  }
834
- acc.push(cur);
835
- return acc;
836
- }, []);
837
- if (shouldPrintDebug) {
838
- console.log(" - attrs (expanded): \n", logLines(attrs.map(attrStr).join(", ")));
839
- }
840
- let prev = null;
841
- function mergeStyles(prev2, next) {
842
- for (const key in next) {
843
- if (pseudos[key]) {
844
- prev2[key] = prev2[key] || {};
845
- Object.assign(prev2[key], next[key]);
1029
+ const stylesToAddToInitialGroup = shouldFlatten ? difference(Object.keys(completeStylesProcessed), Object.keys(completeStaticProps)) : [];
1030
+ if (stylesToAddToInitialGroup.length) {
1031
+ const toAdd = pick(completeStylesProcessed, ...stylesToAddToInitialGroup);
1032
+ const firstGroup = attrs.find((x) => x.type === "style");
1033
+ if (shouldPrintDebug) {
1034
+ console.log(" toAdd", objToStr(toAdd));
1035
+ }
1036
+ if (!firstGroup) {
1037
+ attrs.unshift({ type: "style", value: toAdd });
846
1038
  } else {
847
- prev2[key] = next[key];
1039
+ Object.assign(firstGroup.value, toAdd);
848
1040
  }
849
1041
  }
850
- }
851
- __name(mergeStyles, "mergeStyles");
852
- attrs = attrs.reduce((acc, cur) => {
853
- if (cur.type === "style") {
854
- const key = Object.keys(cur.value)[0];
855
- const value = cur.value[key];
856
- const shouldKeepOriginalAttr = !shouldFlatten && !validStyles[key] && !pseudos[key] && !key.startsWith("data-");
857
- if (shouldKeepOriginalAttr) {
858
- if (shouldPrintDebug) {
859
- console.log(" - keeping as non-style", key);
860
- }
861
- prev = cur;
862
- acc.push({
863
- type: "attr",
864
- value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(typeof value === "string" ? t.stringLiteral(value) : literalToAst(value)))
865
- });
866
- acc.push(cur);
867
- return acc;
868
- }
869
- if (ensureOverridden[key]) {
870
- acc.push({
871
- type: "attr",
872
- value: cur.attr || t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.nullLiteral()))
873
- });
874
- }
875
- if ((prev == null ? void 0 : prev.type) === "style") {
876
- mergeStyles(prev.value, cur.value);
877
- return acc;
878
- }
1042
+ if (shouldPrintDebug) {
1043
+ console.log(" -- stylesToAddToInitialGroup", stylesToAddToInitialGroup.join(", "), { shouldFlatten });
1044
+ console.log(" -- completeStaticProps:\n", logLines(objToStr(completeStaticProps)));
1045
+ console.log(" -- completeStylesProcessed:\n", logLines(objToStr(completeStylesProcessed)));
879
1046
  }
880
- prev = cur;
881
- acc.push(cur);
882
- return acc;
883
- }, []);
884
- if (shouldPrintDebug) {
885
- console.log(" - attrs (combined \u{1F500}): \n", logLines(attrs.map(attrStr).join(", ")));
886
- }
887
- const getStyles = /* @__PURE__ */ __name((props2, debugName = "") => {
888
- if (!props2)
889
- return;
890
- if (!Object.keys(props2).length)
891
- return;
892
- if (excludeProps && !!excludeProps.size) {
893
- for (const key in props2) {
894
- if (excludeProps.has(key)) {
895
- delete props2[key];
1047
+ let getStyleError = null;
1048
+ for (const attr of attrs) {
1049
+ try {
1050
+ if (shouldPrintDebug)
1051
+ console.log(" *", attrStr(attr));
1052
+ switch (attr.type) {
1053
+ case "ternary":
1054
+ const a = getStyles(attr.value.alternate, "ternary.alternate");
1055
+ const c = getStyles(attr.value.consequent, "ternary.consequent");
1056
+ if (a)
1057
+ attr.value.alternate = a;
1058
+ if (c)
1059
+ attr.value.consequent = c;
1060
+ if (shouldPrintDebug)
1061
+ console.log(" => tern ", attrStr(attr));
1062
+ continue;
1063
+ case "style":
1064
+ if (shouldPrintDebug)
1065
+ console.log(" * styles in", logLines(objToStr(attr.value)));
1066
+ const styles = getStyles(attr.value, "style");
1067
+ if (shouldPrintDebug)
1068
+ console.log(" * styles out", logLines(objToStr(styles)));
1069
+ if (styles) {
1070
+ attr.value = Object.fromEntries(Object.keys(styles).map((k) => [k, completeStylesProcessed[k]]));
1071
+ }
1072
+ continue;
896
1073
  }
1074
+ } catch (err) {
1075
+ getStyleError = err;
897
1076
  }
898
1077
  }
899
- const out = getSplitStyles(props2, staticConfig, defaultTheme, {
900
- noClassNames: true
901
- });
902
- const outStyle = {
903
- ...out.style,
904
- ...out.pseudos
905
- };
906
- if (shouldPrintDebug === "verbose") {
907
- console.log(` getStyles ${debugName} (props):
908
- `, logLines(objToStr(props2)));
909
- console.log(` getStyles ${debugName} (out.viewProps):
910
- `, logLines(objToStr(out.viewProps)));
911
- console.log(` getStyles ${debugName} (out.style):
912
- `, logLines(objToStr(outStyle || {}), true));
1078
+ tm.mark("jsx-element-styles", shouldPrintDebug === "verbose");
1079
+ if (getStyleError) {
1080
+ console.log(" \u26A0\uFE0F postprocessing error, deopt", getStyleError);
1081
+ node.attributes = ogAttributes;
1082
+ return node;
913
1083
  }
914
- for (const key in outStyle) {
915
- if (staticConfig.validStyles) {
916
- if (!staticConfig.validStyles[key] && !pseudos[key]) {
917
- delete outStyle[key];
1084
+ const existingStyleKeys = /* @__PURE__ */ new Set();
1085
+ for (let i = attrs.length - 1; i >= 0; i--) {
1086
+ const attr = attrs[i];
1087
+ if (shouldFlatten && canFlattenProps) {
1088
+ if (attr.type === "attr") {
1089
+ if (t.isJSXAttribute(attr.value)) {
1090
+ if (t.isJSXIdentifier(attr.value.name)) {
1091
+ const name = attr.value.name.name;
1092
+ if (INLINE_EXTRACTABLE[name]) {
1093
+ attr.value.name.name = INLINE_EXTRACTABLE[name];
1094
+ }
1095
+ }
1096
+ }
918
1097
  }
919
1098
  }
920
- }
921
- return outStyle;
922
- }, "getStyles");
923
- const completeStylesProcessed = getStyles({
924
- ...staticConfig.defaultProps,
925
- ...completeStaticProps
926
- }, "completeStylesProcessed");
927
- if (!completeStylesProcessed) {
928
- throw new Error(`Impossible, no styles`);
929
- }
930
- const stylesToAddToInitialGroup = shouldFlatten ? difference(Object.keys(completeStylesProcessed), Object.keys(completeStaticProps)) : [];
931
- if (stylesToAddToInitialGroup.length) {
932
- const toAdd = pick(completeStylesProcessed, ...stylesToAddToInitialGroup);
933
- const firstGroup = attrs.find((x) => x.type === "style");
934
- if (shouldPrintDebug) {
935
- console.log(" toAdd", objToStr(toAdd));
936
- }
937
- if (!firstGroup) {
938
- attrs.unshift({ type: "style", value: toAdd });
939
- } else {
940
- Object.assign(firstGroup.value, toAdd);
941
- }
942
- }
943
- if (shouldPrintDebug) {
944
- console.log(" -- stylesToAddToInitialGroup", stylesToAddToInitialGroup.join(", "), { shouldFlatten });
945
- console.log(" -- completeStaticProps:\n", logLines(objToStr(completeStaticProps)));
946
- console.log(" -- completeStylesProcessed:\n", logLines(objToStr(completeStylesProcessed)));
947
- }
948
- let getStyleError = null;
949
- for (const attr of attrs) {
950
- try {
951
- if (shouldPrintDebug)
952
- console.log(" *", attrStr(attr));
953
- switch (attr.type) {
954
- case "ternary":
955
- const a = getStyles(attr.value.alternate, "ternary.alternate");
956
- const c = getStyles(attr.value.consequent, "ternary.consequent");
957
- if (a)
958
- attr.value.alternate = a;
959
- if (c)
960
- attr.value.consequent = c;
961
- if (shouldPrintDebug)
962
- console.log(" => tern ", attrStr(attr));
963
- continue;
964
- case "style":
965
- const styles = getStyles(attr.value, "style");
966
- if (styles) {
967
- attr.value = Object.fromEntries(Object.keys(styles).map((k) => [k, completeStylesProcessed[k]]));
1099
+ if (attr.type === "style") {
1100
+ for (const key in attr.value) {
1101
+ if (existingStyleKeys.has(key)) {
1102
+ if (shouldPrintDebug) {
1103
+ console.log(" >> delete existing", key);
1104
+ }
1105
+ delete attr.value[key];
968
1106
  } else {
969
- console.warn("?????????");
1107
+ existingStyleKeys.add(key);
970
1108
  }
971
- continue;
1109
+ }
972
1110
  }
973
- } catch (err) {
974
- getStyleError = err;
975
1111
  }
976
- }
977
- if (getStyleError) {
978
- console.log(" \u26A0\uFE0F postprocessing error, deopt", getStyleError);
979
- node.attributes = ogAttributes;
980
- return node;
981
- }
982
- const existingStyleKeys = /* @__PURE__ */ new Set();
983
- for (let i = attrs.length - 1; i >= 0; i--) {
984
- const attr = attrs[i];
985
- if (attr.type === "style") {
986
- for (const key in attr.value) {
987
- if (existingStyleKeys.has(key)) {
988
- delete attr.value[key];
989
- } else {
990
- existingStyleKeys.add(key);
991
- }
1112
+ if (shouldFlatten) {
1113
+ if (shouldPrintDebug) {
1114
+ console.log(" [\u2705] flattening", originalNodeName, flatNode);
1115
+ }
1116
+ node.name.name = flatNode;
1117
+ res.flattened++;
1118
+ if (closingElement) {
1119
+ closingElement.name.name = flatNode;
992
1120
  }
993
1121
  }
994
- }
995
- if (shouldPrintDebug) {
996
- console.log(" - attrs (after):\n", logLines(attrs.map(attrStr).join(", ")));
997
- }
998
- if (shouldFlatten) {
999
1122
  if (shouldPrintDebug) {
1000
- console.log(" [\u2705] flattening", originalNodeName, flatNode);
1123
+ console.log(` \u274A\u274A inline props (${inlined.size}):`, shouldDeopt ? " deopted" : "", hasSpread ? " has spread" : "", staticConfig.neverFlatten ? "neverFlatten" : "");
1124
+ console.log(" - attrs (end):\n", logLines(attrs.map(attrStr).join(", ")));
1001
1125
  }
1002
- node.name.name = flatNode;
1003
- res.flattened++;
1004
- if (closingElement) {
1005
- closingElement.name.name = flatNode;
1126
+ onExtractTag({
1127
+ attrs,
1128
+ node,
1129
+ lineNumbers,
1130
+ filePath,
1131
+ attemptEval,
1132
+ jsxPath: traversePath,
1133
+ originalNodeName,
1134
+ isFlattened: shouldFlatten,
1135
+ programPath
1136
+ });
1137
+ } catch (err) {
1138
+ throw err;
1139
+ } finally {
1140
+ if (hasBooleanDebugTrue) {
1141
+ shouldPrintDebug = false;
1006
1142
  }
1007
1143
  }
1008
- if (shouldPrintDebug) {
1009
- console.log(` \u274A\u274A inline props (${inlinePropCount}):`, shouldDeopt ? " deopted" : "", hasSpread ? " has spread" : "", staticConfig.neverFlatten ? "neverFlatten" : "");
1010
- console.log(" - attrs (end):\n", logLines(attrs.map(attrStr).join(", ")));
1011
- }
1012
- onExtractTag({
1013
- attrs,
1014
- node,
1015
- lineNumbers,
1016
- filePath,
1017
- attemptEval,
1018
- jsxPath: traversePath,
1019
- originalNodeName,
1020
- isFlattened: shouldFlatten,
1021
- programPath
1022
- });
1023
1144
  }
1024
1145
  });
1146
+ tm.mark("jsx-done", shouldPrintDebug === "verbose");
1025
1147
  if (modifiedComponents.size) {
1026
1148
  const all = Array.from(modifiedComponents);
1027
1149
  if (shouldPrintDebug) {
@@ -1031,6 +1153,7 @@ function createExtractor() {
1031
1153
  removeUnusedHooks(comp, shouldPrintDebug);
1032
1154
  }
1033
1155
  }
1156
+ tm.done(shouldPrintDebug === "verbose");
1034
1157
  return res;
1035
1158
  }
1036
1159
  };