@tamagui/static 1.0.0-beta.187 → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/constants.js +8 -0
- package/dist/cjs/extractor/accessSafe.js +4 -0
- package/dist/cjs/extractor/babelParse.js +5 -0
- package/dist/cjs/extractor/babelParse.js.map +2 -2
- package/dist/cjs/extractor/buildClassName.js +4 -0
- package/dist/cjs/extractor/createEvaluator.js +5 -0
- package/dist/cjs/extractor/createExtractor.js +863 -733
- package/dist/cjs/extractor/createExtractor.js.map +2 -2
- package/dist/cjs/extractor/ensureImportingConcat.js +4 -0
- package/dist/cjs/extractor/evaluateAstNode.js +4 -0
- package/dist/cjs/extractor/extractHelpers.js +11 -0
- package/dist/cjs/extractor/extractMediaStyle.js +7 -2
- package/dist/cjs/extractor/extractMediaStyle.js.map +2 -2
- package/dist/cjs/extractor/extractToClassNames.js +19 -10
- package/dist/cjs/extractor/extractToClassNames.js.map +2 -2
- package/dist/cjs/extractor/findTopmostFunction.js +4 -0
- package/dist/cjs/extractor/generatedUid.js +4 -0
- package/dist/cjs/extractor/getPrefixLogs.js +4 -0
- package/dist/cjs/extractor/getPropValueFromAttributes.js +4 -0
- package/dist/cjs/extractor/getSourceModule.js +4 -0
- package/dist/cjs/extractor/getStaticBindingsForScope.js +14 -1
- package/dist/cjs/extractor/getStaticBindingsForScope.js.map +2 -2
- package/dist/cjs/extractor/hoistClassNames.js +4 -0
- package/dist/cjs/extractor/literalToAst.js +5 -0
- package/dist/cjs/extractor/loadTamagui.js +67 -47
- package/dist/cjs/extractor/loadTamagui.js.map +2 -2
- package/dist/cjs/extractor/logLines.js +4 -0
- package/dist/cjs/extractor/normalizeTernaries.js +4 -0
- package/dist/cjs/extractor/removeUnusedHooks.js +4 -0
- package/dist/cjs/extractor/timer.js +46 -0
- package/dist/cjs/extractor/timer.js.map +7 -0
- package/dist/cjs/extractor/validHTMLAttributes.js +4 -0
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/patchReactNativeWeb.js +23 -7
- package/dist/cjs/patchReactNativeWeb.js.map +2 -2
- package/dist/esm/extractor/babelParse.js.map +2 -2
- package/dist/esm/extractor/createExtractor.js +869 -742
- package/dist/esm/extractor/createExtractor.js.map +2 -2
- package/dist/esm/extractor/extractMediaStyle.js +2 -2
- package/dist/esm/extractor/extractMediaStyle.js.map +2 -2
- package/dist/esm/extractor/extractToClassNames.js +15 -10
- package/dist/esm/extractor/extractToClassNames.js.map +2 -2
- package/dist/esm/extractor/getStaticBindingsForScope.js +10 -1
- package/dist/esm/extractor/getStaticBindingsForScope.js.map +2 -2
- package/dist/esm/extractor/loadTamagui.js +63 -47
- package/dist/esm/extractor/loadTamagui.js.map +2 -2
- package/dist/esm/extractor/timer.js +24 -0
- package/dist/esm/extractor/timer.js.map +7 -0
- package/dist/esm/patchReactNativeWeb.js +19 -7
- package/dist/esm/patchReactNativeWeb.js.map +2 -2
- package/dist/jsx/extractor/createExtractor.js +869 -742
- package/dist/jsx/extractor/extractMediaStyle.js +2 -2
- package/dist/jsx/extractor/extractToClassNames.js +15 -10
- package/dist/jsx/extractor/getStaticBindingsForScope.js +10 -1
- package/dist/jsx/extractor/loadTamagui.js +63 -47
- package/dist/jsx/extractor/timer.js +23 -0
- package/dist/jsx/patchReactNativeWeb.js +19 -7
- package/package.json +25 -25
- package/types/extractor/babelParse.d.ts.map +1 -1
- package/types/extractor/createExtractor.d.ts +2 -0
- package/types/extractor/createExtractor.d.ts.map +1 -1
- package/types/extractor/extractMediaStyle.d.ts +1 -1
- package/types/extractor/extractMediaStyle.d.ts.map +1 -1
- package/types/extractor/extractToClassNames.d.ts +2 -1
- package/types/extractor/extractToClassNames.d.ts.map +1 -1
- package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -1
- package/types/extractor/loadTamagui.d.ts +2 -2
- package/types/extractor/loadTamagui.d.ts.map +1 -1
- package/types/extractor/timer.d.ts +5 -0
- package/types/extractor/timer.d.ts.map +1 -0
- package/types/patchReactNativeWeb.d.ts.map +1 -1
- 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,12 +23,22 @@ 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,
|
|
26
30
|
style: true,
|
|
27
31
|
className: true
|
|
28
32
|
};
|
|
33
|
+
const INLINE_EXTRACTABLE = {
|
|
34
|
+
ref: "ref",
|
|
35
|
+
key: "key",
|
|
36
|
+
onPress: "onClick",
|
|
37
|
+
onHoverIn: "onMouseEnter",
|
|
38
|
+
onHoverOut: "onMouseLeave",
|
|
39
|
+
onPressIn: "onMouseDown",
|
|
40
|
+
onPressOut: "onMouseUp"
|
|
41
|
+
};
|
|
29
42
|
const isAttr = /* @__PURE__ */ __name((x) => x.type === "attr", "isAttr");
|
|
30
43
|
const validHooks = {
|
|
31
44
|
useMedia: true,
|
|
@@ -33,11 +46,11 @@ const validHooks = {
|
|
|
33
46
|
};
|
|
34
47
|
const createTernary = /* @__PURE__ */ __name((x) => x, "createTernary");
|
|
35
48
|
function createExtractor() {
|
|
49
|
+
if (!process.env.TAMAGUI_TARGET) {
|
|
50
|
+
console.log('\u26A0\uFE0F Please set process.env.TAMAGUI_TARGET to either "web" or "native"');
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
36
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);
|
|
37
|
-
require("esbuild-register/dist/node").register({
|
|
38
|
-
target: "es2019",
|
|
39
|
-
format: "cjs"
|
|
40
|
-
});
|
|
41
54
|
let loadedTamaguiConfig;
|
|
42
55
|
let hasLogged = false;
|
|
43
56
|
return {
|
|
@@ -70,12 +83,18 @@ function createExtractor() {
|
|
|
70
83
|
if (!Array.isArray(props.components)) {
|
|
71
84
|
throw new Error(`Must provide components array with list of Tamagui component modules`);
|
|
72
85
|
}
|
|
86
|
+
const ogDebug = shouldPrintDebug;
|
|
87
|
+
const tm = timer();
|
|
73
88
|
const { components, tamaguiConfig } = loadTamagui({
|
|
74
89
|
config,
|
|
75
90
|
components: props.components || ["tamagui"]
|
|
76
91
|
});
|
|
92
|
+
if (shouldPrintDebug === "verbose") {
|
|
93
|
+
console.log("tamagui.config.ts:", { components, config });
|
|
94
|
+
}
|
|
95
|
+
tm.mark("load-tamagui", shouldPrintDebug === "verbose");
|
|
77
96
|
loadedTamaguiConfig = tamaguiConfig;
|
|
78
|
-
const defaultTheme = tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]];
|
|
97
|
+
const defaultTheme = proxyThemeVariables(tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]]);
|
|
79
98
|
const body = fileOrPath.type === "Program" ? fileOrPath.get("body") : fileOrPath.program.body;
|
|
80
99
|
const isInternalImport = /* @__PURE__ */ __name((importStr) => {
|
|
81
100
|
return isInsideTamagui(sourcePath) && importStr[0] === ".";
|
|
@@ -87,17 +106,26 @@ function createExtractor() {
|
|
|
87
106
|
obj[name] = components[name];
|
|
88
107
|
return obj;
|
|
89
108
|
}, {});
|
|
109
|
+
if (shouldPrintDebug === "verbose") {
|
|
110
|
+
console.log("validComponents", Object.keys(validComponents));
|
|
111
|
+
}
|
|
90
112
|
let doesUseValidImport = false;
|
|
113
|
+
let hasImportedTheme = false;
|
|
91
114
|
for (const bodyPath of body) {
|
|
92
115
|
if (bodyPath.type !== "ImportDeclaration")
|
|
93
116
|
continue;
|
|
94
117
|
const node = "node" in bodyPath ? bodyPath.node : bodyPath;
|
|
95
118
|
const from = node.source.value;
|
|
96
|
-
|
|
97
|
-
|
|
119
|
+
const isValidImport = props.components.includes(from) || isInternalImport(from);
|
|
120
|
+
if (isValidImport) {
|
|
121
|
+
const isValidComponent = node.specifiers.some((specifier) => {
|
|
98
122
|
const name = specifier.local.name;
|
|
99
123
|
return validComponents[name] || validHooks[name];
|
|
100
|
-
})
|
|
124
|
+
});
|
|
125
|
+
if (shouldPrintDebug === "verbose") {
|
|
126
|
+
console.log("import from", from, { isValidComponent });
|
|
127
|
+
}
|
|
128
|
+
if (isValidComponent) {
|
|
101
129
|
doesUseValidImport = true;
|
|
102
130
|
break;
|
|
103
131
|
}
|
|
@@ -109,6 +137,7 @@ function createExtractor() {
|
|
|
109
137
|
if (!doesUseValidImport) {
|
|
110
138
|
return null;
|
|
111
139
|
}
|
|
140
|
+
tm.mark("import-check", shouldPrintDebug === "verbose");
|
|
112
141
|
let couldntParse = false;
|
|
113
142
|
const modifiedComponents = /* @__PURE__ */ new Set();
|
|
114
143
|
const bindingCache = {};
|
|
@@ -130,6 +159,7 @@ function createExtractor() {
|
|
|
130
159
|
},
|
|
131
160
|
JSXElement(traversePath) {
|
|
132
161
|
var _a, _b, _c, _d, _e;
|
|
162
|
+
tm.mark("jsx-element", shouldPrintDebug === "verbose");
|
|
133
163
|
const node = traversePath.node.openingElement;
|
|
134
164
|
const ogAttributes = node.attributes;
|
|
135
165
|
const componentName = findComponentName(traversePath.scope);
|
|
@@ -156,579 +186,772 @@ function createExtractor() {
|
|
|
156
186
|
}
|
|
157
187
|
const originalNodeName = node.name.name;
|
|
158
188
|
res.found++;
|
|
159
|
-
|
|
160
|
-
console.log(`
|
|
161
|
-
<${originalNodeName} />`);
|
|
162
|
-
}
|
|
163
|
-
const filePath = sourcePath.replace(process.cwd(), ".");
|
|
189
|
+
const filePath = `./${relative(process.cwd(), sourcePath)}`;
|
|
164
190
|
const lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "";
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
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(", "));
|
|
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;
|
|
219
201
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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;
|
|
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);
|
|
250
219
|
}
|
|
251
|
-
|
|
220
|
+
return attr;
|
|
252
221
|
}
|
|
222
|
+
return [
|
|
223
|
+
...createTernariesFromObjectProperties(test, alt) || [],
|
|
224
|
+
...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
|
|
225
|
+
].map((ternary) => ({
|
|
226
|
+
type: "ternary",
|
|
227
|
+
value: ternary
|
|
228
|
+
}));
|
|
253
229
|
}
|
|
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
230
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
}
|
|
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);
|
|
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;
|
|
296
237
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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;
|
|
238
|
+
const name = attribute.name.name;
|
|
239
|
+
if (excludeProps == null ? void 0 : excludeProps.has(name)) {
|
|
240
|
+
if (shouldPrintDebug) {
|
|
241
|
+
console.log(" excluding prop", name);
|
|
321
242
|
}
|
|
322
|
-
return
|
|
323
|
-
...createTernariesFromObjectProperties(test, alt) || [],
|
|
324
|
-
...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
|
|
325
|
-
].map((ternary) => ({
|
|
326
|
-
type: "ternary",
|
|
327
|
-
value: ternary
|
|
328
|
-
}));
|
|
243
|
+
return null;
|
|
329
244
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
245
|
+
if (inlineProps.has(name)) {
|
|
246
|
+
inlined.set(name, name);
|
|
247
|
+
if (shouldPrintDebug) {
|
|
248
|
+
console.log(" ! inlining, inline prop", name);
|
|
249
|
+
}
|
|
250
|
+
return attr;
|
|
334
251
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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;
|
|
342
259
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (inlineProps.has(name)) {
|
|
346
|
-
inlinePropCount++;
|
|
347
|
-
if (shouldPrintDebug) {
|
|
348
|
-
console.log(" ! inlining, inline prop", name);
|
|
260
|
+
if (UNTOUCHED_PROPS[name]) {
|
|
261
|
+
return attr;
|
|
349
262
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
shouldDeopt = true;
|
|
354
|
-
inlinePropCount++;
|
|
355
|
-
if (shouldPrintDebug) {
|
|
356
|
-
console.log(" ! inlining, deopted prop", name);
|
|
263
|
+
if (INLINE_EXTRACTABLE[name]) {
|
|
264
|
+
inlined.set(name, INLINE_EXTRACTABLE[name]);
|
|
265
|
+
return attr;
|
|
357
266
|
}
|
|
358
|
-
|
|
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) {
|
|
267
|
+
if (name.startsWith("data-")) {
|
|
368
268
|
return attr;
|
|
369
269
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
+
}
|
|
382
287
|
}
|
|
383
288
|
}
|
|
384
289
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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;
|
|
391
306
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (shouldPrintDebug) {
|
|
398
|
-
console.log(" ! inlining, ref", name);
|
|
307
|
+
if (name === "tag") {
|
|
308
|
+
return {
|
|
309
|
+
type: "attr",
|
|
310
|
+
value: path.node
|
|
311
|
+
};
|
|
399
312
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
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);
|
|
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;
|
|
414
321
|
}
|
|
415
|
-
inlinePropCount++;
|
|
416
|
-
return attr;
|
|
417
322
|
}
|
|
418
323
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
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);
|
|
430
|
-
}
|
|
324
|
+
if (name === "theme") {
|
|
325
|
+
inlined.set("theme", attr.value);
|
|
326
|
+
return attr;
|
|
431
327
|
}
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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)) {
|
|
436
344
|
return {
|
|
437
|
-
type: "
|
|
438
|
-
value:
|
|
345
|
+
type: "style",
|
|
346
|
+
value: { [name]: styleValue },
|
|
347
|
+
name,
|
|
348
|
+
attr: path.node
|
|
439
349
|
};
|
|
440
350
|
}
|
|
351
|
+
if (validHTMLAttributes[key]) {
|
|
352
|
+
return attr;
|
|
353
|
+
}
|
|
441
354
|
if (shouldPrintDebug) {
|
|
442
355
|
console.log(" ! inlining, non-static", key);
|
|
443
356
|
}
|
|
444
|
-
|
|
357
|
+
didInline = true;
|
|
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
|
+
}
|
|
365
|
+
return attr;
|
|
445
366
|
}
|
|
446
|
-
return
|
|
447
|
-
type: "style",
|
|
448
|
-
value: { [name]: styleValue },
|
|
449
|
-
name,
|
|
450
|
-
attr: path.node
|
|
451
|
-
};
|
|
452
|
-
});
|
|
453
|
-
if (inlinePropCount) {
|
|
454
|
-
return attr;
|
|
367
|
+
return attributes;
|
|
455
368
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
+
}
|
|
380
|
+
return {
|
|
381
|
+
type: "style",
|
|
382
|
+
value: { [name]: styleValue },
|
|
383
|
+
name,
|
|
384
|
+
attr: path.node
|
|
385
|
+
};
|
|
386
|
+
} else {
|
|
387
|
+
inlined.set(name, true);
|
|
388
|
+
return attr;
|
|
466
389
|
}
|
|
467
390
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
391
|
+
if (t.isBinaryExpression(value)) {
|
|
392
|
+
if (shouldPrintDebug) {
|
|
393
|
+
console.log(` binary expression ${name} = `, value);
|
|
394
|
+
}
|
|
395
|
+
const { operator, left, right } = value;
|
|
396
|
+
const lVal = attemptEvalSafe(left);
|
|
397
|
+
const rVal = attemptEvalSafe(right);
|
|
398
|
+
if (shouldPrintDebug) {
|
|
399
|
+
console.log(` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`);
|
|
400
|
+
}
|
|
401
|
+
if (lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
|
|
402
|
+
const ternary = addBinaryConditional(operator, left, right);
|
|
403
|
+
if (ternary)
|
|
404
|
+
return ternary;
|
|
405
|
+
}
|
|
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`);
|
|
413
|
+
}
|
|
414
|
+
inlined.set(name, true);
|
|
415
|
+
return attr;
|
|
484
416
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
if (
|
|
488
|
-
|
|
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 };
|
|
489
423
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (
|
|
493
|
-
|
|
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 };
|
|
494
430
|
}
|
|
431
|
+
inlined.set(name, true);
|
|
495
432
|
if (shouldPrintDebug) {
|
|
496
|
-
console.log(`
|
|
433
|
+
console.log(` ! inline no match ${name}`, value);
|
|
497
434
|
}
|
|
498
|
-
inlinePropCount++;
|
|
499
435
|
return attr;
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
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 {
|
|
546
|
-
if (shouldPrintDebug) {
|
|
547
|
-
console.log("attempt", value2.alternate, value2.consequent);
|
|
548
|
-
}
|
|
549
|
-
const aVal = attemptEval(value2.alternate);
|
|
550
|
-
const cVal = attemptEval(value2.consequent);
|
|
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));
|
|
551
440
|
if (shouldPrintDebug) {
|
|
552
|
-
|
|
553
|
-
console.log(" static ternary", type, cVal, aVal);
|
|
441
|
+
console.log(" binaryConditional", cond.test, cons, alt);
|
|
554
442
|
}
|
|
555
443
|
return {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
444
|
+
type: "ternary",
|
|
445
|
+
value: {
|
|
446
|
+
test: cond.test,
|
|
447
|
+
remove,
|
|
448
|
+
alternate: { [name]: alt },
|
|
449
|
+
consequent: { [name]: cons }
|
|
450
|
+
}
|
|
560
451
|
};
|
|
561
|
-
} catch (err) {
|
|
562
|
-
if (shouldPrintDebug) {
|
|
563
|
-
console.log(" cant eval ternary", err.message);
|
|
564
|
-
}
|
|
565
452
|
}
|
|
453
|
+
return null;
|
|
566
454
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
function getStaticLogical(value2) {
|
|
571
|
-
if (t.isLogicalExpression(value2)) {
|
|
572
|
-
if (value2.operator === "&&") {
|
|
455
|
+
__name(addBinaryConditional, "addBinaryConditional");
|
|
456
|
+
function getStaticConditional(value2) {
|
|
457
|
+
if (t.isConditionalExpression(value2)) {
|
|
573
458
|
try {
|
|
574
|
-
const
|
|
459
|
+
const aVal = attemptEval(value2.alternate);
|
|
460
|
+
const cVal = attemptEval(value2.consequent);
|
|
575
461
|
if (shouldPrintDebug) {
|
|
576
|
-
|
|
462
|
+
const type = value2.test.type;
|
|
463
|
+
console.log(" static ternary", type, cVal, aVal);
|
|
577
464
|
}
|
|
578
465
|
return {
|
|
579
|
-
test: value2.
|
|
466
|
+
test: value2.test,
|
|
580
467
|
remove,
|
|
581
|
-
consequent: { [name]:
|
|
582
|
-
alternate:
|
|
468
|
+
consequent: { [name]: cVal },
|
|
469
|
+
alternate: { [name]: aVal }
|
|
583
470
|
};
|
|
584
471
|
} catch (err) {
|
|
585
472
|
if (shouldPrintDebug) {
|
|
586
|
-
console.log("
|
|
473
|
+
console.log(" cant eval ternary", err.message);
|
|
587
474
|
}
|
|
588
475
|
}
|
|
589
476
|
}
|
|
477
|
+
return null;
|
|
590
478
|
}
|
|
591
|
-
|
|
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;
|
|
502
|
+
}
|
|
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;
|
|
516
|
+
}
|
|
517
|
+
return true;
|
|
518
|
+
});
|
|
519
|
+
}, createTernariesFromObjectProperties = function(test, side, ternaryPartial = {}) {
|
|
520
|
+
if (!side) {
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
if (!isExtractable(side)) {
|
|
524
|
+
throw new Error("not extractable");
|
|
525
|
+
}
|
|
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
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
console.log("\u26A0\uFE0F not expression", property);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
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
|
+
});
|
|
587
|
+
});
|
|
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
|
+
}
|
|
597
|
+
}
|
|
598
|
+
Object.assign(prev2[key], next[key]);
|
|
599
|
+
} else {
|
|
600
|
+
prev2[key] = next[key];
|
|
601
|
+
}
|
|
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} />`);
|
|
592
614
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
615
|
+
if (shouldAddDebugProp && !disableDebugAttr) {
|
|
616
|
+
res.modified++;
|
|
617
|
+
node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(tagId.join(" "))));
|
|
618
|
+
}
|
|
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) {
|
|
626
|
+
return;
|
|
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);
|
|
662
|
+
if (shouldPrintDebug) {
|
|
663
|
+
console.log(" staticNamespace", Object.keys(staticNamespace).join(", "));
|
|
664
|
+
}
|
|
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);
|
|
670
|
+
return;
|
|
601
671
|
}
|
|
602
|
-
|
|
603
|
-
|
|
672
|
+
let arg;
|
|
673
|
+
try {
|
|
674
|
+
arg = attemptEval(attr.argument);
|
|
675
|
+
} catch (e) {
|
|
604
676
|
if (shouldPrintDebug) {
|
|
605
|
-
console.log("
|
|
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;
|
|
606
702
|
}
|
|
607
|
-
return false;
|
|
608
703
|
}
|
|
609
|
-
return true;
|
|
610
704
|
});
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
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
|
+
}
|
|
715
|
+
const serialize = require("babel-literal-to-ast");
|
|
716
|
+
const val = staticConfig.defaultProps[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
|
+
}
|
|
616
726
|
}
|
|
617
|
-
|
|
618
|
-
|
|
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
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
type: "attr",
|
|
746
|
+
value: path.node
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
}).flat(4).filter(isPresent);
|
|
750
|
+
if (shouldPrintDebug) {
|
|
751
|
+
console.log(" - attrs (before):\n", logLines(attrs.map(attrStr).join(", ")));
|
|
619
752
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
753
|
+
node.attributes = attrs.filter(isAttr).map((x) => x.value);
|
|
754
|
+
if (couldntParse || shouldDeopt) {
|
|
755
|
+
if (shouldPrintDebug) {
|
|
756
|
+
console.log(` avoid optimizing:`, { couldntParse, shouldDeopt });
|
|
623
757
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
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
|
|
641
779
|
}
|
|
642
|
-
}
|
|
643
|
-
|
|
780
|
+
};
|
|
781
|
+
});
|
|
782
|
+
try {
|
|
783
|
+
return [...out, ...normalized];
|
|
784
|
+
} finally {
|
|
785
|
+
if (shouldPrintDebug) {
|
|
786
|
+
console.log(` normalizeTernaries (${ternaries.length} => ${normalized.length})`);
|
|
644
787
|
}
|
|
788
|
+
ternaries = [];
|
|
645
789
|
}
|
|
646
790
|
}
|
|
647
|
-
if (
|
|
648
|
-
|
|
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
|
-
];
|
|
791
|
+
if (cur.type === "ternary") {
|
|
792
|
+
return out;
|
|
670
793
|
}
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
}
|
|
683
|
-
__name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
|
|
684
|
-
node.attributes = attrs.filter(isAttr).map((x) => x.value);
|
|
685
|
-
if (couldntParse || shouldDeopt) {
|
|
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);
|
|
686
805
|
if (shouldPrintDebug) {
|
|
687
|
-
console.log(
|
|
806
|
+
console.log(" - flatten?", objToStr({ hasSpread, shouldDeopt, shouldFlatten, canFlattenProps, shouldWrapThme, allOtherPropsExtractable, hasOnlyStringChildren }));
|
|
688
807
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
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]));
|
|
701
820
|
}
|
|
702
|
-
if (
|
|
703
|
-
const
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
}
|
|
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;
|
|
832
|
+
}
|
|
833
|
+
const attr = {
|
|
834
|
+
type: "style",
|
|
835
|
+
name,
|
|
836
|
+
value: { [name]: value }
|
|
711
837
|
};
|
|
838
|
+
return attr;
|
|
712
839
|
});
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
|
|
840
|
+
if (defaultStyleAttrs.length) {
|
|
841
|
+
attrs = [...defaultStyleAttrs, ...attrs];
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
if (shouldDeopt) {
|
|
845
|
+
node.attributes = ogAttributes;
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
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
|
+
}
|
|
718
862
|
}
|
|
719
|
-
ternaries = [];
|
|
720
863
|
}
|
|
721
864
|
}
|
|
722
|
-
if (
|
|
723
|
-
|
|
865
|
+
if (shouldPrintDebug) {
|
|
866
|
+
console.log(" - attrs (flattened): \n", logLines(attrs.map(attrStr).join(", ")));
|
|
867
|
+
console.log(" - ensureOverriden:", Object.keys(ensureOverridden).join(", "));
|
|
868
|
+
}
|
|
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
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (cur.type !== "style") {
|
|
881
|
+
acc.push(cur);
|
|
882
|
+
return acc;
|
|
883
|
+
}
|
|
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;
|
|
890
|
+
}
|
|
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
|
+
}
|
|
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(", ")));
|
|
724
909
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
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;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
prev = cur;
|
|
940
|
+
acc.push(cur);
|
|
941
|
+
return acc;
|
|
942
|
+
}, []);
|
|
943
|
+
const state = {
|
|
944
|
+
noClassNames: true,
|
|
945
|
+
focus: false,
|
|
946
|
+
hover: false,
|
|
947
|
+
mounted: true,
|
|
948
|
+
press: false,
|
|
949
|
+
pressIn: false
|
|
950
|
+
};
|
|
951
|
+
const completeStaticProps = Object.keys(attrs).reduce((acc, index) => {
|
|
730
952
|
const cur = attrs[index];
|
|
731
953
|
if (cur.type === "style") {
|
|
954
|
+
normalizeStyleObject(cur.value);
|
|
732
955
|
Object.assign(acc, cur.value);
|
|
733
956
|
}
|
|
734
957
|
if (cur.type === "attr") {
|
|
@@ -746,282 +969,185 @@ function createExtractor() {
|
|
|
746
969
|
acc[key] = value;
|
|
747
970
|
}
|
|
748
971
|
return acc;
|
|
749
|
-
}, {})
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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)));
|
|
981
|
+
}
|
|
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
|
+
}
|
|
758
996
|
}
|
|
759
997
|
try {
|
|
760
|
-
const
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
value
|
|
998
|
+
const out = getSplitStyles(props2, staticConfig, defaultTheme, {
|
|
999
|
+
...state,
|
|
1000
|
+
fallbackProps: completeProps
|
|
1001
|
+
});
|
|
1002
|
+
const outStyle = {
|
|
1003
|
+
...out.style,
|
|
1004
|
+
...out.pseudos
|
|
768
1005
|
};
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
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;
|
|
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];
|
|
794
1012
|
}
|
|
795
1013
|
}
|
|
796
1014
|
}
|
|
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
1015
|
if (shouldPrintDebug) {
|
|
825
|
-
console.log(`
|
|
1016
|
+
console.log(` getStyles ${debugName} (props):
|
|
1017
|
+
`, logLines(objToStr(props2)));
|
|
1018
|
+
console.log(` getStyles ${debugName} (out.viewProps):
|
|
1019
|
+
`, logLines(objToStr(out.viewProps)));
|
|
1020
|
+
console.log(` getStyles ${debugName} (out.style):
|
|
1021
|
+
`, logLines(objToStr(outStyle || {}), true));
|
|
826
1022
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
}
|
|
831
|
-
return acc;
|
|
1023
|
+
return outStyle;
|
|
1024
|
+
} catch (err) {
|
|
1025
|
+
console.log("error", err.message, err.stack);
|
|
1026
|
+
return {};
|
|
832
1027
|
}
|
|
1028
|
+
}, "getStyles");
|
|
1029
|
+
const completeStylesProcessed = getStyles(completeProps, "completeStylesProcessed");
|
|
1030
|
+
if (!completeStylesProcessed) {
|
|
1031
|
+
throw new Error(`Impossible, no styles`);
|
|
833
1032
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
if (pseudos[key]) {
|
|
844
|
-
prev2[key] = prev2[key] || {};
|
|
845
|
-
Object.assign(prev2[key], next[key]);
|
|
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 });
|
|
846
1042
|
} else {
|
|
847
|
-
|
|
1043
|
+
Object.assign(firstGroup.value, toAdd);
|
|
848
1044
|
}
|
|
849
1045
|
}
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
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
|
-
}
|
|
1046
|
+
if (shouldPrintDebug) {
|
|
1047
|
+
console.log(" -- stylesToAddToInitialGroup", stylesToAddToInitialGroup.join(", "), { shouldFlatten });
|
|
1048
|
+
console.log(" -- completeStaticProps:\n", logLines(objToStr(completeStaticProps)));
|
|
1049
|
+
console.log(" -- completeStylesProcessed:\n", logLines(objToStr(completeStylesProcessed)));
|
|
879
1050
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
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;
|
|
896
1077
|
}
|
|
1078
|
+
} catch (err) {
|
|
1079
|
+
getStyleError = err;
|
|
897
1080
|
}
|
|
898
1081
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
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));
|
|
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;
|
|
913
1087
|
}
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
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
|
+
}
|
|
1100
|
+
}
|
|
918
1101
|
}
|
|
919
1102
|
}
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
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]]));
|
|
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);
|
|
1108
|
+
}
|
|
1109
|
+
delete attr.value[key];
|
|
968
1110
|
} else {
|
|
969
|
-
|
|
1111
|
+
existingStyleKeys.add(key);
|
|
970
1112
|
}
|
|
971
|
-
|
|
1113
|
+
}
|
|
972
1114
|
}
|
|
973
|
-
} catch (err) {
|
|
974
|
-
getStyleError = err;
|
|
975
1115
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
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
|
-
}
|
|
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;
|
|
992
1124
|
}
|
|
993
1125
|
}
|
|
994
|
-
}
|
|
995
|
-
if (shouldPrintDebug) {
|
|
996
|
-
console.log(" - attrs (after):\n", logLines(attrs.map(attrStr).join(", ")));
|
|
997
|
-
}
|
|
998
|
-
if (shouldFlatten) {
|
|
999
1126
|
if (shouldPrintDebug) {
|
|
1000
|
-
console.log(
|
|
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(", ")));
|
|
1001
1129
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
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;
|
|
1006
1146
|
}
|
|
1007
1147
|
}
|
|
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
1148
|
}
|
|
1024
1149
|
});
|
|
1150
|
+
tm.mark("jsx-done", shouldPrintDebug === "verbose");
|
|
1025
1151
|
if (modifiedComponents.size) {
|
|
1026
1152
|
const all = Array.from(modifiedComponents);
|
|
1027
1153
|
if (shouldPrintDebug) {
|
|
@@ -1031,6 +1157,7 @@ function createExtractor() {
|
|
|
1031
1157
|
removeUnusedHooks(comp, shouldPrintDebug);
|
|
1032
1158
|
}
|
|
1033
1159
|
}
|
|
1160
|
+
tm.done(shouldPrintDebug === "verbose");
|
|
1034
1161
|
return res;
|
|
1035
1162
|
}
|
|
1036
1163
|
};
|