@hanzogui/static 2.0.0-rc.41-hanzoai.5

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 (211) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/dist/check-dep-versions.cjs +372 -0
  4. package/dist/checkDeps.cjs +271 -0
  5. package/dist/constants.cjs +51 -0
  6. package/dist/exports.cjs +46 -0
  7. package/dist/extractor/accessSafe.cjs +48 -0
  8. package/dist/extractor/babelParse.cjs +56 -0
  9. package/dist/extractor/buildClassName.cjs +86 -0
  10. package/dist/extractor/bundle.cjs +224 -0
  11. package/dist/extractor/bundleConfig.cjs +716 -0
  12. package/dist/extractor/concatClassName.cjs +92 -0
  13. package/dist/extractor/createEvaluator.cjs +88 -0
  14. package/dist/extractor/createExtractor.cjs +1827 -0
  15. package/dist/extractor/createLogger.cjs +50 -0
  16. package/dist/extractor/detectModuleFormat.cjs +55 -0
  17. package/dist/extractor/ensureImportingConcat.cjs +54 -0
  18. package/dist/extractor/errors.cjs +28 -0
  19. package/dist/extractor/esbuildAliasPlugin.cjs +56 -0
  20. package/dist/extractor/esbuildTsconfigPaths.cjs +108 -0
  21. package/dist/extractor/evaluateAstNode.cjs +128 -0
  22. package/dist/extractor/extractHelpers.cjs +182 -0
  23. package/dist/extractor/extractMediaStyle.cjs +169 -0
  24. package/dist/extractor/extractToClassNames.cjs +427 -0
  25. package/dist/extractor/extractToNative.cjs +361 -0
  26. package/dist/extractor/findTopmostFunction.cjs +43 -0
  27. package/dist/extractor/generatedUid.cjs +58 -0
  28. package/dist/extractor/getHanzoguiConfigPathFromOptionsConfig.cjs +41 -0
  29. package/dist/extractor/getPrefixLogs.cjs +31 -0
  30. package/dist/extractor/getPropValueFromAttributes.cjs +85 -0
  31. package/dist/extractor/getSourceModule.cjs +100 -0
  32. package/dist/extractor/getStaticBindingsForScope.cjs +213 -0
  33. package/dist/extractor/hasTopLevelAwait.cjs +62 -0
  34. package/dist/extractor/hoistClassNames.cjs +76 -0
  35. package/dist/extractor/literalToAst.cjs +109 -0
  36. package/dist/extractor/loadFile.cjs +17 -0
  37. package/dist/extractor/loadHanzogui.cjs +367 -0
  38. package/dist/extractor/logLines.cjs +43 -0
  39. package/dist/extractor/normalizeTernaries.cjs +95 -0
  40. package/dist/extractor/propsToFontFamilyCache.cjs +42 -0
  41. package/dist/extractor/regenerateConfig.cjs +178 -0
  42. package/dist/extractor/removeUnusedHooks.cjs +108 -0
  43. package/dist/extractor/timer.cjs +52 -0
  44. package/dist/extractor/validHTMLAttributes.cjs +79 -0
  45. package/dist/extractor/watchHanzoguiConfig.cjs +70 -0
  46. package/dist/getPragmaOptions.cjs +73 -0
  47. package/dist/helpers/memoize.cjs +45 -0
  48. package/dist/helpers/requireHanzoguiCore.cjs +40 -0
  49. package/dist/index.cjs +42 -0
  50. package/dist/registerRequire.cjs +208 -0
  51. package/dist/server.cjs +82 -0
  52. package/dist/setup.cjs +0 -0
  53. package/dist/types.cjs +18 -0
  54. package/dist/worker.cjs +111 -0
  55. package/package.json +101 -0
  56. package/src/check-dep-versions.ts +738 -0
  57. package/src/checkDeps.ts +371 -0
  58. package/src/constants.ts +14 -0
  59. package/src/exports.ts +15 -0
  60. package/src/extractor/accessSafe.ts +22 -0
  61. package/src/extractor/babelParse.ts +38 -0
  62. package/src/extractor/buildClassName.ts +76 -0
  63. package/src/extractor/bundle.ts +277 -0
  64. package/src/extractor/bundleConfig.ts +958 -0
  65. package/src/extractor/concatClassName.ts +105 -0
  66. package/src/extractor/createEvaluator.ts +82 -0
  67. package/src/extractor/createExtractor.ts +2680 -0
  68. package/src/extractor/createLogger.ts +41 -0
  69. package/src/extractor/detectModuleFormat.ts +42 -0
  70. package/src/extractor/ensureImportingConcat.ts +36 -0
  71. package/src/extractor/errors.ts +1 -0
  72. package/src/extractor/esbuildAliasPlugin.ts +40 -0
  73. package/src/extractor/esbuildTsconfigPaths.ts +103 -0
  74. package/src/extractor/evaluateAstNode.ts +136 -0
  75. package/src/extractor/extractHelpers.ts +211 -0
  76. package/src/extractor/extractMediaStyle.ts +194 -0
  77. package/src/extractor/extractToClassNames.ts +631 -0
  78. package/src/extractor/extractToNative.ts +489 -0
  79. package/src/extractor/findTopmostFunction.ts +24 -0
  80. package/src/extractor/generatedUid.ts +41 -0
  81. package/src/extractor/getHanzoguiConfigPathFromOptionsConfig.ts +24 -0
  82. package/src/extractor/getPrefixLogs.ts +9 -0
  83. package/src/extractor/getPropValueFromAttributes.ts +95 -0
  84. package/src/extractor/getSourceModule.ts +96 -0
  85. package/src/extractor/getStaticBindingsForScope.ts +237 -0
  86. package/src/extractor/hasTopLevelAwait.ts +28 -0
  87. package/src/extractor/hoistClassNames.ts +52 -0
  88. package/src/extractor/literalToAst.ts +85 -0
  89. package/src/extractor/loadFile.ts +17 -0
  90. package/src/extractor/loadHanzogui.ts +487 -0
  91. package/src/extractor/logLines.ts +16 -0
  92. package/src/extractor/normalizeTernaries.ts +75 -0
  93. package/src/extractor/propsToFontFamilyCache.ts +18 -0
  94. package/src/extractor/regenerateConfig.ts +186 -0
  95. package/src/extractor/removeUnusedHooks.ts +83 -0
  96. package/src/extractor/timer.ts +25 -0
  97. package/src/extractor/validHTMLAttributes.ts +52 -0
  98. package/src/extractor/watchHanzoguiConfig.ts +57 -0
  99. package/src/getPragmaOptions.ts +57 -0
  100. package/src/helpers/memoize.ts +24 -0
  101. package/src/helpers/requireHanzoguiCore.ts +28 -0
  102. package/src/index.ts +4 -0
  103. package/src/registerRequire.ts +283 -0
  104. package/src/server.ts +43 -0
  105. package/src/setup.ts +0 -0
  106. package/src/types.ts +109 -0
  107. package/src/worker.ts +142 -0
  108. package/types/check-dep-versions.d.ts +37 -0
  109. package/types/check-dep-versions.d.ts.map +1 -0
  110. package/types/checkDeps.d.ts +19 -0
  111. package/types/checkDeps.d.ts.map +1 -0
  112. package/types/constants.d.ts +6 -0
  113. package/types/constants.d.ts.map +1 -0
  114. package/types/exports.d.ts +16 -0
  115. package/types/exports.d.ts.map +1 -0
  116. package/types/extractor/accessSafe.d.ts +3 -0
  117. package/types/extractor/accessSafe.d.ts.map +1 -0
  118. package/types/extractor/babelParse.d.ts +5 -0
  119. package/types/extractor/babelParse.d.ts.map +1 -0
  120. package/types/extractor/buildClassName.d.ts +7 -0
  121. package/types/extractor/buildClassName.d.ts.map +1 -0
  122. package/types/extractor/bundle.d.ts +121 -0
  123. package/types/extractor/bundle.d.ts.map +1 -0
  124. package/types/extractor/bundleConfig.d.ts +49 -0
  125. package/types/extractor/bundleConfig.d.ts.map +1 -0
  126. package/types/extractor/concatClassName.d.ts +8 -0
  127. package/types/extractor/concatClassName.d.ts.map +1 -0
  128. package/types/extractor/createEvaluator.d.ts +12 -0
  129. package/types/extractor/createEvaluator.d.ts.map +1 -0
  130. package/types/extractor/createExtractor.d.ts +32 -0
  131. package/types/extractor/createExtractor.d.ts.map +1 -0
  132. package/types/extractor/createLogger.d.ts +3 -0
  133. package/types/extractor/createLogger.d.ts.map +1 -0
  134. package/types/extractor/detectModuleFormat.d.ts +5 -0
  135. package/types/extractor/detectModuleFormat.d.ts.map +1 -0
  136. package/types/extractor/ensureImportingConcat.d.ts +4 -0
  137. package/types/extractor/ensureImportingConcat.d.ts.map +1 -0
  138. package/types/extractor/errors.d.ts +3 -0
  139. package/types/extractor/errors.d.ts.map +1 -0
  140. package/types/extractor/esbuildAliasPlugin.d.ts +18 -0
  141. package/types/extractor/esbuildAliasPlugin.d.ts.map +1 -0
  142. package/types/extractor/esbuildTsconfigPaths.d.ts +11 -0
  143. package/types/extractor/esbuildTsconfigPaths.d.ts.map +1 -0
  144. package/types/extractor/evaluateAstNode.d.ts +3 -0
  145. package/types/extractor/evaluateAstNode.d.ts.map +1 -0
  146. package/types/extractor/extractHelpers.d.ts +28 -0
  147. package/types/extractor/extractHelpers.d.ts.map +1 -0
  148. package/types/extractor/extractMediaStyle.d.ts +11 -0
  149. package/types/extractor/extractMediaStyle.d.ts.map +1 -0
  150. package/types/extractor/extractToClassNames.d.ts +25 -0
  151. package/types/extractor/extractToClassNames.d.ts.map +1 -0
  152. package/types/extractor/extractToNative.d.ts +13 -0
  153. package/types/extractor/extractToNative.d.ts.map +1 -0
  154. package/types/extractor/findTopmostFunction.d.ts +4 -0
  155. package/types/extractor/findTopmostFunction.d.ts.map +1 -0
  156. package/types/extractor/generatedUid.d.ts +5 -0
  157. package/types/extractor/generatedUid.d.ts.map +1 -0
  158. package/types/extractor/getHanzoguiConfigPathFromOptionsConfig.d.ts +3 -0
  159. package/types/extractor/getHanzoguiConfigPathFromOptionsConfig.d.ts.map +1 -0
  160. package/types/extractor/getPrefixLogs.d.ts +3 -0
  161. package/types/extractor/getPrefixLogs.d.ts.map +1 -0
  162. package/types/extractor/getPropValueFromAttributes.d.ts +19 -0
  163. package/types/extractor/getPropValueFromAttributes.d.ts.map +1 -0
  164. package/types/extractor/getSourceModule.d.ts +16 -0
  165. package/types/extractor/getSourceModule.d.ts.map +1 -0
  166. package/types/extractor/getStaticBindingsForScope.d.ts +5 -0
  167. package/types/extractor/getStaticBindingsForScope.d.ts.map +1 -0
  168. package/types/extractor/hasTopLevelAwait.d.ts +2 -0
  169. package/types/extractor/hasTopLevelAwait.d.ts.map +1 -0
  170. package/types/extractor/hoistClassNames.d.ts +6 -0
  171. package/types/extractor/hoistClassNames.d.ts.map +1 -0
  172. package/types/extractor/literalToAst.d.ts +4 -0
  173. package/types/extractor/literalToAst.d.ts.map +1 -0
  174. package/types/extractor/loadFile.d.ts +1 -0
  175. package/types/extractor/loadFile.d.ts.map +1 -0
  176. package/types/extractor/loadHanzogui.d.ts +22 -0
  177. package/types/extractor/loadHanzogui.d.ts.map +1 -0
  178. package/types/extractor/logLines.d.ts +2 -0
  179. package/types/extractor/logLines.d.ts.map +1 -0
  180. package/types/extractor/normalizeTernaries.d.ts +3 -0
  181. package/types/extractor/normalizeTernaries.d.ts.map +1 -0
  182. package/types/extractor/propsToFontFamilyCache.d.ts +4 -0
  183. package/types/extractor/propsToFontFamilyCache.d.ts.map +1 -0
  184. package/types/extractor/regenerateConfig.d.ts +9 -0
  185. package/types/extractor/regenerateConfig.d.ts.map +1 -0
  186. package/types/extractor/removeUnusedHooks.d.ts +3 -0
  187. package/types/extractor/removeUnusedHooks.d.ts.map +1 -0
  188. package/types/extractor/timer.d.ts +5 -0
  189. package/types/extractor/timer.d.ts.map +1 -0
  190. package/types/extractor/validHTMLAttributes.d.ts +52 -0
  191. package/types/extractor/validHTMLAttributes.d.ts.map +1 -0
  192. package/types/extractor/watchHanzoguiConfig.d.ts +5 -0
  193. package/types/extractor/watchHanzoguiConfig.d.ts.map +1 -0
  194. package/types/getPragmaOptions.d.ts +8 -0
  195. package/types/getPragmaOptions.d.ts.map +1 -0
  196. package/types/helpers/memoize.d.ts +8 -0
  197. package/types/helpers/memoize.d.ts.map +1 -0
  198. package/types/helpers/requireHanzoguiCore.d.ts +3 -0
  199. package/types/helpers/requireHanzoguiCore.d.ts.map +1 -0
  200. package/types/index.d.ts +4 -0
  201. package/types/index.d.ts.map +1 -0
  202. package/types/registerRequire.d.ts +10 -0
  203. package/types/registerRequire.d.ts.map +1 -0
  204. package/types/server.d.ts +3 -0
  205. package/types/server.d.ts.map +1 -0
  206. package/types/setup.d.ts +1 -0
  207. package/types/setup.d.ts.map +1 -0
  208. package/types/types.d.ts +92 -0
  209. package/types/types.d.ts.map +1 -0
  210. package/types/worker.d.ts +46 -0
  211. package/types/worker.d.ts.map +1 -0
