@tamagui/static 1.0.0-beta.230 → 1.0.0-beta.3

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