@hanzogui/static 3.0.5 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/check-dep-versions.cjs +201 -96
  2. package/dist/checkDeps.cjs +163 -92
  3. package/dist/constants.cjs +32 -30
  4. package/dist/exports.cjs +15 -13
  5. package/dist/extractor/accessSafe.cjs +25 -23
  6. package/dist/extractor/babelParse.cjs +30 -28
  7. package/dist/extractor/buildClassName.cjs +59 -35
  8. package/dist/extractor/bundle.cjs +159 -104
  9. package/dist/extractor/bundleConfig.cjs +463 -263
  10. package/dist/extractor/concatClassName.cjs +41 -29
  11. package/dist/extractor/createEvaluator.cjs +54 -41
  12. package/dist/extractor/createExtractor.cjs +1338 -661
  13. package/dist/extractor/createLogger.cjs +30 -25
  14. package/dist/extractor/detectModuleFormat.cjs +22 -16
  15. package/dist/extractor/ensureImportingConcat.cjs +31 -25
  16. package/dist/extractor/errors.cjs +12 -10
  17. package/dist/extractor/esbuildAliasPlugin.cjs +28 -16
  18. package/dist/extractor/esbuildTsconfigPaths.cjs +58 -36
  19. package/dist/extractor/evaluateAstNode.cjs +104 -59
  20. package/dist/extractor/extractHelpers.cjs +130 -67
  21. package/dist/extractor/extractMediaStyle.cjs +110 -69
  22. package/dist/extractor/extractToClassNames.cjs +336 -230
  23. package/dist/extractor/extractToNative.cjs +244 -149
  24. package/dist/extractor/findTopmostFunction.cjs +22 -13
  25. package/dist/extractor/generatedUid.cjs +39 -28
  26. package/dist/extractor/getGuiConfigPathFromOptionsConfig.cjs +20 -14
  27. package/dist/extractor/getPrefixLogs.cjs +12 -10
  28. package/dist/extractor/getPropValueFromAttributes.cjs +52 -34
  29. package/dist/extractor/getSourceModule.cjs +73 -37
  30. package/dist/extractor/getStaticBindingsForScope.cjs +131 -68
  31. package/dist/extractor/hoistClassNames.cjs +46 -32
  32. package/dist/extractor/literalToAst.cjs +67 -42
  33. package/dist/extractor/loadFile.cjs +9 -3
  34. package/dist/extractor/loadGui.cjs +198 -109
  35. package/dist/extractor/logLines.cjs +27 -19
  36. package/dist/extractor/normalizeTernaries.cjs +66 -44
  37. package/dist/extractor/propsToFontFamilyCache.cjs +15 -11
  38. package/dist/extractor/regenerateConfig.cjs +109 -81
  39. package/dist/extractor/removeUnusedHooks.cjs +73 -41
  40. package/dist/extractor/timer.cjs +23 -14
  41. package/dist/extractor/validHTMLAttributes.cjs +61 -59
  42. package/dist/extractor/watchGuiConfig.cjs +35 -23
  43. package/dist/getPragmaOptions.cjs +34 -19
  44. package/dist/helpers/memoize.cjs +24 -16
  45. package/dist/helpers/requireGuiCore.cjs +22 -15
  46. package/dist/index.cjs +26 -24
  47. package/dist/registerRequire.cjs +132 -77
  48. package/dist/server.cjs +35 -28
  49. package/dist/types.cjs +7 -5
  50. package/dist/worker.cjs +62 -40
  51. package/package.json +15 -15
  52. package/src/checkDeps.ts +1 -1
  53. package/src/extractor/bundle.ts +1 -1
  54. package/src/extractor/bundleConfig.ts +5 -5
  55. package/src/extractor/createExtractor.ts +28 -13
  56. package/src/extractor/createLogger.ts +1 -3
  57. package/src/extractor/extractToNative.ts +35 -9
  58. package/src/extractor/loadGui.ts +4 -4
  59. package/src/extractor/regenerateConfig.ts +2 -2
  60. package/src/getPragmaOptions.ts +2 -2
  61. package/src/helpers/requireGuiCore.ts +6 -6
  62. package/src/registerRequire.ts +4 -4
  63. package/src/worker.ts +1 -1
  64. package/types/extractor/createExtractor.d.ts.map +1 -1
  65. package/types/extractor/createLogger.d.ts.map +1 -1
  66. package/types/extractor/extractToNative.d.ts.map +1 -1
  67. package/types/worker.d.ts +1 -1
