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