@@ -0,0 +1,169 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
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, {
16
+ get: () => from[key],
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
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);
34
+ var extractMediaStyle_exports = {};
35
+ __export(extractMediaStyle_exports, {
36
+ extractMediaStyle: () => extractMediaStyle,
37
+ isValidMediaCall: () => isValidMediaCall
38
+ });
39
+ module.exports = __toCommonJS(extractMediaStyle_exports);
40
+ var t = __toESM(require("@babel/types"));
41
+ var core = __toESM(require("@hanzogui/core"));
42
+ var import_requireHanzoguiCore = require("../helpers/requireHanzoguiCore.cjs");
43
+ var import_extractHelpers = require("./extractHelpers.cjs");
44
+ function extractMediaStyle(props, ternary, jsxPath, hanzoguiConfig, sourcePath, importance = 0, shouldPrintDebug = false) {
45
+ const {
46
+ getCSSStylesAtomic
47
+ } = (0, import_requireHanzoguiCore.requireHanzoguiCore)("web");
48
+ const mt = getMediaQueryTernary(props, ternary, jsxPath, sourcePath);
49
+ if (!mt) {
50
+ return null;
51
+ }
52
+ const {
53
+ key
54
+ } = mt;
55
+ const mq = hanzoguiConfig.media[key];
56
+ if (!mq) {
57
+ console.error(`Media query "${key}" not found: ${Object.keys(hanzoguiConfig.media)}`);
58
+ return null;
59
+ }
60
+ const getStyleObj = (styleObj, negate = false) => {
61
+ return styleObj ? {
62
+ styleObj,
63
+ negate
64
+ } : null;
65
+ };
66
+ const styleOpts = [getStyleObj(ternary.consequent, false), getStyleObj(ternary.alternate, true)].filter(import_extractHelpers.isPresent);
67
+ if (shouldPrintDebug && !styleOpts.length) {
68
+ console.info(" media query, no styles?");
69
+ return null;
70
+ }
71
+ const mediaKeys = Object.keys(hanzoguiConfig.media);
72
+ const mediaKeyPrecendence = mediaKeys.reduce((acc, cur, i) => {
73
+ acc[cur] = new Array(importance + 1).fill(":root").join("");
74
+ return acc;
75
+ }, {});
76
+ let mediaStyles = [];
77
+ for (const {
78
+ styleObj,
79
+ negate
80
+ } of styleOpts) {
81
+ const styles = getCSSStylesAtomic(styleObj);
82
+ const singleMediaStyles = styles.map(style => {
83
+ const mediaStyle = core.createMediaStyle(style, key, hanzoguiConfig.media, true, negate);
84
+ const className = `.${mediaStyle[core.StyleObjectIdentifier]}`;
85
+ return {
86
+ ...mediaStyle,
87
+ className
88
+ };
89
+ });
90
+ if (shouldPrintDebug === "verbose") {
91
+ console.info(" media styles:", importance, styleObj, singleMediaStyles.map(x => x[core.StyleObjectIdentifier]).join(", "));
92
+ }
93
+ mediaStyles = [...mediaStyles, ...singleMediaStyles];
94
+ }
95
+ ternary.remove();
96
+ return {
97
+ mediaStyles,
98
+ ternaryWithoutMedia: mt.ternaryWithoutMedia
99
+ };
100
+ }
101
+ function getMediaQueryTernary(props, ternary, jsxPath, sourcePath) {
102
+ if (t.isLogicalExpression(ternary.test) && ternary.test.operator === "&&") {
103
+ const mediaLeft = getMediaInfoFromExpression(props, ternary.test.left, jsxPath, sourcePath, ternary.inlineMediaQuery);
104
+ if (mediaLeft) {
105
+ return {
106
+ ...mediaLeft,
107
+ ternaryWithoutMedia: {
108
+ ...ternary,
109
+ test: ternary.test.right
110
+ }
111
+ };
112
+ }
113
+ }
114
+ const result = getMediaInfoFromExpression(props, ternary.test, jsxPath, sourcePath, ternary.inlineMediaQuery);
115
+ if (result) {
116
+ return {
117
+ ...result,
118
+ ternaryWithoutMedia: null
119
+ };
120
+ }
121
+ return null;
122
+ }
123
+ function getMediaInfoFromExpression(props, test, jsxPath, sourcePath, inlineMediaQuery) {
124
+ if (inlineMediaQuery) {
125
+ return {
126
+ key: inlineMediaQuery,
127
+ bindingName: inlineMediaQuery
128
+ };
129
+ }
130
+ if (t.isMemberExpression(test) && t.isIdentifier(test.object) && t.isIdentifier(test.property)) {
131
+ const name = test.object["name"];
132
+ const key = test.property["name"];
133
+ const bindings = jsxPath.scope.getAllBindings();
134
+ const binding = bindings[name];
135
+ if (!binding) return false;
136
+ const bindingNode = binding.path?.node;
137
+ if (!t.isVariableDeclarator(bindingNode) || !bindingNode.init) return false;
138
+ if (!isValidMediaCall(props, jsxPath, bindingNode.init, sourcePath)) return false;
139
+ return {
140
+ key,
141
+ bindingName: name
142
+ };
143
+ }
144
+ if (t.isIdentifier(test)) {
145
+ const key = test.name;
146
+ const node = jsxPath.scope.getBinding(test.name)?.path?.node;
147
+ if (!t.isVariableDeclarator(node)) return false;
148
+ if (!node.init || !isValidMediaCall(props, jsxPath, node.init, sourcePath)) return false;
149
+ return {
150
+ key,
151
+ bindingName: key
152
+ };
153
+ }
154
+ return null;
155
+ }
156
+ function isValidMediaCall(props, jsxPath, init, sourcePath) {
157
+ if (!init || !t.isCallExpression(init)) return false;
158
+ if (!t.isIdentifier(init.callee)) return false;
159
+ if (init.callee.name !== "useMedia") return false;
160
+ const bindings = jsxPath.scope.getAllBindings();
161
+ const mediaBinding = bindings["useMedia"];
162
+ if (!mediaBinding) return false;
163
+ const useMediaImport = mediaBinding.path.parent;
164
+ if (!t.isImportDeclaration(useMediaImport)) return false;
165
+ if (!(0, import_extractHelpers.isValidImport)(props, sourcePath)) {
166
+ return false;
167
+ }
168
+ return true;
169
+ }
@@ -0,0 +1,427 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
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, {
16
+ get: () => from[key],
17
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
18
+ });
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
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);
34
+ var extractToClassNames_exports = {};
35
+ __export(extractToClassNames_exports, {
36
+ extractToClassNames: () => extractToClassNames
37
+ });
38
+ module.exports = __toCommonJS(extractToClassNames_exports);
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_requireHanzoguiCore = require("../helpers/requireHanzoguiCore.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(" ");
55
+ async function extractToClassNames({
56
+ extractor,
57
+ source,
58
+ sourcePath = "",
59
+ options,
60
+ shouldPrintDebug
61
+ }) {
62
+ const tm = (0, import_timer.timer)();
63
+ const {
64
+ getCSSStylesAtomic,
65
+ createMediaStyle
66
+ } = (0, import_requireHanzoguiCore.requireHanzoguiCore)("web");
67
+ if (sourcePath.includes("node_modules")) {
68
+ return null;
69
+ }
70
+ if (shouldPrintDebug) {
71
+ console.warn(`--- ${sourcePath} ---
72
+
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["_disableLoadHanzogui"]) {
85
+ await extractor.loadHanzogui(options);
86
+ }
87
+ const printLog = (0, import_createLogger.createLogger)(sourcePath, options);
88
+ let ast;
89
+ try {
90
+ ast = (0, import_babelParse.babelParse)(source, sourcePath);
91
+ } catch (err) {
92
+ console.error("babel parse error:", sourcePath.slice(0, 100));
93
+ throw err;
94
+ }
95
+ tm.mark(`babel-parse`, shouldPrintDebug === "verbose");
96
+ const cssMap = /* @__PURE__ */new Map();
97
+ const hanzoguiConfig = extractor.getHanzogui();
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"),
149
+ commentTexts: [comment]
150
+ });
151
+ }
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.getHanzogui().media, mediaType, false, mediaStylesSeen);
167
+ const identifier2 = addStyle(mediaStyle);
168
+ classNames.push(identifier2);
169
+ continue;
170
+ }
171
+ if (mediaName in hanzoguiConfig.media) {
172
+ const mediaStyle = createMediaStyle(style2, mediaName, extractor.getHanzogui().media, true, false, mediaStylesSeen);
173
+ const identifier2 = addStyle(mediaStyle);
174
+ classNames.push(identifier2);
175
+ continue;
176
+ }
177
+ const identifier = addStyle(style2);
178
+ classNames.push(identifier);
179
+ }
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;
202
+ }
203
+ }
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.getHanzogui(), sourcePath || "", mediaStylesSeen++, shouldPrintDebug);
217
+ if (mediaExtraction) {
218
+ if (mediaExtraction.mediaStyles) {
219
+ mergeForwardBaseStyle = (0, import_web.mergeProps)(mergeForwardBaseStyle || {}, {
220
+ [`$${ternary.inlineMediaQuery}`]: attr.value.consequent
221
+ });
222
+ }
223
+ if (mediaExtraction.ternaryWithoutMedia) {
224
+ ternary = mediaExtraction.ternaryWithoutMedia;
225
+ } else {
226
+ return [];
227
+ }
228
+ }
229
+ }
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) {
293
+ expandedTernaries.push({
294
+ fontFamily,
295
+ // !prevTest && test: just consequent
296
+ test: t.logicalExpression("&&", t.unaryExpression("!", prev.test), ternary.test),
297
+ consequent: ternary.consequent,
298
+ alternate: null,
299
+ remove
300
+ });
301
+ }
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) {
315
+ expandedTernaries.push({
316
+ fontFamily,
317
+ test: t.logicalExpression("&&", t.unaryExpression("!", prev.test), ternary.test),
318
+ consequent: ternary.alternate,
319
+ remove,
320
+ alternate: null
321
+ });
322
+ }
323
+ }
324
+ }
325
+ let ternaryClassNameExpr = null;
326
+ if (hasTernaries) {
327
+ for (const ternary of expandedTernaries) {
328
+ if (!ternary.consequent) continue;
329
+ const classNames = addStyles(ternary.consequent);
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
+ }
342
+ }
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({
350
+ hasTernaries,
351
+ baseClassNameExpression
352
+ });
353
+ }
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 {
383
+ ast,
384
+ styles,
385
+ js: result.code,
386
+ map: result.map,
387
+ stats: {
388
+ styled: res.styled,
389
+ flattened: res.flattened,
390
+ optimized: res.optimized,
391
+ found: res.found
392
+ }
393
+ };
394
+ }
395
+ function hoistClassNames(path2, expr) {
396
+ if (t.isStringLiteral(expr)) {
397
+ return hoistClassName(path2, expr.value);
398
+ }
399
+ if (t.isLogicalExpression(expr)) {
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);
402
+ return t.logicalExpression(expr.operator, left, right);
403
+ }
404
+ if (t.isConditionalExpression(expr)) {
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);
407
+ return t.conditionalExpression(expr.test, cons, alt);
408
+ }
409
+ return expr;
410
+ }
411
+ function hoistClassName(path2, str) {
412
+ const uid = path2.scope.generateUidIdentifier("cn");
413
+ const parent = path2.findParent(path3 => path3.isProgram());
414
+ if (!parent) throw new Error(`no program?`);
415
+ const variable = t.variableDeclaration("const", [t.variableDeclarator(uid, t.stringLiteral(cleanupClassName(str)))]);
416
+ parent.unshiftContainer("body", variable);
417
+ return uid;
418
+ }
419
+ function cleanupClassName(inStr) {
420
+ const out = /* @__PURE__ */new Set();
421
+ for (const part of inStr.split(" ")) {
422
+ if (!part || part === " ") continue;
423
+ if (part === "font_") continue;
424
+ out.add(part);
425
+ }
426
+ return [...out].join(" ");
427
+ }