@tamagui/static 1.0.0-alpha.0
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/README.md +21 -0
- package/dist/constants.js +10 -0
- package/dist/constants.js.map +7 -0
- package/dist/extractor/accessSafe.js +11 -0
- package/dist/extractor/accessSafe.js.map +7 -0
- package/dist/extractor/babelParse.js +30 -0
- package/dist/extractor/babelParse.js.map +7 -0
- package/dist/extractor/buildClassName.js +40 -0
- package/dist/extractor/buildClassName.js.map +7 -0
- package/dist/extractor/createEvaluator.js +51 -0
- package/dist/extractor/createEvaluator.js.map +7 -0
- package/dist/extractor/createExtractor.js +801 -0
- package/dist/extractor/createExtractor.js.map +7 -0
- package/dist/extractor/ensureImportingConcat.js +24 -0
- package/dist/extractor/ensureImportingConcat.js.map +7 -0
- package/dist/extractor/evaluateAstNode.js +94 -0
- package/dist/extractor/evaluateAstNode.js.map +7 -0
- package/dist/extractor/extractHelpers.js +92 -0
- package/dist/extractor/extractHelpers.js.map +7 -0
- package/dist/extractor/extractMediaStyle.js +151 -0
- package/dist/extractor/extractMediaStyle.js.map +7 -0
- package/dist/extractor/extractToClassNames.js +243 -0
- package/dist/extractor/extractToClassNames.js.map +7 -0
- package/dist/extractor/findTopmostFunction.js +23 -0
- package/dist/extractor/findTopmostFunction.js.map +7 -0
- package/dist/extractor/generatedUid.js +28 -0
- package/dist/extractor/generatedUid.js.map +7 -0
- package/dist/extractor/getPropValueFromAttributes.js +49 -0
- package/dist/extractor/getPropValueFromAttributes.js.map +7 -0
- package/dist/extractor/getSourceModule.js +69 -0
- package/dist/extractor/getSourceModule.js.map +7 -0
- package/dist/extractor/getStaticBindingsForScope.js +128 -0
- package/dist/extractor/getStaticBindingsForScope.js.map +7 -0
- package/dist/extractor/hoistClassNames.js +44 -0
- package/dist/extractor/hoistClassNames.js.map +7 -0
- package/dist/extractor/literalToAst.js +34 -0
- package/dist/extractor/literalToAst.js.map +7 -0
- package/dist/extractor/loadTamagui.js +41 -0
- package/dist/extractor/loadTamagui.js.map +7 -0
- package/dist/extractor/normalizeTernaries.js +52 -0
- package/dist/extractor/normalizeTernaries.js.map +7 -0
- package/dist/extractor/removeUnusedHooks.js +78 -0
- package/dist/extractor/removeUnusedHooks.js.map +7 -0
- package/dist/index.cjs +2079 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +7 -0
- package/dist/patchReactNativeWeb.js +92 -0
- package/dist/patchReactNativeWeb.js.map +7 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +7 -0
- package/package.json +72 -0
- package/src/constants.ts +10 -0
- package/src/extractor/accessSafe.ts +18 -0
- package/src/extractor/babelParse.ts +27 -0
- package/src/extractor/buildClassName.ts +61 -0
- package/src/extractor/createEvaluator.ts +68 -0
- package/src/extractor/createExtractor.ts +1113 -0
- package/src/extractor/ensureImportingConcat.ts +33 -0
- package/src/extractor/evaluateAstNode.ts +121 -0
- package/src/extractor/extractHelpers.ts +93 -0
- package/src/extractor/extractMediaStyle.ts +191 -0
- package/src/extractor/extractToClassNames.ts +337 -0
- package/src/extractor/findTopmostFunction.ts +22 -0
- package/src/extractor/generatedUid.ts +43 -0
- package/src/extractor/getPropValueFromAttributes.ts +92 -0
- package/src/extractor/getSourceModule.ts +101 -0
- package/src/extractor/getStaticBindingsForScope.ts +173 -0
- package/src/extractor/hoistClassNames.ts +45 -0
- package/src/extractor/literalToAst.ts +32 -0
- package/src/extractor/loadTamagui.ts +61 -0
- package/src/extractor/normalizeTernaries.ts +60 -0
- package/src/extractor/removeUnusedHooks.ts +76 -0
- package/src/index.ts +9 -0
- package/src/patchReactNativeWeb.ts +107 -0
- package/src/types.ts +80 -0
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import traverse from "@babel/traverse";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import * as CoreNode from "@tamagui/core-node";
|
|
6
|
+
import { stylePropsTransform } from "@tamagui/helpers";
|
|
7
|
+
import { difference, pick } from "lodash";
|
|
8
|
+
import { createEvaluator, createSafeEvaluator } from "./createEvaluator";
|
|
9
|
+
import { evaluateAstNode } from "./evaluateAstNode";
|
|
10
|
+
import { attrStr, findComponentName, isInsideTamagui, isPresent, objToStr } from "./extractHelpers";
|
|
11
|
+
import { findTopmostFunction } from "./findTopmostFunction";
|
|
12
|
+
import { getStaticBindingsForScope } from "./getStaticBindingsForScope";
|
|
13
|
+
import { literalToAst } from "./literalToAst";
|
|
14
|
+
import { loadTamagui } from "./loadTamagui";
|
|
15
|
+
import { normalizeTernaries } from "./normalizeTernaries";
|
|
16
|
+
import { removeUnusedHooks } from "./removeUnusedHooks";
|
|
17
|
+
const { mediaQueryConfig, postProcessStyles, pseudos } = CoreNode;
|
|
18
|
+
const FAILED_EVAL = Symbol("failed_style_eval");
|
|
19
|
+
const UNTOUCHED_PROPS = {
|
|
20
|
+
key: true,
|
|
21
|
+
style: true,
|
|
22
|
+
className: true
|
|
23
|
+
};
|
|
24
|
+
const isAttr = /* @__PURE__ */ __name((x) => x.type === "attr", "isAttr");
|
|
25
|
+
const validHooks = {
|
|
26
|
+
useMedia: true,
|
|
27
|
+
useTheme: true
|
|
28
|
+
};
|
|
29
|
+
const createTernary = /* @__PURE__ */ __name((x) => x, "createTernary");
|
|
30
|
+
function createExtractor() {
|
|
31
|
+
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);
|
|
32
|
+
require("esbuild-register/dist/node").register({
|
|
33
|
+
target: "es2019",
|
|
34
|
+
format: "cjs"
|
|
35
|
+
});
|
|
36
|
+
let loadedTamaguiConfig;
|
|
37
|
+
return {
|
|
38
|
+
getTamaguiConfig() {
|
|
39
|
+
return loadedTamaguiConfig;
|
|
40
|
+
},
|
|
41
|
+
parse: (fileOrPath, {
|
|
42
|
+
config = "tamagui.config.ts",
|
|
43
|
+
importsWhitelist = ["constants.js"],
|
|
44
|
+
evaluateVars = true,
|
|
45
|
+
shouldPrintDebug = false,
|
|
46
|
+
sourcePath = "",
|
|
47
|
+
onExtractTag,
|
|
48
|
+
getFlattenedNode,
|
|
49
|
+
onDidFlatten,
|
|
50
|
+
...props
|
|
51
|
+
}) => {
|
|
52
|
+
if (sourcePath === "") {
|
|
53
|
+
throw new Error(`Must provide a source file name`);
|
|
54
|
+
}
|
|
55
|
+
if (!Array.isArray(props.components)) {
|
|
56
|
+
throw new Error(`Must provide components array with list of Tamagui component modules`);
|
|
57
|
+
}
|
|
58
|
+
const { components, tamaguiConfig } = loadTamagui({
|
|
59
|
+
config,
|
|
60
|
+
components: props.components || ["tamagui"]
|
|
61
|
+
});
|
|
62
|
+
loadedTamaguiConfig = tamaguiConfig;
|
|
63
|
+
const defaultTheme = tamaguiConfig.themes[Object.keys(tamaguiConfig.themes)[0]];
|
|
64
|
+
let doesUseValidImport = false;
|
|
65
|
+
const body = fileOrPath.type === "Program" ? fileOrPath.get("body") : fileOrPath.program.body;
|
|
66
|
+
const isInternalImport = /* @__PURE__ */ __name((importStr) => isInsideTamagui(sourcePath) && importStr[0] === ".", "isInternalImport");
|
|
67
|
+
const validComponents = Object.keys(components).filter((key) => !!components[key]?.staticConfig).reduce((obj, name) => {
|
|
68
|
+
obj[name] = components[name];
|
|
69
|
+
return obj;
|
|
70
|
+
}, {});
|
|
71
|
+
for (const bodyPath of body) {
|
|
72
|
+
if (bodyPath.type !== "ImportDeclaration")
|
|
73
|
+
continue;
|
|
74
|
+
const node = "node" in bodyPath ? bodyPath.node : bodyPath;
|
|
75
|
+
const from = node.source.value;
|
|
76
|
+
if (props.components.includes(from) || isInternalImport(from)) {
|
|
77
|
+
if (node.specifiers.some((specifier) => {
|
|
78
|
+
const name = specifier.local.name;
|
|
79
|
+
return validComponents[name] || validHooks[name];
|
|
80
|
+
})) {
|
|
81
|
+
doesUseValidImport = true;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (shouldPrintDebug) {
|
|
87
|
+
console.log(sourcePath, { doesUseValidImport });
|
|
88
|
+
}
|
|
89
|
+
if (!doesUseValidImport) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
let couldntParse = false;
|
|
93
|
+
const modifiedComponents = new Set();
|
|
94
|
+
const bindingCache = {};
|
|
95
|
+
const callTraverse = /* @__PURE__ */ __name((a) => {
|
|
96
|
+
return fileOrPath.type === "File" ? traverse(fileOrPath, a) : fileOrPath.traverse(a);
|
|
97
|
+
}, "callTraverse");
|
|
98
|
+
let programPath;
|
|
99
|
+
callTraverse({
|
|
100
|
+
Program: {
|
|
101
|
+
enter(path) {
|
|
102
|
+
programPath = path;
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
JSXElement(traversePath) {
|
|
106
|
+
const node = traversePath.node.openingElement;
|
|
107
|
+
const ogAttributes = node.attributes;
|
|
108
|
+
const componentName = findComponentName(traversePath.scope);
|
|
109
|
+
const closingElement = traversePath.node.closingElement;
|
|
110
|
+
if (t.isJSXMemberExpression(closingElement?.name) || !t.isJSXIdentifier(node.name)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const binding = traversePath.scope.getBinding(node.name.name);
|
|
114
|
+
if (binding) {
|
|
115
|
+
if (!t.isImportDeclaration(binding.path.parent)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const source = binding.path.parent.source;
|
|
119
|
+
if (!props.components.includes(source.value) && !isInternalImport(source.value)) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (!validComponents[binding.identifier.name]) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const component = validComponents[node.name.name];
|
|
127
|
+
if (!component || !component.staticConfig) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const { staticConfig } = component;
|
|
131
|
+
const originalNodeName = node.name.name;
|
|
132
|
+
const isTextView = staticConfig.isText || false;
|
|
133
|
+
const validStyles = staticConfig?.validStyles ?? {};
|
|
134
|
+
let tagName = staticConfig.defaultProps?.tag ?? (isTextView ? "span" : "div");
|
|
135
|
+
traversePath.get("openingElement").get("attributes").forEach((path) => {
|
|
136
|
+
const attr = path.node;
|
|
137
|
+
if (t.isJSXSpreadAttribute(attr))
|
|
138
|
+
return;
|
|
139
|
+
if (attr.name.name !== "tag")
|
|
140
|
+
return;
|
|
141
|
+
const val = attr.value;
|
|
142
|
+
if (!t.isStringLiteral(val))
|
|
143
|
+
return;
|
|
144
|
+
tagName = val.value;
|
|
145
|
+
});
|
|
146
|
+
const flatNode = getFlattenedNode({ isTextView, tag: tagName });
|
|
147
|
+
const deoptProps = new Set([
|
|
148
|
+
...props.deoptProps ?? [],
|
|
149
|
+
...staticConfig.deoptProps ?? []
|
|
150
|
+
]);
|
|
151
|
+
const excludeProps = new Set(props.excludeProps ?? []);
|
|
152
|
+
const isExcludedProp = /* @__PURE__ */ __name((name) => {
|
|
153
|
+
const res = excludeProps.has(name);
|
|
154
|
+
if (res && shouldPrintDebug)
|
|
155
|
+
console.log(` excluding ${name}`);
|
|
156
|
+
return res;
|
|
157
|
+
}, "isExcludedProp");
|
|
158
|
+
const isDeoptedProp = /* @__PURE__ */ __name((name) => {
|
|
159
|
+
const res = deoptProps.has(name);
|
|
160
|
+
if (res && shouldPrintDebug)
|
|
161
|
+
console.log(` deopting ${name}`);
|
|
162
|
+
return res;
|
|
163
|
+
}, "isDeoptedProp");
|
|
164
|
+
if (shouldPrintDebug) {
|
|
165
|
+
console.log(`
|
|
166
|
+
<${originalNodeName} />`);
|
|
167
|
+
}
|
|
168
|
+
const staticNamespace = getStaticBindingsForScope(traversePath.scope, importsWhitelist, sourcePath, bindingCache, shouldPrintDebug);
|
|
169
|
+
const attemptEval = !evaluateVars ? evaluateAstNode : createEvaluator({
|
|
170
|
+
tamaguiConfig,
|
|
171
|
+
staticNamespace,
|
|
172
|
+
sourcePath,
|
|
173
|
+
traversePath,
|
|
174
|
+
shouldPrintDebug
|
|
175
|
+
});
|
|
176
|
+
const attemptEvalSafe = createSafeEvaluator(attemptEval);
|
|
177
|
+
if (shouldPrintDebug) {
|
|
178
|
+
console.log(" staticNamespace", Object.keys(staticNamespace).join(", "));
|
|
179
|
+
}
|
|
180
|
+
const hasDeopt = /* @__PURE__ */ __name((obj) => {
|
|
181
|
+
return Object.keys(obj).some(isDeoptedProp);
|
|
182
|
+
}, "hasDeopt");
|
|
183
|
+
const flattenedAttrs = [];
|
|
184
|
+
traversePath.get("openingElement").get("attributes").forEach((path) => {
|
|
185
|
+
const attr = path.node;
|
|
186
|
+
if (!t.isJSXSpreadAttribute(attr)) {
|
|
187
|
+
flattenedAttrs.push(attr);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
let arg;
|
|
191
|
+
try {
|
|
192
|
+
arg = attemptEval(attr.argument);
|
|
193
|
+
} catch (e) {
|
|
194
|
+
if (shouldPrintDebug) {
|
|
195
|
+
console.log(" couldnt parse spread", e.message);
|
|
196
|
+
}
|
|
197
|
+
flattenedAttrs.push(attr);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (typeof arg !== "undefined") {
|
|
201
|
+
try {
|
|
202
|
+
if (typeof arg !== "object" || arg == null) {
|
|
203
|
+
if (shouldPrintDebug) {
|
|
204
|
+
console.log(" non object or null arg", arg);
|
|
205
|
+
}
|
|
206
|
+
flattenedAttrs.push(attr);
|
|
207
|
+
} else {
|
|
208
|
+
for (const k in arg) {
|
|
209
|
+
const value = arg[k];
|
|
210
|
+
if (!value && typeof value === "object") {
|
|
211
|
+
console.log("shouldnt we handle this?", k, value, arg);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
flattenedAttrs.push(t.jsxAttribute(t.jsxIdentifier(k), t.jsxExpressionContainer(literalToAst(value))));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} catch (err) {
|
|
218
|
+
console.warn("cant parse spread, caught err", err);
|
|
219
|
+
couldntParse = true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
if (couldntParse) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
node.attributes = flattenedAttrs;
|
|
227
|
+
if (staticConfig.defaultProps) {
|
|
228
|
+
for (const key in staticConfig.defaultProps) {
|
|
229
|
+
const serialize = require("babel-literal-to-ast");
|
|
230
|
+
const val = staticConfig.defaultProps[key];
|
|
231
|
+
node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(key), typeof val === "string" ? t.stringLiteral(val) : t.jsxExpressionContainer(serialize(val))));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
let attrs = [];
|
|
235
|
+
let shouldDeopt = false;
|
|
236
|
+
let inlinePropCount = 0;
|
|
237
|
+
let isFlattened = false;
|
|
238
|
+
attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
|
|
239
|
+
try {
|
|
240
|
+
const res = evaluateAttribute(path);
|
|
241
|
+
if (!res) {
|
|
242
|
+
path.remove();
|
|
243
|
+
}
|
|
244
|
+
return res;
|
|
245
|
+
} catch (err) {
|
|
246
|
+
console.log("Error extracting attribute", err.message, err.stack);
|
|
247
|
+
console.log("node", path.node);
|
|
248
|
+
return {
|
|
249
|
+
type: "attr",
|
|
250
|
+
value: path.node
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}).flat(4).filter(isPresent);
|
|
254
|
+
function isStaticAttributeName(name) {
|
|
255
|
+
return !!(!!validStyles[name] || staticConfig.validPropsExtra?.[name] || !!pseudos[name] || staticConfig.variants?.[name] || tamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
|
|
256
|
+
}
|
|
257
|
+
__name(isStaticAttributeName, "isStaticAttributeName");
|
|
258
|
+
function isExtractable(obj) {
|
|
259
|
+
return t.isObjectExpression(obj) && obj.properties.every((prop) => {
|
|
260
|
+
if (!t.isObjectProperty(prop)) {
|
|
261
|
+
console.log("not object prop", prop);
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
const propName = prop.key["name"];
|
|
265
|
+
if (!isStaticAttributeName(propName) && propName !== "tag") {
|
|
266
|
+
console.log(" not a valid style prop!", propName);
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
return true;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
__name(isExtractable, "isExtractable");
|
|
273
|
+
function createTernariesFromObjectProperties(test, side, ternaryPartial = {}) {
|
|
274
|
+
if (!side) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
if (!isExtractable(side)) {
|
|
278
|
+
throw new Error("not extractable");
|
|
279
|
+
}
|
|
280
|
+
return side.properties.flatMap((property) => {
|
|
281
|
+
if (!t.isObjectProperty(property)) {
|
|
282
|
+
throw new Error("expected object property");
|
|
283
|
+
}
|
|
284
|
+
if (t.isConditionalExpression(property.value)) {
|
|
285
|
+
const [truthy, falsy] = [
|
|
286
|
+
t.objectExpression([t.objectProperty(property.key, property.value.consequent)]),
|
|
287
|
+
t.objectExpression([t.objectProperty(property.key, property.value.alternate)])
|
|
288
|
+
].map((x) => attemptEval(x));
|
|
289
|
+
return [
|
|
290
|
+
createTernary({
|
|
291
|
+
remove() {
|
|
292
|
+
},
|
|
293
|
+
...ternaryPartial,
|
|
294
|
+
test: t.logicalExpression("&&", test, property.value.test),
|
|
295
|
+
consequent: truthy,
|
|
296
|
+
alternate: null
|
|
297
|
+
}),
|
|
298
|
+
createTernary({
|
|
299
|
+
...ternaryPartial,
|
|
300
|
+
test: t.logicalExpression("&&", test, t.unaryExpression("!", property.value.test)),
|
|
301
|
+
consequent: falsy,
|
|
302
|
+
alternate: null,
|
|
303
|
+
remove() {
|
|
304
|
+
}
|
|
305
|
+
})
|
|
306
|
+
];
|
|
307
|
+
}
|
|
308
|
+
const obj = t.objectExpression([t.objectProperty(property.key, property.value)]);
|
|
309
|
+
const consequent = attemptEval(obj);
|
|
310
|
+
return createTernary({
|
|
311
|
+
remove() {
|
|
312
|
+
},
|
|
313
|
+
...ternaryPartial,
|
|
314
|
+
test,
|
|
315
|
+
consequent,
|
|
316
|
+
alternate: null
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
__name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
|
|
321
|
+
function evaluateAttribute(path) {
|
|
322
|
+
const attribute = path.node;
|
|
323
|
+
const attr = { type: "attr", value: attribute };
|
|
324
|
+
if (t.isJSXSpreadAttribute(attribute)) {
|
|
325
|
+
const arg = attribute.argument;
|
|
326
|
+
const conditional = t.isConditionalExpression(arg) ? [arg.test, arg.consequent, arg.alternate] : t.isLogicalExpression(arg) && arg.operator === "&&" ? [arg.left, arg.right, null] : null;
|
|
327
|
+
if (conditional) {
|
|
328
|
+
const [test, alt, cons] = conditional;
|
|
329
|
+
if (!test)
|
|
330
|
+
throw new Error(`no test`);
|
|
331
|
+
if ([alt, cons].some((side) => side && !isExtractable(side))) {
|
|
332
|
+
console.log("not extractable", alt, cons);
|
|
333
|
+
return attr;
|
|
334
|
+
}
|
|
335
|
+
return [
|
|
336
|
+
...createTernariesFromObjectProperties(test, alt) || [],
|
|
337
|
+
...cons && createTernariesFromObjectProperties(t.unaryExpression("!", test), cons) || []
|
|
338
|
+
].map((ternary) => ({
|
|
339
|
+
type: "ternary",
|
|
340
|
+
value: ternary
|
|
341
|
+
}));
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (t.isJSXSpreadAttribute(attribute) || !attribute.name || typeof attribute.name.name !== "string") {
|
|
345
|
+
inlinePropCount++;
|
|
346
|
+
return attr;
|
|
347
|
+
}
|
|
348
|
+
const name = attribute.name.name;
|
|
349
|
+
if (isExcludedProp(name)) {
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
if (isDeoptedProp(name)) {
|
|
353
|
+
if (shouldPrintDebug) {
|
|
354
|
+
console.log(" ! inlining, deopt prop", name);
|
|
355
|
+
}
|
|
356
|
+
inlinePropCount++;
|
|
357
|
+
return attr;
|
|
358
|
+
}
|
|
359
|
+
if (UNTOUCHED_PROPS[name]) {
|
|
360
|
+
return attr;
|
|
361
|
+
}
|
|
362
|
+
if (name[0] === "$" && t.isJSXExpressionContainer(attribute?.value)) {
|
|
363
|
+
const shortname = name.slice(1);
|
|
364
|
+
if (mediaQueryConfig[shortname]) {
|
|
365
|
+
const expression = attribute.value.expression;
|
|
366
|
+
if (!t.isJSXEmptyExpression(expression)) {
|
|
367
|
+
const ternaries2 = createTernariesFromObjectProperties(t.stringLiteral(shortname), expression, {
|
|
368
|
+
inlineMediaQuery: shortname
|
|
369
|
+
});
|
|
370
|
+
if (ternaries2) {
|
|
371
|
+
return ternaries2.map((value2) => ({
|
|
372
|
+
type: "ternary",
|
|
373
|
+
value: value2
|
|
374
|
+
}));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const [value, valuePath] = (() => {
|
|
380
|
+
if (t.isJSXExpressionContainer(attribute?.value)) {
|
|
381
|
+
return [attribute.value.expression, path.get("value")];
|
|
382
|
+
} else {
|
|
383
|
+
return [attribute.value, path.get("value")];
|
|
384
|
+
}
|
|
385
|
+
})();
|
|
386
|
+
const remove = /* @__PURE__ */ __name(() => {
|
|
387
|
+
Array.isArray(valuePath) ? valuePath.map((p) => p.remove()) : valuePath.remove();
|
|
388
|
+
}, "remove");
|
|
389
|
+
if (name === "ref") {
|
|
390
|
+
if (shouldPrintDebug) {
|
|
391
|
+
console.log(" ! inlining, ref", name);
|
|
392
|
+
}
|
|
393
|
+
inlinePropCount++;
|
|
394
|
+
return attr;
|
|
395
|
+
}
|
|
396
|
+
if (name === "tag") {
|
|
397
|
+
return {
|
|
398
|
+
type: "attr",
|
|
399
|
+
value: path.node
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const styleValue = attemptEvalSafe(value);
|
|
403
|
+
if (!isStaticAttributeName(name)) {
|
|
404
|
+
let keys = [name];
|
|
405
|
+
if (staticConfig.propMapper) {
|
|
406
|
+
const out = staticConfig.propMapper(name, styleValue, defaultTheme, staticConfig.defaultProps);
|
|
407
|
+
if (out) {
|
|
408
|
+
keys = Object.keys(out);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
if (keys.some((k) => !isStaticAttributeName(k) && k !== "tag")) {
|
|
412
|
+
if (shouldPrintDebug) {
|
|
413
|
+
console.log(" ! inlining, not static attribute name", name);
|
|
414
|
+
}
|
|
415
|
+
inlinePropCount++;
|
|
416
|
+
return attr;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (styleValue !== FAILED_EVAL) {
|
|
420
|
+
return {
|
|
421
|
+
type: "style",
|
|
422
|
+
value: { [name]: styleValue }
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
if (t.isBinaryExpression(value)) {
|
|
426
|
+
const { operator, left, right } = value;
|
|
427
|
+
const lVal = attemptEvalSafe(left);
|
|
428
|
+
const rVal = attemptEvalSafe(right);
|
|
429
|
+
if (shouldPrintDebug) {
|
|
430
|
+
console.log(` evalBinaryExpression lVal ${String(lVal)}, rVal ${String(rVal)}`);
|
|
431
|
+
}
|
|
432
|
+
if (lVal !== FAILED_EVAL && t.isConditionalExpression(right)) {
|
|
433
|
+
const ternary = addBinaryConditional(operator, left, right);
|
|
434
|
+
if (ternary)
|
|
435
|
+
return ternary;
|
|
436
|
+
}
|
|
437
|
+
if (rVal !== FAILED_EVAL && t.isConditionalExpression(left)) {
|
|
438
|
+
const ternary = addBinaryConditional(operator, right, left);
|
|
439
|
+
if (ternary)
|
|
440
|
+
return ternary;
|
|
441
|
+
}
|
|
442
|
+
if (shouldPrintDebug) {
|
|
443
|
+
console.log(` evalBinaryExpression cant extract`);
|
|
444
|
+
}
|
|
445
|
+
inlinePropCount++;
|
|
446
|
+
return attr;
|
|
447
|
+
}
|
|
448
|
+
const staticConditional = getStaticConditional(value);
|
|
449
|
+
if (staticConditional) {
|
|
450
|
+
return { type: "ternary", value: staticConditional };
|
|
451
|
+
}
|
|
452
|
+
const staticLogical = getStaticLogical(value);
|
|
453
|
+
if (staticLogical) {
|
|
454
|
+
return { type: "ternary", value: staticLogical };
|
|
455
|
+
}
|
|
456
|
+
if (shouldPrintDebug) {
|
|
457
|
+
console.log(" ! inline prop via no match", name, value.type);
|
|
458
|
+
}
|
|
459
|
+
inlinePropCount++;
|
|
460
|
+
return attr;
|
|
461
|
+
function addBinaryConditional(operator, staticExpr, cond) {
|
|
462
|
+
if (getStaticConditional(cond)) {
|
|
463
|
+
const alt = attemptEval(t.binaryExpression(operator, staticExpr, cond.alternate));
|
|
464
|
+
const cons = attemptEval(t.binaryExpression(operator, staticExpr, cond.consequent));
|
|
465
|
+
if (shouldPrintDebug) {
|
|
466
|
+
console.log(" binaryConditional", cond.test, cons, alt);
|
|
467
|
+
}
|
|
468
|
+
return {
|
|
469
|
+
type: "ternary",
|
|
470
|
+
value: {
|
|
471
|
+
test: cond.test,
|
|
472
|
+
remove,
|
|
473
|
+
alternate: { [name]: alt },
|
|
474
|
+
consequent: { [name]: cons }
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
__name(addBinaryConditional, "addBinaryConditional");
|
|
481
|
+
function getStaticConditional(value2) {
|
|
482
|
+
if (t.isConditionalExpression(value2)) {
|
|
483
|
+
try {
|
|
484
|
+
if (shouldPrintDebug) {
|
|
485
|
+
console.log("attempt", value2.alternate, value2.consequent);
|
|
486
|
+
}
|
|
487
|
+
const aVal = attemptEval(value2.alternate);
|
|
488
|
+
const cVal = attemptEval(value2.consequent);
|
|
489
|
+
if (shouldPrintDebug) {
|
|
490
|
+
const type = value2.test.type;
|
|
491
|
+
console.log(" static ternary", type, cVal, aVal);
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
test: value2.test,
|
|
495
|
+
remove,
|
|
496
|
+
consequent: { [name]: cVal },
|
|
497
|
+
alternate: { [name]: aVal }
|
|
498
|
+
};
|
|
499
|
+
} catch (err) {
|
|
500
|
+
if (shouldPrintDebug) {
|
|
501
|
+
console.log(" cant eval ternary", err.message);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
__name(getStaticConditional, "getStaticConditional");
|
|
508
|
+
function getStaticLogical(value2) {
|
|
509
|
+
if (t.isLogicalExpression(value2)) {
|
|
510
|
+
if (value2.operator === "&&") {
|
|
511
|
+
try {
|
|
512
|
+
const val = attemptEval(value2.right);
|
|
513
|
+
if (shouldPrintDebug) {
|
|
514
|
+
console.log(" staticLogical", value2.left, name, val);
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
test: value2.left,
|
|
518
|
+
remove,
|
|
519
|
+
consequent: { [name]: val },
|
|
520
|
+
alternate: null
|
|
521
|
+
};
|
|
522
|
+
} catch (err) {
|
|
523
|
+
if (shouldPrintDebug) {
|
|
524
|
+
console.log(" cant static eval logical", err);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
__name(getStaticLogical, "getStaticLogical");
|
|
532
|
+
}
|
|
533
|
+
__name(evaluateAttribute, "evaluateAttribute");
|
|
534
|
+
if (shouldPrintDebug) {
|
|
535
|
+
console.log(" - attrs (before): ", attrs.map(attrStr).join(", "));
|
|
536
|
+
}
|
|
537
|
+
node.attributes = attrs.filter(isAttr).map((x) => x.value);
|
|
538
|
+
if (couldntParse) {
|
|
539
|
+
if (shouldPrintDebug) {
|
|
540
|
+
console.log(` cancel:`, { couldntParse, shouldDeopt });
|
|
541
|
+
}
|
|
542
|
+
node.attributes = ogAttributes;
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const parentFn = findTopmostFunction(traversePath);
|
|
546
|
+
if (parentFn) {
|
|
547
|
+
modifiedComponents.add(parentFn);
|
|
548
|
+
}
|
|
549
|
+
const filePath = sourcePath.replace(process.cwd(), ".");
|
|
550
|
+
const lineNumbers = node.loc ? node.loc.start.line + (node.loc.start.line !== node.loc.end.line ? `-${node.loc.end.line}` : "") : "";
|
|
551
|
+
let ternaries = [];
|
|
552
|
+
attrs = attrs.reduce((out, cur) => {
|
|
553
|
+
const next = attrs[attrs.indexOf(cur) + 1];
|
|
554
|
+
if (cur.type === "ternary") {
|
|
555
|
+
ternaries.push(cur.value);
|
|
556
|
+
}
|
|
557
|
+
if ((!next || next.type !== "ternary") && ternaries.length) {
|
|
558
|
+
const normalized = normalizeTernaries(ternaries).map(({ alternate, consequent, ...rest }) => {
|
|
559
|
+
return {
|
|
560
|
+
type: "ternary",
|
|
561
|
+
value: {
|
|
562
|
+
...rest,
|
|
563
|
+
alternate: alternate || null,
|
|
564
|
+
consequent: consequent || null
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
});
|
|
568
|
+
try {
|
|
569
|
+
return [...out, ...normalized];
|
|
570
|
+
} finally {
|
|
571
|
+
if (shouldPrintDebug) {
|
|
572
|
+
console.log(` normalizeTernaries (${ternaries.length} => ${normalized.length})`);
|
|
573
|
+
}
|
|
574
|
+
ternaries = [];
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (cur.type === "ternary") {
|
|
578
|
+
return out;
|
|
579
|
+
}
|
|
580
|
+
out.push(cur);
|
|
581
|
+
return out;
|
|
582
|
+
}, []).flat();
|
|
583
|
+
const completeStaticProps = {
|
|
584
|
+
...Object.keys(attrs).reduce((acc, index) => {
|
|
585
|
+
const cur = attrs[index];
|
|
586
|
+
if (cur.type === "style") {
|
|
587
|
+
Object.assign(acc, cur.value);
|
|
588
|
+
}
|
|
589
|
+
if (cur.type === "attr") {
|
|
590
|
+
if (t.isJSXSpreadAttribute(cur.value)) {
|
|
591
|
+
return acc;
|
|
592
|
+
}
|
|
593
|
+
if (!t.isJSXIdentifier(cur.value.name)) {
|
|
594
|
+
return acc;
|
|
595
|
+
}
|
|
596
|
+
const key = cur.value.name.name;
|
|
597
|
+
const value = attemptEvalSafe(cur.value.value || t.booleanLiteral(true));
|
|
598
|
+
if (value === FAILED_EVAL) {
|
|
599
|
+
return acc;
|
|
600
|
+
}
|
|
601
|
+
acc[key] = value;
|
|
602
|
+
}
|
|
603
|
+
return acc;
|
|
604
|
+
}, {})
|
|
605
|
+
};
|
|
606
|
+
const hasSpread = node.attributes.some((x) => t.isJSXSpreadAttribute(x));
|
|
607
|
+
const hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText"));
|
|
608
|
+
const shouldFlatten = !shouldDeopt && inlinePropCount === 0 && !hasSpread && staticConfig.neverFlatten !== true && (staticConfig.neverFlatten === "jsx" ? hasOnlyStringChildren : true);
|
|
609
|
+
if (!shouldFlatten) {
|
|
610
|
+
attrs = attrs.reduce((acc, cur) => {
|
|
611
|
+
if (cur.type === "style") {
|
|
612
|
+
for (const key in cur.value) {
|
|
613
|
+
const shouldInsertNull = !!staticConfig.ensureOverriddenProp?.[key];
|
|
614
|
+
const isSetInAttrsAlready = attrs.some((x) => x.type === "attr" && x.value.type === "JSXAttribute" && x.value.name.name === key);
|
|
615
|
+
const shouldInsertNullOverride = shouldInsertNull && !isSetInAttrsAlready;
|
|
616
|
+
if (shouldInsertNullOverride) {
|
|
617
|
+
acc.push({
|
|
618
|
+
type: "attr",
|
|
619
|
+
value: t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(t.nullLiteral()))
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
acc.push(cur);
|
|
625
|
+
return acc;
|
|
626
|
+
}, []);
|
|
627
|
+
}
|
|
628
|
+
if (shouldPrintDebug) {
|
|
629
|
+
console.log(" - attrs (flattened): ", attrs.map(attrStr).join(", "));
|
|
630
|
+
}
|
|
631
|
+
attrs = attrs.reduce((acc, cur) => {
|
|
632
|
+
if (cur.type !== "attr" || !t.isJSXAttribute(cur.value) || typeof cur.value.name.name !== "string") {
|
|
633
|
+
if (cur.type === "style") {
|
|
634
|
+
const key = Object.keys(cur.value)[0];
|
|
635
|
+
if (!validStyles[key] && !pseudos[key]) {
|
|
636
|
+
if (shouldPrintDebug) {
|
|
637
|
+
console.log(" \u274C excluding", key);
|
|
638
|
+
}
|
|
639
|
+
return acc;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
acc.push(cur);
|
|
644
|
+
return acc;
|
|
645
|
+
}, []);
|
|
646
|
+
if (shouldPrintDebug) {
|
|
647
|
+
console.log(" - attrs (evaluated styles): ", attrs.map(attrStr).join(", "));
|
|
648
|
+
}
|
|
649
|
+
let prev = null;
|
|
650
|
+
attrs = attrs.reduce((acc, cur) => {
|
|
651
|
+
if (cur.type === "style") {
|
|
652
|
+
if (prev?.type === "style") {
|
|
653
|
+
Object.assign(prev.value, cur.value);
|
|
654
|
+
return acc;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
acc.push(cur);
|
|
658
|
+
prev = cur;
|
|
659
|
+
return acc;
|
|
660
|
+
}, []);
|
|
661
|
+
if (shouldPrintDebug) {
|
|
662
|
+
console.log(" - attrs (combined \u{1F500}): ", attrs.map(attrStr).join(", "));
|
|
663
|
+
}
|
|
664
|
+
const getStyles = /* @__PURE__ */ __name((props2) => {
|
|
665
|
+
if (!props2)
|
|
666
|
+
return;
|
|
667
|
+
if (!!excludeProps.size) {
|
|
668
|
+
for (const key in props2) {
|
|
669
|
+
if (isExcludedProp(key))
|
|
670
|
+
delete props2[key];
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const out = postProcessStyles(props2, staticConfig, defaultTheme);
|
|
674
|
+
const next = out?.style ?? props2;
|
|
675
|
+
if (shouldPrintDebug) {
|
|
676
|
+
console.log(" >> getStyles: ", objToStr(props2), "==>>", objToStr(next));
|
|
677
|
+
console.log(" >> style: ", objToStr(out.style));
|
|
678
|
+
console.log(" >> viewp: ", objToStr(out.viewProps));
|
|
679
|
+
}
|
|
680
|
+
if (staticConfig.validStyles) {
|
|
681
|
+
for (const key in next) {
|
|
682
|
+
if (!staticConfig.validStyles[key] && !pseudos[key]) {
|
|
683
|
+
delete next[key];
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return next;
|
|
688
|
+
}, "getStyles");
|
|
689
|
+
if (shouldPrintDebug) {
|
|
690
|
+
console.log(" staticConfig.defaultProps", staticConfig.defaultProps);
|
|
691
|
+
}
|
|
692
|
+
const completeStylesProcessed = getStyles({
|
|
693
|
+
...staticConfig.defaultProps,
|
|
694
|
+
...completeStaticProps
|
|
695
|
+
});
|
|
696
|
+
const stylesToAddToInitialGroup = difference(Object.keys(completeStylesProcessed), Object.keys(completeStaticProps));
|
|
697
|
+
if (stylesToAddToInitialGroup.length) {
|
|
698
|
+
const toAdd = pick(completeStylesProcessed, ...stylesToAddToInitialGroup);
|
|
699
|
+
const firstGroup = attrs.find((x) => x.type === "style");
|
|
700
|
+
if (shouldPrintDebug) {
|
|
701
|
+
console.log(" stylesToAddToInitialGroup", stylesToAddToInitialGroup.join(", "));
|
|
702
|
+
console.log(" toAdd", objToStr(toAdd));
|
|
703
|
+
}
|
|
704
|
+
if (!firstGroup) {
|
|
705
|
+
attrs.unshift({ type: "style", value: toAdd });
|
|
706
|
+
} else {
|
|
707
|
+
Object.assign(firstGroup.value, toAdd);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (shouldPrintDebug) {
|
|
711
|
+
console.log(" completeStaticProps", objToStr(completeStaticProps));
|
|
712
|
+
console.log(" completeStylesProcessed", objToStr(completeStylesProcessed));
|
|
713
|
+
}
|
|
714
|
+
for (const attr of attrs) {
|
|
715
|
+
try {
|
|
716
|
+
switch (attr.type) {
|
|
717
|
+
case "ternary":
|
|
718
|
+
const a = getStyles(attr.value.alternate);
|
|
719
|
+
const c = getStyles(attr.value.consequent);
|
|
720
|
+
attr.value.alternate = a;
|
|
721
|
+
attr.value.consequent = c;
|
|
722
|
+
if (shouldPrintDebug) {
|
|
723
|
+
console.log(" => tern ", attrStr(attr));
|
|
724
|
+
}
|
|
725
|
+
break;
|
|
726
|
+
case "style":
|
|
727
|
+
const next = {};
|
|
728
|
+
for (const key in attr.value) {
|
|
729
|
+
if (key in stylePropsTransform) {
|
|
730
|
+
next["transform"] = completeStylesProcessed["transform"];
|
|
731
|
+
} else {
|
|
732
|
+
next[key] = completeStylesProcessed[key] ?? attr.value[key];
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
attr.value = next;
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
} catch (err) {
|
|
739
|
+
if (shouldPrintDebug) {
|
|
740
|
+
console.log(" postprocessing error, deopt", err);
|
|
741
|
+
node.attributes = ogAttributes;
|
|
742
|
+
return node;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (shouldPrintDebug) {
|
|
747
|
+
console.log(" - attrs (after): ", attrs.map(attrStr).join(", "));
|
|
748
|
+
}
|
|
749
|
+
if (shouldAddDebugProp) {
|
|
750
|
+
const preName = componentName ? `${componentName}:` : "";
|
|
751
|
+
attrs.unshift({
|
|
752
|
+
type: "attr",
|
|
753
|
+
value: t.jsxAttribute(t.jsxIdentifier("data-is"), t.stringLiteral(` ${preName}${node.name.name} ${filePath.replace("./", "")}:${lineNumbers} `))
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
if (shouldFlatten) {
|
|
757
|
+
if (shouldPrintDebug) {
|
|
758
|
+
console.log(" [\u2705] flattening", originalNodeName, flatNode);
|
|
759
|
+
}
|
|
760
|
+
isFlattened = true;
|
|
761
|
+
node.name.name = flatNode;
|
|
762
|
+
onDidFlatten?.();
|
|
763
|
+
if (closingElement) {
|
|
764
|
+
closingElement.name.name = flatNode;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (shouldPrintDebug) {
|
|
768
|
+
console.log(" [\u274A] inline props ", inlinePropCount, shouldDeopt ? " deopted" : "", hasSpread ? " spread" : "", "!flatten", staticConfig.neverFlatten);
|
|
769
|
+
console.log(" - attrs (end): ", attrs.map(attrStr).join(", "));
|
|
770
|
+
}
|
|
771
|
+
onExtractTag({
|
|
772
|
+
attrs,
|
|
773
|
+
node,
|
|
774
|
+
lineNumbers,
|
|
775
|
+
filePath,
|
|
776
|
+
attemptEval,
|
|
777
|
+
jsxPath: traversePath,
|
|
778
|
+
originalNodeName,
|
|
779
|
+
isFlattened,
|
|
780
|
+
programPath
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
if (modifiedComponents.size) {
|
|
785
|
+
const all = Array.from(modifiedComponents);
|
|
786
|
+
if (shouldPrintDebug) {
|
|
787
|
+
console.log(" [\u{1FA9D}] hook check", all.length);
|
|
788
|
+
}
|
|
789
|
+
for (const comp of all) {
|
|
790
|
+
removeUnusedHooks(comp, shouldPrintDebug);
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
__name(createExtractor, "createExtractor");
|
|
797
|
+
export {
|
|
798
|
+
FAILED_EVAL,
|
|
799
|
+
createExtractor
|
|
800
|
+
};
|
|
801
|
+
//# sourceMappingURL=createExtractor.js.map
|