@@ -2,54 +2,56 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var extractToClassNames_exports = {};
33
35
  __export(extractToClassNames_exports, {
34
36
  extractToClassNames: () => extractToClassNames
35
37
  });
36
38
  module.exports = __toCommonJS(extractToClassNames_exports);
37
- var import_generator = __toESM(require("@babel/generator")),
38
- t = __toESM(require("@babel/types")),
39
- import_web = require("@hanzogui/web"),
40
- path = __toESM(require("node:path")),
41
- util = __toESM(require("node:util")),
42
- import_requireGuiCore = require("../helpers/requireGuiCore.cjs"),
43
- import_babelParse = require("./babelParse.cjs"),
44
- import_createLogger = require("./createLogger.cjs"),
45
- import_extractMediaStyle = require("./extractMediaStyle.cjs"),
46
- import_normalizeTernaries = require("./normalizeTernaries.cjs"),
47
- import_propsToFontFamilyCache = require("./propsToFontFamilyCache.cjs"),
48
- import_timer = require("./timer.cjs"),
49
- import_errors = require("./errors.cjs"),
50
- import_concatClassName = require("./concatClassName.cjs");
51
- const remove = () => {},
52
- spaceString = t.stringLiteral(" ");
39
+ var import_generator = __toESM(require("@babel/generator"));
40
+ var t = __toESM(require("@babel/types"));
41
+ var import_web = require("@hanzogui/web");
42
+ var path = __toESM(require("node:path"));
43
+ var util = __toESM(require("node:util"));
44
+ var import_requireGuiCore = require("../helpers/requireGuiCore.cjs");
45
+ var import_babelParse = require("./babelParse.cjs");
46
+ var import_createLogger = require("./createLogger.cjs");
47
+ var import_extractMediaStyle = require("./extractMediaStyle.cjs");
48
+ var import_normalizeTernaries = require("./normalizeTernaries.cjs");
49
+ var import_propsToFontFamilyCache = require("./propsToFontFamilyCache.cjs");
50
+ var import_timer = require("./timer.cjs");
51
+ var import_errors = require("./errors.cjs");
52
+ var import_concatClassName = require("./concatClassName.cjs");
53
+ const remove = () => {};
54
+ const spaceString = t.stringLiteral(" ");
53
55
  async function extractToClassNames({
54
56
  extractor,
55
57
  source,
@@ -57,160 +59,238 @@ async function extractToClassNames({
57
59
  options,
58
60
  shouldPrintDebug
59
61
  }) {
60
- const tm = (0, import_timer.timer)(),
61
- {
62
- getCSSStylesAtomic,
63
- createMediaStyle
64
- } = (0, import_requireGuiCore.requireGuiCore)("web");
65
- if (sourcePath.includes("node_modules")) return null;
66
- if (shouldPrintDebug && console.warn(`--- ${sourcePath} ---
62
+ const tm = (0, import_timer.timer)();
63
+ const {
64
+ getCSSStylesAtomic,
65
+ createMediaStyle
66
+ } = (0, import_requireGuiCore.requireGuiCore)("web");
67
+ if (sourcePath.includes("node_modules")) {
68
+ return null;
69
+ }
70
+ if (shouldPrintDebug) {
71
+ console.warn(`--- ${sourcePath} ---
67
72
 
68
- `), typeof source != "string") throw new Error("`source` must be a string of javascript");
69
- if (!path.isAbsolute(sourcePath)) throw new Error("`sourcePath` must be an absolute path to a .js file, got: " + sourcePath);
70
- /.[tj]sx?$/i.test(sourcePath || "") || console.warn(`${sourcePath.slice(0, 100)} - bad filename.`), !options.disableExtraction && !options._disableLoadGui && (await extractor.loadGui(options));
73
+ `);
74
+ }
75
+ if (typeof source !== "string") {
76
+ throw new Error("`source` must be a string of javascript");
77
+ }
78
+ if (!path.isAbsolute(sourcePath)) {
79
+ throw new Error("`sourcePath` must be an absolute path to a .js file, got: " + sourcePath);
80
+ }
81
+ if (!/.[tj]sx?$/i.test(sourcePath || "")) {
82
+ console.warn(`${sourcePath.slice(0, 100)} - bad filename.`);
83
+ }
84
+ if (!options.disableExtraction && !options["_disableLoadGui"]) {
85
+ await extractor.loadGui(options);
86
+ }
71
87
  const printLog = (0, import_createLogger.createLogger)(sourcePath, options);
72
88
  let ast;
73
89
  try {
74
90
  ast = (0, import_babelParse.babelParse)(source, sourcePath);
75
91
  } catch (err) {
76
- throw console.error("babel parse error:", sourcePath.slice(0, 100)), err;
92
+ console.error("babel parse error:", sourcePath.slice(0, 100));
93
+ throw err;
77
94
  }
78
- tm.mark("babel-parse", shouldPrintDebug === "verbose");
79
- const cssMap = /* @__PURE__ */new Map(),
80
- guiConfig = extractor.getGui(),
81
- res = await extractor.parse(ast, {
82
- shouldPrintDebug,
83
- ...options,
84
- platform: "web",
85
- sourcePath,
86
- extractStyledDefinitions: !0,
87
- onStyledDefinitionRule(identifier, rules) {
88
- const css = rules.join(`
89
- `);
90
- shouldPrintDebug && console.info(`adding styled() rule: .${identifier} ${css}`), cssMap.set(`.${identifier}`, {
91
- css,
92
- commentTexts: []
93
- });
94
- },
95
- getFlattenedNode: ({
96
- tag
97
- }) => tag,
98
- onExtractTag: ({
99
- parserProps,
100
- attrs,
101
- node,
102
- attemptEval,
103
- jsxPath,
104
- originalNodeName,
105
- filePath,
106
- lineNumbers,
107
- staticConfig
108
- }) => {
109
- if (staticConfig.acceptsClassName === !1) throw new import_errors.BailOptimizationError();
110
- const finalAttrs = [];
111
- let mergeForwardBaseStyle = null,
112
- attrClassName = null,
113
- baseFontFamily = "",
114
- mediaStylesSeen = 1;
115
- const comment = util.format("/* %s:%s (%s) */", filePath, lineNumbers, originalNodeName);
116
- function addStyle(style) {
117
- const identifier = style[import_web.StyleObjectIdentifier],
118
- rules = style[import_web.StyleObjectRules],
119
- selector = `.${identifier}`;
120
- return cssMap.has(selector) ? cssMap.get(selector).commentTexts.push(comment) : rules.length && cssMap.set(selector, {
121
- css: rules.join(`
122
- `),
95
+ tm.mark(`babel-parse`, shouldPrintDebug === "verbose");
96
+ const cssMap = /* @__PURE__ */new Map();
97
+ const guiConfig = extractor.getGui();
98
+ const res = await extractor.parse(ast, {
99
+ shouldPrintDebug,
100
+ ...options,
101
+ platform: "web",
102
+ sourcePath,
103
+ extractStyledDefinitions: true,
104
+ onStyledDefinitionRule(identifier, rules) {
105
+ const css = rules.join("\n");
106
+ if (shouldPrintDebug) {
107
+ console.info(`adding styled() rule: .${identifier} ${css}`);
108
+ }
109
+ cssMap.set(`.${identifier}`, {
110
+ css,
111
+ commentTexts: []
112
+ });
113
+ },
114
+ getFlattenedNode: ({
115
+ tag
116
+ }) => {
117
+ return tag;
118
+ },
119
+ onExtractTag: ({
120
+ parserProps,
121
+ attrs,
122
+ node,
123
+ attemptEval,
124
+ jsxPath,
125
+ originalNodeName,
126
+ filePath,
127
+ lineNumbers,
128
+ staticConfig
129
+ }) => {
130
+ if (staticConfig.acceptsClassName === false) {
131
+ throw new import_errors.BailOptimizationError();
132
+ }
133
+ const finalAttrs = [];
134
+ let mergeForwardBaseStyle = null;
135
+ let attrClassName = null;
136
+ let baseFontFamily = "";
137
+ let mediaStylesSeen = 1;
138
+ const comment = util.format("/* %s:%s (%s) */", filePath, lineNumbers, originalNodeName);
139
+ function addStyle(style) {
140
+ const identifier = style[import_web.StyleObjectIdentifier];
141
+ const rules = style[import_web.StyleObjectRules];
142
+ const selector = `.${identifier}`;
143
+ if (cssMap.has(selector)) {
144
+ const val = cssMap.get(selector);
145
+ val.commentTexts.push(comment);
146
+ } else if (rules.length) {
147
+ cssMap.set(selector, {
148
+ css: rules.join("\n"),
123
149
  commentTexts: [comment]
124
- }), identifier;
150
+ });
125
151
  }
126
- function addStyles(style) {
127
- const cssStyles = getCSSStylesAtomic(style),
128
- classNames = [];
129
- for (const style2 of cssStyles) {
130
- const mediaName = style2[0].slice(1);
131
- if (mediaName.startsWith("group-")) throw new import_errors.BailOptimizationError();
132
- const mediaTypeMatch = mediaName.match(/^(theme|platform)-/);
133
- if (mediaTypeMatch) {
134
- const mediaType = mediaTypeMatch[1],
135
- mediaStyle = createMediaStyle(style2, mediaName, extractor.getGui().media, mediaType, !1, mediaStylesSeen),
136
- identifier2 = addStyle(mediaStyle);
137
- classNames.push(identifier2);
138
- continue;
139
- }
140
- if (mediaName in guiConfig.media) {
141
- const mediaStyle = createMediaStyle(style2, mediaName, extractor.getGui().media, !0, !1, mediaStylesSeen),
142
- identifier2 = addStyle(mediaStyle);
143
- classNames.push(identifier2);
144
- continue;
145
- }
146
- const identifier = addStyle(style2);
147
- classNames.push(identifier);
152
+ return identifier;
153
+ }
154
+ function addStyles(style) {
155
+ const cssStyles = getCSSStylesAtomic(style);
156
+ const classNames = [];
157
+ for (const style2 of cssStyles) {
158
+ const property = style2[0];
159
+ const mediaName = property.slice(1);
160
+ if (mediaName.startsWith("group-")) {
161
+ throw new import_errors.BailOptimizationError();
162
+ }
163
+ const mediaTypeMatch = mediaName.match(/^(theme|platform)-/);
164
+ if (mediaTypeMatch) {
165
+ const mediaType = mediaTypeMatch[1];
166
+ const mediaStyle = createMediaStyle(style2, mediaName, extractor.getGui().media, mediaType, false, mediaStylesSeen);
167
+ const identifier2 = addStyle(mediaStyle);
168
+ classNames.push(identifier2);
169
+ continue;
170
+ }
171
+ if (mediaName in guiConfig.media) {
172
+ const mediaStyle = createMediaStyle(style2, mediaName, extractor.getGui().media, true, false, mediaStylesSeen);
173
+ const identifier2 = addStyle(mediaStyle);
174
+ classNames.push(identifier2);
175
+ continue;
148
176
  }
149
- return classNames;
177
+ const identifier = addStyle(style2);
178
+ classNames.push(identifier);
150
179
  }
151
- const onlyTernaries = attrs.flatMap(attr => {
152
- if (attr.type === "attr") {
153
- const value = attr.value;
154
- if (t.isJSXSpreadAttribute(value)) return console.error("Should never happen"), [];
155
- if (value.name.name === "className") {
156
- let inner = value.value;
157
- t.isJSXExpressionContainer(inner) && (inner = inner.expression);
158
- try {
159
- const evaluatedValue = inner ? attemptEval(inner) : null;
160
- typeof evaluatedValue == "string" && (attrClassName = t.stringLiteral(evaluatedValue));
161
- } catch {
162
- inner && (attrClassName ||= inner);
163
- }
164
- return [];
180
+ return classNames;
181
+ }
182
+ const onlyTernaries = attrs.flatMap(attr => {
183
+ if (attr.type === "attr") {
184
+ const value = attr.value;
185
+ if (t.isJSXSpreadAttribute(value)) {
186
+ console.error(`Should never happen`);
187
+ return [];
188
+ }
189
+ if (value.name.name === "className") {
190
+ let inner = value.value;
191
+ if (t.isJSXExpressionContainer(inner)) {
192
+ inner = inner.expression;
193
+ }
194
+ try {
195
+ const evaluatedValue = inner ? attemptEval(inner) : null;
196
+ if (typeof evaluatedValue === "string") {
197
+ attrClassName = t.stringLiteral(evaluatedValue);
198
+ }
199
+ } catch (e) {
200
+ if (inner) {
201
+ attrClassName ||= inner;
165
202
  }
166
- return finalAttrs.push(value), [];
167
203
  }
168
- if (attr.type === "style") return mergeForwardBaseStyle = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, attr.value), baseFontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(attr.value) || "", [];
169
- let ternary = attr.value;
170
- if (ternary.inlineMediaQuery) {
171
- const mediaExtraction = (0, import_extractMediaStyle.extractMediaStyle)(parserProps, attr.value, jsxPath, extractor.getGui(), sourcePath || "", mediaStylesSeen++, shouldPrintDebug);
172
- if (mediaExtraction) if (mediaExtraction.mediaStyles && (mergeForwardBaseStyle = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, {
204
+ return [];
205
+ }
206
+ finalAttrs.push(value);
207
+ return [];
208
+ }
209
+ if (attr.type === "style") {
210
+ mergeForwardBaseStyle = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, attr.value);
211
+ baseFontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(attr.value) || "";
212
+ return [];
213
+ }
214
+ let ternary = attr.value;
215
+ if (ternary.inlineMediaQuery) {
216
+ const mediaExtraction = (0, import_extractMediaStyle.extractMediaStyle)(parserProps, attr.value, jsxPath, extractor.getGui(), sourcePath || "", mediaStylesSeen++, shouldPrintDebug);
217
+ if (mediaExtraction) {
218
+ if (mediaExtraction.mediaStyles) {
219
+ mergeForwardBaseStyle = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, {
173
220
  [`$${ternary.inlineMediaQuery}`]: attr.value.consequent
174
- })), mediaExtraction.ternaryWithoutMedia) ternary = mediaExtraction.ternaryWithoutMedia;else return [];
221
+ });
175
222
  }
176
- let mergedAlternate, mergedConsequent;
177
- return ternary.alternate && Object.keys(ternary.alternate).length && (mergedAlternate = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, ternary.alternate || {}), (0, import_propsToFontFamilyCache.forwardFontFamilyName)(ternary.alternate, mergedAlternate, baseFontFamily)), ternary.consequent && Object.keys(ternary.consequent).length && (mergedConsequent = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, ternary.consequent || {}), (0, import_propsToFontFamilyCache.forwardFontFamilyName)(ternary.consequent, mergedConsequent, baseFontFamily)), {
178
- ...ternary,
179
- alternate: mergedAlternate,
180
- consequent: mergedConsequent
181
- };
182
- }),
183
- hasTernaries = !!onlyTernaries.length,
184
- baseClassNames = mergeForwardBaseStyle ? addStyles(mergeForwardBaseStyle) : null;
185
- let baseClassNameStr = baseClassNames ? baseClassNames.join(" ") : "";
186
- baseFontFamily && (baseClassNameStr = `font_${baseFontFamily}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`), baseClassNameStr = `${staticConfig.isText ? "is_Text" : "is_View"}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`;
187
- const componentNameFinal = staticConfig.componentName;
188
- let base = componentNameFinal && componentNameFinal !== "Text" ? t.stringLiteral(`is_${componentNameFinal}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`) : t.stringLiteral(baseClassNameStr || "");
189
- attrClassName = attrClassName;
190
- const baseClassNameExpression = attrClassName ? t.isStringLiteral(attrClassName) ? t.stringLiteral(base.value ? `${base.value} ${attrClassName.value}` : attrClassName.value) : t.binaryExpression("+", t.binaryExpression("+", attrClassName, spaceString), base) : base,
191
- expandedTernaries = [];
192
- if (onlyTernaries.length) {
193
- const normalizedTernaries = (0, import_normalizeTernaries.normalizeTernaries)(onlyTernaries);
194
- for (const ternary of normalizedTernaries) {
195
- if (!expandedTernaries.length) {
196
- expandTernary(ternary);
197
- continue;
223
+ if (mediaExtraction.ternaryWithoutMedia) {
224
+ ternary = mediaExtraction.ternaryWithoutMedia;
225
+ } else {
226
+ return [];
198
227
  }
199
- const prevTernaries = [...expandedTernaries];
200
- for (const prev of prevTernaries) expandTernary(ternary, prev);
201
228
  }
202
229
  }
203
- function expandTernary(ternary, prev) {
204
- if (ternary.consequent && Object.keys(ternary.consequent).length) {
205
- const fontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(ternary.consequent);
230
+ let mergedAlternate;
231
+ let mergedConsequent;
232
+ if (ternary.alternate && Object.keys(ternary.alternate).length) {
233
+ mergedAlternate = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, ternary.alternate || {});
234
+ (0, import_propsToFontFamilyCache.forwardFontFamilyName)(ternary.alternate, mergedAlternate, baseFontFamily);
235
+ }
236
+ if (ternary.consequent && Object.keys(ternary.consequent).length) {
237
+ mergedConsequent = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, ternary.consequent || {});
238
+ (0, import_propsToFontFamilyCache.forwardFontFamilyName)(ternary.consequent, mergedConsequent, baseFontFamily);
239
+ }
240
+ return {
241
+ ...ternary,
242
+ alternate: mergedAlternate,
243
+ consequent: mergedConsequent
244
+ };
245
+ });
246
+ const hasTernaries = Boolean(onlyTernaries.length);
247
+ const baseClassNames = mergeForwardBaseStyle ? addStyles(mergeForwardBaseStyle) : null;
248
+ let baseClassNameStr = !baseClassNames ? "" : baseClassNames.join(" ");
249
+ if (baseFontFamily) {
250
+ baseClassNameStr = `font_${baseFontFamily}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`;
251
+ }
252
+ const baseTypeClass = staticConfig.isText ? "is_Text" : "is_View";
253
+ baseClassNameStr = `${baseTypeClass}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`;
254
+ const componentNameFinal = staticConfig.componentName;
255
+ let base = componentNameFinal && componentNameFinal !== "Text" ? t.stringLiteral(`is_${componentNameFinal}${baseClassNameStr ? ` ${baseClassNameStr}` : ""}`) : t.stringLiteral(baseClassNameStr || "");
256
+ attrClassName = attrClassName;
257
+ const baseClassNameExpression = (() => {
258
+ if (attrClassName) {
259
+ if (t.isStringLiteral(attrClassName)) {
260
+ return t.stringLiteral(base.value ? `${base.value} ${attrClassName.value}` : attrClassName.value);
261
+ } else {
262
+ return t.binaryExpression("+", t.binaryExpression("+", attrClassName, spaceString), base);
263
+ }
264
+ }
265
+ return base;
266
+ })();
267
+ const expandedTernaries = [];
268
+ if (onlyTernaries.length) {
269
+ const normalizedTernaries = (0, import_normalizeTernaries.normalizeTernaries)(onlyTernaries);
270
+ for (const ternary of normalizedTernaries) {
271
+ if (!expandedTernaries.length) {
272
+ expandTernary(ternary);
273
+ continue;
274
+ }
275
+ const prevTernaries = [...expandedTernaries];
276
+ for (const prev of prevTernaries) {
277
+ expandTernary(ternary, prev);
278
+ }
279
+ }
280
+ }
281
+ function expandTernary(ternary, prev) {
282
+ if (ternary.consequent && Object.keys(ternary.consequent).length) {
283
+ const fontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(ternary.consequent);
284
+ expandedTernaries.push({
285
+ fontFamily,
286
+ // prevTest && test: merge consequent
287
+ test: prev ? t.logicalExpression("&&", prev.test, ternary.test) : ternary.test,
288
+ consequent: prev ? (0, import_web.mergeProps)(prev.consequent, ternary.consequent) : ternary.consequent,
289
+ remove,
290
+ alternate: null
291
+ });
292
+ if (prev) {
206
293
  expandedTernaries.push({
207
- fontFamily,
208
- // prevTest && test: merge consequent
209
- test: prev ? t.logicalExpression("&&", prev.test, ternary.test) : ternary.test,
210
- consequent: prev ? (0, import_web.mergeProps)(prev.consequent, ternary.consequent) : ternary.consequent,
211
- remove,
212
- alternate: null
213
- }), prev && expandedTernaries.push({
214
294
  fontFamily,
215
295
  // !prevTest && test: just consequent
216
296
  test: t.logicalExpression("&&", t.unaryExpression("!", prev.test), ternary.test),
@@ -219,17 +299,20 @@ async function extractToClassNames({
219
299
  remove
220
300
  });
221
301
  }
222
- if (ternary.alternate && Object.keys(ternary.alternate).length) {
223
- const fontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(ternary.alternate),
224
- negated = t.unaryExpression("!", ternary.test);
302
+ }
303
+ if (ternary.alternate && Object.keys(ternary.alternate).length) {
304
+ const fontFamily = (0, import_propsToFontFamilyCache.getFontFamilyNameFromProps)(ternary.alternate);
305
+ const negated = t.unaryExpression("!", ternary.test);
306
+ expandedTernaries.push({
307
+ fontFamily,
308
+ // prevTest && !test: merge alternate
309
+ test: prev ? t.logicalExpression("&&", prev.test, negated) : negated,
310
+ consequent: prev ? (0, import_web.mergeProps)(prev.alternate, ternary.alternate) : ternary.alternate,
311
+ remove,
312
+ alternate: null
313
+ });
314
+ if (prev) {
225
315
  expandedTernaries.push({
226
- fontFamily,
227
- // prevTest && !test: merge alternate
228
- test: prev ? t.logicalExpression("&&", prev.test, negated) : negated,
229
- consequent: prev ? (0, import_web.mergeProps)(prev.alternate, ternary.alternate) : ternary.alternate,
230
- remove,
231
- alternate: null
232
- }), prev && expandedTernaries.push({
233
316
  fontFamily,
234
317
  test: t.logicalExpression("&&", t.unaryExpression("!", prev.test), ternary.test),
235
318
  consequent: ternary.alternate,
@@ -238,49 +321,65 @@ async function extractToClassNames({
238
321
  });
239
322
  }
240
323
  }
241
- let ternaryClassNameExpr = null;
242
- if (hasTernaries) for (const ternary of expandedTernaries) {
324
+ }
325
+ let ternaryClassNameExpr = null;
326
+ if (hasTernaries) {
327
+ for (const ternary of expandedTernaries) {
243
328
  if (!ternary.consequent) continue;
244
329
  const classNames = addStyles(ternary.consequent);
245
- ternary.fontFamily && classNames.unshift(`font_${ternary.fontFamily}`);
246
- const baseString = t.isStringLiteral(baseClassNameExpression) ? baseClassNameExpression.value : "",
247
- fullClassNameWithDups = (baseString ? `${baseString} ` : "") + classNames.join(" "),
248
- fullClassName = (0, import_concatClassName.concatClassName)(fullClassNameWithDups),
249
- classNameLiteral = t.stringLiteral(fullClassName);
250
- ternaryClassNameExpr ? ternaryClassNameExpr = t.conditionalExpression(ternary.test, classNameLiteral, ternaryClassNameExpr) : ternaryClassNameExpr = t.conditionalExpression(ternary.test, classNameLiteral, baseClassNameExpression);
330
+ if (ternary.fontFamily) {
331
+ classNames.unshift(`font_${ternary.fontFamily}`);
332
+ }
333
+ const baseString = t.isStringLiteral(baseClassNameExpression) ? baseClassNameExpression.value : "";
334
+ const fullClassNameWithDups = (baseString ? `${baseString} ` : "") + classNames.join(" ");
335
+ const fullClassName = (0, import_concatClassName.concatClassName)(fullClassNameWithDups);
336
+ const classNameLiteral = t.stringLiteral(fullClassName);
337
+ if (!ternaryClassNameExpr) {
338
+ ternaryClassNameExpr = t.conditionalExpression(ternary.test, classNameLiteral, baseClassNameExpression);
339
+ } else {
340
+ ternaryClassNameExpr = t.conditionalExpression(ternary.test, classNameLiteral, ternaryClassNameExpr);
341
+ }
251
342
  }
252
- let finalExpression = ternaryClassNameExpr || baseClassNameExpression || null;
253
- if (shouldPrintDebug && (console.info("attrs", JSON.stringify(attrs, null, 2)), console.info("expandedTernaries", JSON.stringify(expandedTernaries, null, 2)), console.info("finalExpression", JSON.stringify(finalExpression, null, 2)), console.info({
343
+ }
344
+ let finalExpression = ternaryClassNameExpr || baseClassNameExpression || null;
345
+ if (shouldPrintDebug) {
346
+ console.info("attrs", JSON.stringify(attrs, null, 2));
347
+ console.info("expandedTernaries", JSON.stringify(expandedTernaries, null, 2));
348
+ console.info("finalExpression", JSON.stringify(finalExpression, null, 2));
349
+ console.info({
254
350
  hasTernaries,
255
351
  baseClassNameExpression
256
- })), finalExpression) {
257
- finalExpression = hoistClassNames(jsxPath, finalExpression);
258
- const classNameProp = t.jsxAttribute(t.jsxIdentifier("className"), t.jsxExpressionContainer(finalExpression));
259
- finalAttrs.unshift(classNameProp);
260
- }
261
- node.attributes = finalAttrs;
352
+ });
262
353
  }
263
- });
264
- if (!res || !res.modified && !res.optimized && !res.flattened && !res.styled) return shouldPrintDebug && console.info("no res or none modified", res), null;
265
- const styles = Array.from(cssMap.values()).map(x => x.css).join(`
266
- `).trim(),
267
- result = (0, import_generator.default)(ast, {
268
- concise: !1,
269
- filename: sourcePath,
270
- // this makes the debug output terrible, and i think sourcemap works already
271
- retainLines: !1,
272
- sourceFileName: sourcePath,
273
- sourceMaps: !0
274
- }, source);
275
- return shouldPrintDebug && (console.info(`
276
- -------- output code -------
277
-
278
- `, result.code.split(`
279
- `).filter(x => !x.startsWith("//")).join(`
280
- `)), console.info(`
281
- -------- output style --------
282
-
283
- `, styles)), printLog(res), {
354
+ if (finalExpression) {
355
+ finalExpression = hoistClassNames(jsxPath, finalExpression);
356
+ const classNameProp = t.jsxAttribute(t.jsxIdentifier("className"), t.jsxExpressionContainer(finalExpression));
357
+ finalAttrs.unshift(classNameProp);
358
+ }
359
+ node.attributes = finalAttrs;
360
+ }
361
+ });
362
+ if (!res || !res.modified && !res.optimized && !res.flattened && !res.styled) {
363
+ if (shouldPrintDebug) {
364
+ console.info("no res or none modified", res);
365
+ }
366
+ return null;
367
+ }
368
+ const styles = Array.from(cssMap.values()).map(x => x.css).join("\n").trim();
369
+ const result = (0, import_generator.default)(ast, {
370
+ concise: false,
371
+ filename: sourcePath,
372
+ // this makes the debug output terrible, and i think sourcemap works already
373
+ retainLines: false,
374
+ sourceFileName: sourcePath,
375
+ sourceMaps: true
376
+ }, source);
377
+ if (shouldPrintDebug) {
378
+ console.info("\n -------- output code ------- \n\n", result.code.split("\n").filter(x => !x.startsWith("//")).join("\n"));
379
+ console.info("\n -------- output style -------- \n\n", styles);
380
+ }
381
+ printLog(res);
382
+ return {
284
383
  ast,
285
384
  styles,
286
385
  js: result.code,
@@ -294,28 +393,35 @@ async function extractToClassNames({
294
393
  };
295
394
  }
296
395
  function hoistClassNames(path2, expr) {
297
- if (t.isStringLiteral(expr)) return hoistClassName(path2, expr.value);
396
+ if (t.isStringLiteral(expr)) {
397
+ return hoistClassName(path2, expr.value);
398
+ }
298
399
  if (t.isLogicalExpression(expr)) {
299
- const left = t.isStringLiteral(expr.left) ? hoistClassName(path2, expr.left.value) : expr.left,
300
- right = t.isStringLiteral(expr.right) ? hoistClassName(path2, expr.right.value) : hoistClassNames(path2, expr.right);
400
+ const left = t.isStringLiteral(expr.left) ? hoistClassName(path2, expr.left.value) : expr.left;
401
+ const right = t.isStringLiteral(expr.right) ? hoistClassName(path2, expr.right.value) : hoistClassNames(path2, expr.right);
301
402
  return t.logicalExpression(expr.operator, left, right);
302
403
  }
303
404
  if (t.isConditionalExpression(expr)) {
304
- const cons = t.isStringLiteral(expr.consequent) ? hoistClassName(path2, expr.consequent.value) : hoistClassNames(path2, expr.consequent),
305
- alt = t.isStringLiteral(expr.alternate) ? hoistClassName(path2, expr.alternate.value) : hoistClassNames(path2, expr.alternate);
405
+ const cons = t.isStringLiteral(expr.consequent) ? hoistClassName(path2, expr.consequent.value) : hoistClassNames(path2, expr.consequent);
406
+ const alt = t.isStringLiteral(expr.alternate) ? hoistClassName(path2, expr.alternate.value) : hoistClassNames(path2, expr.alternate);
306
407
  return t.conditionalExpression(expr.test, cons, alt);
307
408
  }
308
409
  return expr;
309
410
  }
310
411
  function hoistClassName(path2, str) {
311
- const uid = path2.scope.generateUidIdentifier("cn"),
312
- parent = path2.findParent(path3 => path3.isProgram());
313
- if (!parent) throw new Error("no program?");
412
+ const uid = path2.scope.generateUidIdentifier("cn");
413
+ const parent = path2.findParent(path3 => path3.isProgram());
414
+ if (!parent) throw new Error(`no program?`);
314
415
  const variable = t.variableDeclaration("const", [t.variableDeclarator(uid, t.stringLiteral(cleanupClassName(str)))]);
315
- return parent.unshiftContainer("body", variable), uid;
416
+ parent.unshiftContainer("body", variable);
417
+ return uid;
316
418
  }
317
419
  function cleanupClassName(inStr) {
318
420
  const out = /* @__PURE__ */new Set();
319
- for (const part of inStr.split(" ")) !part || part === " " || part !== "font_" && out.add(part);
421
+ for (const part of inStr.split(" ")) {
422
+ if (!part || part === " ") continue;
423
+ if (part === "font_") continue;
424
+ out.add(part);
425
+ }
320
426
  return [...out].join(" ");
321
427
  }