@tamagui/static 1.0.1-beta.68 → 1.0.1-beta.69
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/extractor/createEvaluator.js +1 -1
- package/dist/cjs/extractor/createEvaluator.js.map +2 -2
- package/dist/cjs/extractor/createExtractor.js +227 -115
- package/dist/cjs/extractor/createExtractor.js.map +2 -2
- package/dist/cjs/extractor/extractToClassNames.js +16 -20
- package/dist/cjs/extractor/extractToClassNames.js.map +2 -2
- package/dist/cjs/patchReactNativeWeb.js +29 -89
- package/dist/cjs/patchReactNativeWeb.js.map +2 -2
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/extractor/createEvaluator.js +1 -1
- package/dist/esm/extractor/createEvaluator.js.map +2 -2
- package/dist/esm/extractor/createExtractor.js +232 -119
- package/dist/esm/extractor/createExtractor.js.map +2 -2
- package/dist/esm/extractor/extractToClassNames.js +16 -20
- package/dist/esm/extractor/extractToClassNames.js.map +2 -2
- package/dist/esm/patchReactNativeWeb.js +29 -89
- package/dist/esm/patchReactNativeWeb.js.map +2 -2
- package/dist/jsx/extractor/createEvaluator.js +1 -1
- package/dist/jsx/extractor/createExtractor.js +232 -119
- package/dist/jsx/extractor/extractToClassNames.js +16 -20
- package/dist/jsx/patchReactNativeWeb.js +29 -89
- package/package.json +17 -12
- package/src/extractor/createEvaluator.ts +2 -1
- package/src/extractor/createExtractor.ts +356 -161
- package/src/extractor/extractToClassNames.ts +20 -31
- package/src/patchReactNativeWeb.ts +47 -96
- package/src/types.ts +6 -8
- package/types/extractor/createEvaluator.d.ts +1 -1
- package/types/extractor/createEvaluator.d.ts.map +1 -1
- package/types/extractor/createExtractor.d.ts +2 -1
- package/types/extractor/createExtractor.d.ts.map +1 -1
- package/types/extractor/extractToClassNames.d.ts.map +1 -1
- package/types/patchReactNativeWeb.d.ts.map +1 -1
- package/types/types.d.ts +4 -7
- package/types/types.d.ts.map +1 -1
|
@@ -6,6 +6,7 @@ import * as t from "@babel/types";
|
|
|
6
6
|
import {
|
|
7
7
|
expandStyles,
|
|
8
8
|
getSplitStyles,
|
|
9
|
+
getStylesAtomic,
|
|
9
10
|
mediaQueryConfig,
|
|
10
11
|
normalizeStyleObject,
|
|
11
12
|
proxyThemeVariables,
|
|
@@ -55,6 +56,11 @@ function createExtractor() {
|
|
|
55
56
|
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);
|
|
56
57
|
let loadedTamaguiConfig;
|
|
57
58
|
let hasLogged = false;
|
|
59
|
+
function isValidStyleKey(name, staticConfig) {
|
|
60
|
+
var _a;
|
|
61
|
+
return !!(!!((_a = staticConfig.validStyles) == null ? void 0 : _a[name]) || !!pseudos[name] || loadedTamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
|
|
62
|
+
}
|
|
63
|
+
__name(isValidStyleKey, "isValidStyleKey");
|
|
58
64
|
return {
|
|
59
65
|
getTamagui() {
|
|
60
66
|
return loadedTamaguiConfig;
|
|
@@ -66,12 +72,14 @@ function createExtractor() {
|
|
|
66
72
|
shouldPrintDebug = false,
|
|
67
73
|
sourcePath = "",
|
|
68
74
|
onExtractTag,
|
|
75
|
+
onStyleRule,
|
|
69
76
|
getFlattenedNode,
|
|
70
77
|
disable,
|
|
71
78
|
disableExtraction,
|
|
72
79
|
disableExtractInlineMedia,
|
|
73
80
|
disableExtractVariables,
|
|
74
81
|
disableDebugAttr,
|
|
82
|
+
extractStyledDefinitions,
|
|
75
83
|
prefixLogs,
|
|
76
84
|
excludeProps,
|
|
77
85
|
target,
|
|
@@ -129,6 +137,16 @@ function createExtractor() {
|
|
|
129
137
|
continue;
|
|
130
138
|
const node = "node" in bodyPath ? bodyPath.node : bodyPath;
|
|
131
139
|
const from = node.source.value;
|
|
140
|
+
if (extractStyledDefinitions) {
|
|
141
|
+
if (from === "@tamagui/core" || from === "tamagui") {
|
|
142
|
+
if (node.specifiers.some((specifier) => {
|
|
143
|
+
return specifier.local.name === "styled";
|
|
144
|
+
})) {
|
|
145
|
+
doesUseValidImport = true;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
132
150
|
const isValidImport = props.components.includes(from) || isInternalImport(from);
|
|
133
151
|
if (isValidImport) {
|
|
134
152
|
const isValidComponent = node.specifiers.some((specifier) => {
|
|
@@ -159,6 +177,7 @@ function createExtractor() {
|
|
|
159
177
|
}, "callTraverse");
|
|
160
178
|
let programPath;
|
|
161
179
|
const res = {
|
|
180
|
+
styled: 0,
|
|
162
181
|
flattened: 0,
|
|
163
182
|
optimized: 0,
|
|
164
183
|
modified: 0,
|
|
@@ -170,8 +189,90 @@ function createExtractor() {
|
|
|
170
189
|
programPath = path;
|
|
171
190
|
}
|
|
172
191
|
},
|
|
192
|
+
CallExpression(path) {
|
|
193
|
+
if (disable || disableExtraction) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (!t.isIdentifier(path.node.callee) || path.node.callee.name !== "styled") {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const name = t.isVariableDeclarator(path.parent) && t.isIdentifier(path.parent.id) ? path.parent.id.name : "unknown";
|
|
200
|
+
const definition = path.node.arguments[1];
|
|
201
|
+
if (!name || !definition || !t.isObjectExpression(definition)) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const Component = validComponents[name];
|
|
205
|
+
if (!Component) {
|
|
206
|
+
if (shouldPrintDebug) {
|
|
207
|
+
console.log(`Didn't recognize styled(${name}), ${name} isn't in design system provided to tamagui.config.ts`);
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const componentSkipProps = /* @__PURE__ */ new Set([
|
|
212
|
+
...Component.staticConfig.inlineWhenUnflattened || [],
|
|
213
|
+
...Component.staticConfig.inlineProps || [],
|
|
214
|
+
...Component.staticConfig.deoptProps || []
|
|
215
|
+
]);
|
|
216
|
+
const skipped = [];
|
|
217
|
+
const styles = {};
|
|
218
|
+
const skipProps = {
|
|
219
|
+
variants: true,
|
|
220
|
+
defaultVariants: true,
|
|
221
|
+
name: true
|
|
222
|
+
};
|
|
223
|
+
const staticNamespace = getStaticBindingsForScope(path.scope, importsWhitelist, sourcePath, bindingCache, shouldPrintDebug);
|
|
224
|
+
const attemptEval = !evaluateVars ? evaluateAstNode : createEvaluator({
|
|
225
|
+
tamaguiConfig,
|
|
226
|
+
staticNamespace,
|
|
227
|
+
sourcePath,
|
|
228
|
+
shouldPrintDebug
|
|
229
|
+
});
|
|
230
|
+
const attemptEvalSafe = createSafeEvaluator(attemptEval);
|
|
231
|
+
for (const property of definition.properties) {
|
|
232
|
+
if (!t.isObjectProperty(property) || !t.isIdentifier(property.key) || skipProps[property.key.name] || !isValidStyleKey(property.key.name, Component.staticConfig) || componentSkipProps.has(property.key.name)) {
|
|
233
|
+
skipped.push(property);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const out2 = attemptEvalSafe(property.value);
|
|
237
|
+
if (out2 === FAILED_EVAL) {
|
|
238
|
+
skipped.push(property);
|
|
239
|
+
} else {
|
|
240
|
+
styles[property.key.name] = out2;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const out = getSplitStyles(styles, Component.staticConfig, defaultTheme, {
|
|
244
|
+
focus: false,
|
|
245
|
+
hover: false,
|
|
246
|
+
mounted: false,
|
|
247
|
+
press: false,
|
|
248
|
+
pressIn: false,
|
|
249
|
+
resolveVariablesAs: "variable"
|
|
250
|
+
});
|
|
251
|
+
const classNames = {
|
|
252
|
+
...out.classNames
|
|
253
|
+
};
|
|
254
|
+
const atomics = getStylesAtomic(out.style);
|
|
255
|
+
for (const atomic of atomics) {
|
|
256
|
+
for (const rule of atomic.rules) {
|
|
257
|
+
out.rulesToInsert.push([atomic.identifier, rule]);
|
|
258
|
+
}
|
|
259
|
+
classNames[atomic.property] = atomic.identifier;
|
|
260
|
+
}
|
|
261
|
+
definition.properties = skipped;
|
|
262
|
+
for (const cn in classNames) {
|
|
263
|
+
const val = classNames[cn];
|
|
264
|
+
definition.properties.push(t.objectProperty(t.stringLiteral(cn), t.stringLiteral(val)));
|
|
265
|
+
}
|
|
266
|
+
for (const [identifier, rule] of out.rulesToInsert) {
|
|
267
|
+
onStyleRule == null ? void 0 : onStyleRule(identifier, [rule]);
|
|
268
|
+
}
|
|
269
|
+
res.styled++;
|
|
270
|
+
if (shouldPrintDebug) {
|
|
271
|
+
console.log(`Extracted styled(${name}) props:`, styles);
|
|
272
|
+
}
|
|
273
|
+
},
|
|
173
274
|
JSXElement(traversePath) {
|
|
174
|
-
var _a, _b, _c, _d, _e;
|
|
275
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
175
276
|
tm.mark("jsx-element", shouldPrintDebug === "verbose");
|
|
176
277
|
const node = traversePath.node.openingElement;
|
|
177
278
|
const ogAttributes = node.attributes;
|
|
@@ -213,10 +314,7 @@ function createExtractor() {
|
|
|
213
314
|
shouldPrintDebug = debugPropValue;
|
|
214
315
|
}
|
|
215
316
|
try {
|
|
216
|
-
let
|
|
217
|
-
var _a2;
|
|
218
|
-
return !!(!!validStyles[name] || !!pseudos[name] || ((_a2 = staticConfig.variants) == null ? void 0 : _a2[name]) || tamaguiConfig.shorthands[name] || (name[0] === "$" ? !!mediaQueryConfig[name.slice(1)] : false));
|
|
219
|
-
}, evaluateAttribute = function(path) {
|
|
317
|
+
let evaluateAttribute = function(path) {
|
|
220
318
|
const attribute = path.node;
|
|
221
319
|
const attr = { type: "attr", value: attribute };
|
|
222
320
|
if (t.isJSXSpreadAttribute(attribute)) {
|
|
@@ -339,36 +437,35 @@ function createExtractor() {
|
|
|
339
437
|
return attr;
|
|
340
438
|
}
|
|
341
439
|
const styleValue = attemptEvalSafe(value);
|
|
342
|
-
if (!isValidStyleKey(name)) {
|
|
440
|
+
if (!variants[name] && !isValidStyleKey(name, staticConfig)) {
|
|
343
441
|
let keys = [name];
|
|
344
442
|
let out = null;
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
if (out) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
out = Object.fromEntries(out);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
if (out) {
|
|
357
|
-
if (isTargetingHTML) {
|
|
358
|
-
out = rnw.createDOMProps(isTextView ? "span" : "div", out);
|
|
359
|
-
delete out.className;
|
|
360
|
-
}
|
|
443
|
+
out = staticConfig.propMapper(name, styleValue, defaultTheme, staticConfig.defaultProps, { resolveVariablesAs: "auto" }, void 0, shouldPrintDebug);
|
|
444
|
+
if (out) {
|
|
445
|
+
if (!Array.isArray(out)) {
|
|
446
|
+
console.warn(`Error expected array but got`, out);
|
|
447
|
+
couldntParse = true;
|
|
448
|
+
shouldDeopt = true;
|
|
449
|
+
} else {
|
|
450
|
+
out = Object.fromEntries(out);
|
|
361
451
|
keys = Object.keys(out);
|
|
362
452
|
}
|
|
363
453
|
}
|
|
454
|
+
if (out) {
|
|
455
|
+
if (isTargetingHTML) {
|
|
456
|
+
out = rnw.createDOMProps(isTextView ? "span" : "div", out);
|
|
457
|
+
delete out.className;
|
|
458
|
+
}
|
|
459
|
+
keys = Object.keys(out);
|
|
460
|
+
}
|
|
364
461
|
let didInline = false;
|
|
365
462
|
const attributes = keys.map((key) => {
|
|
366
463
|
const val = out[key];
|
|
367
|
-
if (isValidStyleKey(key)) {
|
|
464
|
+
if (isValidStyleKey(key, staticConfig)) {
|
|
368
465
|
return {
|
|
369
466
|
type: "style",
|
|
370
|
-
value: { [
|
|
371
|
-
name,
|
|
467
|
+
value: { [key]: styleValue },
|
|
468
|
+
name: key,
|
|
372
469
|
attr: path.node
|
|
373
470
|
};
|
|
374
471
|
}
|
|
@@ -394,7 +491,7 @@ function createExtractor() {
|
|
|
394
491
|
if (inlineWhenUnflattened.has(name)) {
|
|
395
492
|
inlineWhenUnflattenedOGVals[name] = { styleValue, attr };
|
|
396
493
|
}
|
|
397
|
-
if (isValidStyleKey(name)) {
|
|
494
|
+
if (isValidStyleKey(name, staticConfig)) {
|
|
398
495
|
if (shouldPrintDebug) {
|
|
399
496
|
console.log(` style: ${name} =`, styleValue);
|
|
400
497
|
}
|
|
@@ -411,6 +508,9 @@ function createExtractor() {
|
|
|
411
508
|
attr: path.node
|
|
412
509
|
};
|
|
413
510
|
} else {
|
|
511
|
+
if (variants[name]) {
|
|
512
|
+
variantValues.set(name, styleValue);
|
|
513
|
+
}
|
|
414
514
|
inlined.set(name, true);
|
|
415
515
|
return attr;
|
|
416
516
|
}
|
|
@@ -535,7 +635,7 @@ function createExtractor() {
|
|
|
535
635
|
return false;
|
|
536
636
|
}
|
|
537
637
|
const propName = prop.key["name"];
|
|
538
|
-
if (!isValidStyleKey(propName) && propName !== "tag") {
|
|
638
|
+
if (!isValidStyleKey(propName, staticConfig) && propName !== "tag") {
|
|
539
639
|
if (shouldPrintDebug) {
|
|
540
640
|
console.log(" not a valid style prop!", propName);
|
|
541
641
|
}
|
|
@@ -638,7 +738,6 @@ function createExtractor() {
|
|
|
638
738
|
}
|
|
639
739
|
}
|
|
640
740
|
};
|
|
641
|
-
__name(isValidStyleKey, "isValidStyleKey");
|
|
642
741
|
__name(evaluateAttribute, "evaluateAttribute");
|
|
643
742
|
__name(isExtractable, "isExtractable");
|
|
644
743
|
__name(createTernariesFromObjectProperties, "createTernariesFromObjectProperties");
|
|
@@ -656,14 +755,16 @@ function createExtractor() {
|
|
|
656
755
|
}
|
|
657
756
|
const shouldLog = !hasLogged;
|
|
658
757
|
if (shouldLog) {
|
|
758
|
+
console.log(` 1\uFE0F\u20E3 Inline optimized 2\uFE0F\u20E3 Inline flattened 3\uFE0F\u20E3 styled() extracted`);
|
|
659
759
|
const prefix = " |";
|
|
660
|
-
console.log(prefixLogs || prefix, "
|
|
760
|
+
console.log(prefixLogs || prefix, " total \xB7 1\uFE0F\u20E3 \xB7 2\uFE0F\u20E3 \xB7 3\uFE0F\u20E3");
|
|
661
761
|
hasLogged = true;
|
|
662
762
|
}
|
|
663
763
|
if (disableExtraction) {
|
|
664
764
|
return;
|
|
665
765
|
}
|
|
666
766
|
const { staticConfig } = component;
|
|
767
|
+
const variants = staticConfig.variants || {};
|
|
667
768
|
const isTextView = staticConfig.isText || false;
|
|
668
769
|
const validStyles = (_a = staticConfig == null ? void 0 : staticConfig.validStyles) != null ? _a : {};
|
|
669
770
|
let tagName = (_c = (_b = staticConfig.defaultProps) == null ? void 0 : _b.tag) != null ? _c : isTextView ? "span" : "div";
|
|
@@ -746,26 +847,10 @@ function createExtractor() {
|
|
|
746
847
|
}
|
|
747
848
|
tm.mark("jsx-element-flattened", shouldPrintDebug === "verbose");
|
|
748
849
|
node.attributes = flattenedAttrs;
|
|
749
|
-
if (staticConfig.defaultProps) {
|
|
750
|
-
for (const key in staticConfig.defaultProps) {
|
|
751
|
-
if (isValidStyleKey(key)) {
|
|
752
|
-
continue;
|
|
753
|
-
}
|
|
754
|
-
const serialize = require("babel-literal-to-ast");
|
|
755
|
-
const val = staticConfig.defaultProps[key];
|
|
756
|
-
try {
|
|
757
|
-
const serializedDefaultProp = serialize(val);
|
|
758
|
-
node.attributes.unshift(t.jsxAttribute(t.jsxIdentifier(key), typeof val === "string" ? t.stringLiteral(val) : t.jsxExpressionContainer(serializedDefaultProp)));
|
|
759
|
-
} catch (err) {
|
|
760
|
-
console.warn(`\u26A0\uFE0F Error evaluating default prop for component ${node.name.name}, prop ${key}
|
|
761
|
-
error: ${err}
|
|
762
|
-
value:`, val, "\n defaultProps:", staticConfig.defaultProps);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
850
|
let attrs = [];
|
|
767
851
|
let shouldDeopt = false;
|
|
768
852
|
const inlined = /* @__PURE__ */ new Map();
|
|
853
|
+
const variantValues = /* @__PURE__ */ new Map();
|
|
769
854
|
let hasSetOptimized = false;
|
|
770
855
|
const inlineWhenUnflattenedOGVals = {};
|
|
771
856
|
attrs = traversePath.get("openingElement").get("attributes").flatMap((path) => {
|
|
@@ -839,10 +924,14 @@ function createExtractor() {
|
|
|
839
924
|
const hasOnlyStringChildren = !hasSpread && (node.selfClosing || traversePath.node.children && traversePath.node.children.every((x) => x.type === "JSXText"));
|
|
840
925
|
let themeVal = inlined.get("theme");
|
|
841
926
|
inlined.delete("theme");
|
|
842
|
-
const
|
|
843
|
-
|
|
844
|
-
|
|
927
|
+
for (const [key] of [...inlined]) {
|
|
928
|
+
if (INLINE_EXTRACTABLE[key] || ((_d = staticConfig.variants) == null ? void 0 : _d[key])) {
|
|
929
|
+
inlined.delete(key);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
const canFlattenProps = inlined.size === 0;
|
|
845
933
|
let shouldFlatten = !shouldDeopt && canFlattenProps && !hasSpread && staticConfig.neverFlatten !== true && (staticConfig.neverFlatten === "jsx" ? hasOnlyStringChildren : true);
|
|
934
|
+
const shouldWrapTheme = shouldFlatten && themeVal;
|
|
846
935
|
if (disableExtractVariables) {
|
|
847
936
|
themeAccessListeners.add((key) => {
|
|
848
937
|
shouldFlatten = false;
|
|
@@ -852,11 +941,11 @@ function createExtractor() {
|
|
|
852
941
|
});
|
|
853
942
|
}
|
|
854
943
|
if (shouldPrintDebug) {
|
|
855
|
-
console.log(" - flatten?", objToStr({ hasSpread, shouldDeopt, shouldFlatten, canFlattenProps,
|
|
944
|
+
console.log(" - flatten?", objToStr({ hasSpread, shouldDeopt, shouldFlatten, canFlattenProps, shouldWrapTheme, hasOnlyStringChildren }), "inlined", [...inlined]);
|
|
856
945
|
}
|
|
857
|
-
if (shouldFlatten &&
|
|
946
|
+
if (shouldFlatten && shouldWrapTheme) {
|
|
858
947
|
if (shouldPrintDebug) {
|
|
859
|
-
console.log(" - wrapping theme",
|
|
948
|
+
console.log(" - wrapping theme", themeVal);
|
|
860
949
|
}
|
|
861
950
|
attrs = attrs.filter((x) => x.type === "attr" && t.isJSXAttribute(x.value) && x.value.name.name === "theme" ? false : true);
|
|
862
951
|
if (!hasImportedTheme) {
|
|
@@ -869,7 +958,7 @@ function createExtractor() {
|
|
|
869
958
|
}
|
|
870
959
|
if (shouldFlatten && staticConfig.defaultProps) {
|
|
871
960
|
const defaultStyleAttrs = Object.keys(staticConfig.defaultProps).flatMap((key) => {
|
|
872
|
-
if (!isValidStyleKey(key)) {
|
|
961
|
+
if (!isValidStyleKey(key, staticConfig)) {
|
|
873
962
|
return [];
|
|
874
963
|
}
|
|
875
964
|
const value = staticConfig.defaultProps[key];
|
|
@@ -899,10 +988,10 @@ function createExtractor() {
|
|
|
899
988
|
for (const cur of attrs) {
|
|
900
989
|
if (cur.type === "style") {
|
|
901
990
|
for (const key in cur.value) {
|
|
902
|
-
const shouldEnsureOverridden = !!((
|
|
991
|
+
const shouldEnsureOverridden = !!((_e = staticConfig.ensureOverriddenProp) == null ? void 0 : _e[key]);
|
|
903
992
|
const isSetInAttrsAlready = attrs.some((x) => x.type === "attr" && x.value.type === "JSXAttribute" && x.value.name.name === key);
|
|
904
993
|
if (!isSetInAttrsAlready) {
|
|
905
|
-
const isVariant = !!((
|
|
994
|
+
const isVariant = !!((_f = staticConfig.variants) == null ? void 0 : _f[cur.name || ""]);
|
|
906
995
|
if (isVariant || shouldEnsureOverridden) {
|
|
907
996
|
ensureOverridden[key] = true;
|
|
908
997
|
}
|
|
@@ -915,13 +1004,88 @@ function createExtractor() {
|
|
|
915
1004
|
console.log(" - attrs (flattened): \n", logLines(attrs.map(attrStr).join(", ")));
|
|
916
1005
|
console.log(" - ensureOverriden:", Object.keys(ensureOverridden).join(", "));
|
|
917
1006
|
}
|
|
1007
|
+
const state = {
|
|
1008
|
+
noClassNames: false,
|
|
1009
|
+
focus: false,
|
|
1010
|
+
hover: false,
|
|
1011
|
+
mounted: true,
|
|
1012
|
+
press: false,
|
|
1013
|
+
pressIn: false
|
|
1014
|
+
};
|
|
1015
|
+
let foundStaticProps = {};
|
|
1016
|
+
for (const key in attrs) {
|
|
1017
|
+
const cur = attrs[key];
|
|
1018
|
+
if (cur.type === "style") {
|
|
1019
|
+
normalizeStyleObject(cur.value);
|
|
1020
|
+
foundStaticProps = {
|
|
1021
|
+
...foundStaticProps,
|
|
1022
|
+
...expandStyles(cur.value)
|
|
1023
|
+
};
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
if (cur.type === "attr") {
|
|
1027
|
+
if (t.isJSXSpreadAttribute(cur.value)) {
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
if (!t.isJSXIdentifier(cur.value.name)) {
|
|
1031
|
+
continue;
|
|
1032
|
+
}
|
|
1033
|
+
const key2 = cur.value.name.name;
|
|
1034
|
+
const value = attemptEvalSafe(cur.value.value || t.booleanLiteral(true));
|
|
1035
|
+
if (value !== FAILED_EVAL) {
|
|
1036
|
+
foundStaticProps = {
|
|
1037
|
+
...foundStaticProps,
|
|
1038
|
+
[key2]: value
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
const completeProps = {};
|
|
1044
|
+
for (const key in staticConfig.defaultProps) {
|
|
1045
|
+
if (!(key in foundStaticProps)) {
|
|
1046
|
+
completeProps[key] = staticConfig.defaultProps[key];
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
for (const key in foundStaticProps) {
|
|
1050
|
+
completeProps[key] = foundStaticProps[key];
|
|
1051
|
+
}
|
|
918
1052
|
attrs = attrs.reduce((acc, cur) => {
|
|
919
1053
|
if (!cur)
|
|
920
1054
|
return acc;
|
|
921
1055
|
if (cur.type === "attr" && !t.isJSXSpreadAttribute(cur.value)) {
|
|
922
1056
|
if (shouldFlatten) {
|
|
923
|
-
|
|
924
|
-
|
|
1057
|
+
const name = cur.value.name.name;
|
|
1058
|
+
if (typeof name === "string") {
|
|
1059
|
+
if (name === "tag") {
|
|
1060
|
+
return acc;
|
|
1061
|
+
}
|
|
1062
|
+
if (variants[name] && variantValues.has(name)) {
|
|
1063
|
+
let out = Object.fromEntries(staticConfig.propMapper(name, variantValues.get(name), defaultTheme, completeProps, { ...state, resolveVariablesAs: "auto" }, void 0, shouldPrintDebug) || []);
|
|
1064
|
+
if (out && isTargetingHTML) {
|
|
1065
|
+
const cn = out.className;
|
|
1066
|
+
out = rnw.createDOMProps(isTextView ? "span" : "div", out);
|
|
1067
|
+
out.className = cn;
|
|
1068
|
+
}
|
|
1069
|
+
if (shouldPrintDebug) {
|
|
1070
|
+
console.log(" - expanded variant", name, out);
|
|
1071
|
+
}
|
|
1072
|
+
for (const key2 in out) {
|
|
1073
|
+
const value2 = out[key2];
|
|
1074
|
+
if (isValidStyleKey(key2, staticConfig)) {
|
|
1075
|
+
acc.push({
|
|
1076
|
+
type: "style",
|
|
1077
|
+
value: { [key2]: value2 },
|
|
1078
|
+
name: key2,
|
|
1079
|
+
attr: cur.value
|
|
1080
|
+
});
|
|
1081
|
+
} else {
|
|
1082
|
+
acc.push({
|
|
1083
|
+
type: "attr",
|
|
1084
|
+
value: t.jsxAttribute(t.jsxIdentifier(key2), t.jsxExpressionContainer(typeof value2 === "string" ? t.stringLiteral(value2) : literalToAst(value2)))
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
925
1089
|
}
|
|
926
1090
|
}
|
|
927
1091
|
}
|
|
@@ -988,51 +1152,6 @@ function createExtractor() {
|
|
|
988
1152
|
acc.push(cur);
|
|
989
1153
|
return acc;
|
|
990
1154
|
}, []);
|
|
991
|
-
const state = {
|
|
992
|
-
noClassNames: true,
|
|
993
|
-
focus: false,
|
|
994
|
-
hover: false,
|
|
995
|
-
mounted: true,
|
|
996
|
-
press: false,
|
|
997
|
-
pressIn: false
|
|
998
|
-
};
|
|
999
|
-
let foundStaticProps = {};
|
|
1000
|
-
for (const key in attrs) {
|
|
1001
|
-
const cur = attrs[key];
|
|
1002
|
-
if (cur.type === "style") {
|
|
1003
|
-
normalizeStyleObject(cur.value);
|
|
1004
|
-
foundStaticProps = {
|
|
1005
|
-
...foundStaticProps,
|
|
1006
|
-
...expandStyles(cur.value)
|
|
1007
|
-
};
|
|
1008
|
-
continue;
|
|
1009
|
-
}
|
|
1010
|
-
if (cur.type === "attr") {
|
|
1011
|
-
if (t.isJSXSpreadAttribute(cur.value)) {
|
|
1012
|
-
continue;
|
|
1013
|
-
}
|
|
1014
|
-
if (!t.isJSXIdentifier(cur.value.name)) {
|
|
1015
|
-
continue;
|
|
1016
|
-
}
|
|
1017
|
-
const key2 = cur.value.name.name;
|
|
1018
|
-
const value = attemptEvalSafe(cur.value.value || t.booleanLiteral(true));
|
|
1019
|
-
if (value !== FAILED_EVAL) {
|
|
1020
|
-
foundStaticProps = {
|
|
1021
|
-
...foundStaticProps,
|
|
1022
|
-
[key2]: value
|
|
1023
|
-
};
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
const completeProps = {};
|
|
1028
|
-
for (const key in staticConfig.defaultProps) {
|
|
1029
|
-
if (!(key in foundStaticProps)) {
|
|
1030
|
-
completeProps[key] = staticConfig.defaultProps[key];
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
for (const key in foundStaticProps) {
|
|
1034
|
-
completeProps[key] = foundStaticProps[key];
|
|
1035
|
-
}
|
|
1036
1155
|
if (shouldPrintDebug) {
|
|
1037
1156
|
console.log(" - attrs (combined \u{1F500}): \n", logLines(attrs.map(attrStr).join(", ")));
|
|
1038
1157
|
console.log(" - defaultProps: \n", logLines(objToStr(staticConfig.defaultProps)));
|
|
@@ -1063,15 +1182,6 @@ function createExtractor() {
|
|
|
1063
1182
|
...out.style,
|
|
1064
1183
|
...out.pseudos
|
|
1065
1184
|
};
|
|
1066
|
-
omitInvalidStyles(outStyle);
|
|
1067
|
-
if (shouldPrintDebug) {
|
|
1068
|
-
console.log(` getStyles ${debugName} (props):
|
|
1069
|
-
`, logLines(objToStr(props2)));
|
|
1070
|
-
console.log(` getStyles ${debugName} (out.viewProps):
|
|
1071
|
-
`, logLines(objToStr(out.viewProps)));
|
|
1072
|
-
console.log(` getStyles ${debugName} (out.style):
|
|
1073
|
-
`, logLines(objToStr(outStyle || {}), true));
|
|
1074
|
-
}
|
|
1075
1185
|
return outStyle;
|
|
1076
1186
|
} catch (err) {
|
|
1077
1187
|
console.log("error", err.message, err.stack);
|
|
@@ -1097,8 +1207,8 @@ function createExtractor() {
|
|
|
1097
1207
|
}
|
|
1098
1208
|
}
|
|
1099
1209
|
if (shouldPrintDebug) {
|
|
1100
|
-
|
|
1101
|
-
|
|
1210
|
+
if (shouldFlatten)
|
|
1211
|
+
console.log(" -- addInitialStyleKeys", addInitialStyleKeys.join(", "));
|
|
1102
1212
|
}
|
|
1103
1213
|
let getStyleError = null;
|
|
1104
1214
|
for (const attr of attrs) {
|
|
@@ -1115,14 +1225,14 @@ function createExtractor() {
|
|
|
1115
1225
|
console.log(" => tern ", attrStr(attr));
|
|
1116
1226
|
continue;
|
|
1117
1227
|
case "style":
|
|
1118
|
-
if (shouldPrintDebug)
|
|
1119
|
-
console.log(" * styles in", attr.value);
|
|
1120
1228
|
const styles = getStyles(attr.value, "style");
|
|
1121
|
-
if (shouldPrintDebug)
|
|
1122
|
-
console.log(" * styles out", logLines(objToStr(styles)));
|
|
1123
1229
|
if (styles) {
|
|
1124
|
-
attr.value =
|
|
1230
|
+
attr.value = styles;
|
|
1125
1231
|
}
|
|
1232
|
+
if (shouldPrintDebug)
|
|
1233
|
+
console.log(" * styles (in)", logLines(objToStr(attr.value)));
|
|
1234
|
+
if (shouldPrintDebug)
|
|
1235
|
+
console.log(" * styles (out)", logLines(objToStr(styles)));
|
|
1126
1236
|
continue;
|
|
1127
1237
|
}
|
|
1128
1238
|
} catch (err) {
|
|
@@ -1141,7 +1251,7 @@ function createExtractor() {
|
|
|
1141
1251
|
const existingStyleKeys = /* @__PURE__ */ new Set();
|
|
1142
1252
|
for (let i = attrs.length - 1; i >= 0; i--) {
|
|
1143
1253
|
const attr = attrs[i];
|
|
1144
|
-
if (shouldFlatten
|
|
1254
|
+
if (shouldFlatten) {
|
|
1145
1255
|
if (attr.type === "attr") {
|
|
1146
1256
|
if (t.isJSXAttribute(attr.value)) {
|
|
1147
1257
|
if (t.isJSXIdentifier(attr.value.name)) {
|
|
@@ -1149,6 +1259,9 @@ function createExtractor() {
|
|
|
1149
1259
|
if (INLINE_EXTRACTABLE[name]) {
|
|
1150
1260
|
attr.value.name.name = INLINE_EXTRACTABLE[name];
|
|
1151
1261
|
}
|
|
1262
|
+
if ((_g = staticConfig.variants) == null ? void 0 : _g[name]) {
|
|
1263
|
+
const expanded = getStyles({});
|
|
1264
|
+
}
|
|
1152
1265
|
}
|
|
1153
1266
|
}
|
|
1154
1267
|
}
|
|
@@ -16,7 +16,6 @@ import { isSimpleSpread } from "./extractHelpers";
|
|
|
16
16
|
import { extractMediaStyle } from "./extractMediaStyle";
|
|
17
17
|
import { getPrefixLogs } from "./getPrefixLogs";
|
|
18
18
|
import { hoistClassNames } from "./hoistClassNames";
|
|
19
|
-
import { literalToAst } from "./literalToAst";
|
|
20
19
|
import { logLines } from "./logLines";
|
|
21
20
|
import { timer } from "./timer";
|
|
22
21
|
const mergeStyleGroups = {
|
|
@@ -61,6 +60,10 @@ function extractToClassNames({
|
|
|
61
60
|
shouldPrintDebug,
|
|
62
61
|
...options,
|
|
63
62
|
target: "html",
|
|
63
|
+
extractStyledDefinitions: true,
|
|
64
|
+
onStyleRule(identifier, rules) {
|
|
65
|
+
cssMap.set(`.${identifier}`, { css: rules.join(";"), commentTexts: [] });
|
|
66
|
+
},
|
|
64
67
|
getFlattenedNode: ({ tag }) => {
|
|
65
68
|
hasFlattened = true;
|
|
66
69
|
return tag;
|
|
@@ -77,6 +80,8 @@ function extractToClassNames({
|
|
|
77
80
|
isFlattened
|
|
78
81
|
}) => {
|
|
79
82
|
var _a2, _b;
|
|
83
|
+
const didFlattenThisTag = hasFlattened;
|
|
84
|
+
hasFlattened = false;
|
|
80
85
|
let finalClassNames = [];
|
|
81
86
|
let finalAttrs = [];
|
|
82
87
|
let finalStyles = [];
|
|
@@ -117,26 +122,13 @@ function extractToClassNames({
|
|
|
117
122
|
switch (attr.type) {
|
|
118
123
|
case "style":
|
|
119
124
|
if (!isFlattened) {
|
|
120
|
-
const { hoverStyle, pressStyle, focusStyle } = attr.value;
|
|
121
|
-
const pseudos = [
|
|
122
|
-
["hoverStyle", hoverStyle],
|
|
123
|
-
["pressStyle", pressStyle],
|
|
124
|
-
["focusStyle", focusStyle]
|
|
125
|
-
];
|
|
126
125
|
const styles2 = getStylesAtomic(attr.value, {
|
|
127
126
|
splitTransforms: true
|
|
128
127
|
});
|
|
129
128
|
finalStyles = [...finalStyles, ...styles2];
|
|
130
|
-
for (const [key, value] of pseudos) {
|
|
131
|
-
if (value && Object.keys(value).length) {
|
|
132
|
-
finalAttrs.push(t.jsxAttribute(t.jsxIdentifier(key), t.jsxExpressionContainer(literalToAst(value))));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
129
|
for (const style of styles2) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
finalAttrs.push(t.jsxAttribute(t.jsxIdentifier(style.property), t.stringLiteral(style.identifier)));
|
|
130
|
+
const prop = style.pseudo ? `${style.property}-${style.pseudo}` : style.property;
|
|
131
|
+
finalAttrs.push(t.jsxAttribute(t.jsxIdentifier(prop), t.stringLiteral(style.identifier)));
|
|
140
132
|
}
|
|
141
133
|
} else {
|
|
142
134
|
const styles2 = addStyles(attr.value);
|
|
@@ -217,7 +209,7 @@ function extractToClassNames({
|
|
|
217
209
|
const nameExpr = names ? hoistClassNames(jsxPath, existingHoists, names) : null;
|
|
218
210
|
let expr = nameExpr;
|
|
219
211
|
if (nameExpr && !t.isIdentifier(nameExpr)) {
|
|
220
|
-
if (!
|
|
212
|
+
if (!didFlattenThisTag) {
|
|
221
213
|
} else {
|
|
222
214
|
ensureImportingConcat(programPath);
|
|
223
215
|
const simpleSpreads = attrs.filter((x) => t.isJSXSpreadAttribute(x.value) && isSimpleSpread(x.value));
|
|
@@ -230,7 +222,8 @@ function extractToClassNames({
|
|
|
230
222
|
node.attributes.push(t.jsxAttribute(t.jsxIdentifier("className"), t.jsxExpressionContainer(expr)));
|
|
231
223
|
}
|
|
232
224
|
const comment = util.format("/* %s:%s (%s) */", filePath, lineNumbers, originalNodeName);
|
|
233
|
-
for (const {
|
|
225
|
+
for (const { identifier, rules } of finalStyles) {
|
|
226
|
+
const className = `.${identifier}`;
|
|
234
227
|
if (cssMap.has(className)) {
|
|
235
228
|
if (comment) {
|
|
236
229
|
const val = cssMap.get(className);
|
|
@@ -250,7 +243,7 @@ function extractToClassNames({
|
|
|
250
243
|
}
|
|
251
244
|
}
|
|
252
245
|
});
|
|
253
|
-
if (!res || !res.modified && !res.optimized && !res.flattened) {
|
|
246
|
+
if (!res || !res.modified && !res.optimized && !res.flattened && !res.styled) {
|
|
254
247
|
if (shouldPrintDebug) {
|
|
255
248
|
console.log("no res or none modified", res);
|
|
256
249
|
}
|
|
@@ -277,6 +270,7 @@ function extractToClassNames({
|
|
|
277
270
|
if (shouldLogTiming) {
|
|
278
271
|
const memUsed = mem ? Math.round((process.memoryUsage().heapUsed - mem.heapUsed) / 1024 / 1204 * 10) / 10 : 0;
|
|
279
272
|
const path2 = basename(sourcePath).replace(/\.[jt]sx?$/, "").slice(0, 22).trim().padStart(24);
|
|
273
|
+
const numStyled = `${res.styled}`.padStart(3);
|
|
280
274
|
const numOptimized = `${res.optimized}`.padStart(3);
|
|
281
275
|
const numFound = `${res.found}`.padStart(3);
|
|
282
276
|
const numFlattened = `${res.flattened}`.padStart(3);
|
|
@@ -284,7 +278,9 @@ function extractToClassNames({
|
|
|
284
278
|
const timing = Date.now() - start;
|
|
285
279
|
const timingWarning = timing > 50 ? "\u26A0\uFE0F" : timing > 150 ? "\u2622\uFE0F" : "";
|
|
286
280
|
const timingStr = `${timing}ms${timingWarning}`.padStart(6);
|
|
287
|
-
|
|
281
|
+
const pre = getPrefixLogs(options);
|
|
282
|
+
const memStr = memory ? `(${memory})` : "";
|
|
283
|
+
console.log(`${pre} ${path2} ${numFound} \xB7 ${numOptimized} \xB7 ${numFlattened} \xB7 ${numStyled} ${timingStr} ${memStr}`);
|
|
288
284
|
}
|
|
289
285
|
return {
|
|
290
286
|
ast,
|