@hanzogui/static 3.0.6 → 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.
- package/dist/check-dep-versions.cjs +201 -96
- package/dist/checkDeps.cjs +163 -92
- package/dist/constants.cjs +32 -30
- package/dist/exports.cjs +15 -13
- package/dist/extractor/accessSafe.cjs +25 -23
- package/dist/extractor/babelParse.cjs +30 -28
- package/dist/extractor/buildClassName.cjs +59 -35
- package/dist/extractor/bundle.cjs +159 -104
- package/dist/extractor/bundleConfig.cjs +463 -263
- package/dist/extractor/concatClassName.cjs +41 -29
- package/dist/extractor/createEvaluator.cjs +54 -41
- package/dist/extractor/createExtractor.cjs +1338 -661
- package/dist/extractor/createLogger.cjs +30 -25
- package/dist/extractor/detectModuleFormat.cjs +22 -16
- package/dist/extractor/ensureImportingConcat.cjs +31 -25
- package/dist/extractor/errors.cjs +12 -10
- package/dist/extractor/esbuildAliasPlugin.cjs +28 -16
- package/dist/extractor/esbuildTsconfigPaths.cjs +58 -36
- package/dist/extractor/evaluateAstNode.cjs +104 -59
- package/dist/extractor/extractHelpers.cjs +130 -67
- package/dist/extractor/extractMediaStyle.cjs +110 -69
- package/dist/extractor/extractToClassNames.cjs +336 -230
- package/dist/extractor/extractToNative.cjs +244 -149
- package/dist/extractor/findTopmostFunction.cjs +22 -13
- package/dist/extractor/generatedUid.cjs +39 -28
- package/dist/extractor/getGuiConfigPathFromOptionsConfig.cjs +20 -14
- package/dist/extractor/getPrefixLogs.cjs +12 -10
- package/dist/extractor/getPropValueFromAttributes.cjs +52 -34
- package/dist/extractor/getSourceModule.cjs +73 -37
- package/dist/extractor/getStaticBindingsForScope.cjs +131 -68
- package/dist/extractor/hoistClassNames.cjs +46 -32
- package/dist/extractor/literalToAst.cjs +67 -42
- package/dist/extractor/loadFile.cjs +9 -3
- package/dist/extractor/loadGui.cjs +198 -109
- package/dist/extractor/logLines.cjs +27 -19
- package/dist/extractor/normalizeTernaries.cjs +66 -44
- package/dist/extractor/propsToFontFamilyCache.cjs +15 -11
- package/dist/extractor/regenerateConfig.cjs +109 -81
- package/dist/extractor/removeUnusedHooks.cjs +73 -41
- package/dist/extractor/timer.cjs +23 -14
- package/dist/extractor/validHTMLAttributes.cjs +61 -59
- package/dist/extractor/watchGuiConfig.cjs +35 -23
- package/dist/getPragmaOptions.cjs +34 -19
- package/dist/helpers/memoize.cjs +24 -16
- package/dist/helpers/requireGuiCore.cjs +22 -15
- package/dist/index.cjs +26 -24
- package/dist/registerRequire.cjs +132 -77
- package/dist/server.cjs +35 -28
- package/dist/types.cjs +7 -5
- package/dist/worker.cjs +62 -40
- package/package.json +2 -2
- package/src/checkDeps.ts +1 -1
- package/src/extractor/bundle.ts +1 -1
- package/src/extractor/bundleConfig.ts +5 -5
- package/src/extractor/createExtractor.ts +28 -13
- package/src/extractor/createLogger.ts +1 -3
- package/src/extractor/extractToNative.ts +35 -9
- package/src/extractor/loadGui.ts +4 -4
- package/src/extractor/regenerateConfig.ts +2 -2
- package/src/getPragmaOptions.ts +2 -2
- package/src/helpers/requireGuiCore.ts +6 -6
- package/src/registerRequire.ts +4 -4
- package/src/worker.ts +1 -1
- package/types/extractor/createExtractor.d.ts.map +1 -1
- package/types/extractor/createLogger.d.ts.map +1 -1
- package/types/extractor/extractToNative.d.ts.map +1 -1
- 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
|
-
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
7
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
20
22
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const remove = () => {}
|
|
52
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (sourcePath.includes("node_modules"))
|
|
66
|
-
|
|
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
|
-
`)
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
92
|
+
console.error("babel parse error:", sourcePath.slice(0, 100));
|
|
93
|
+
throw err;
|
|
77
94
|
}
|
|
78
|
-
tm.mark(
|
|
79
|
-
const cssMap = /* @__PURE__ */new Map()
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
})
|
|
150
|
+
});
|
|
125
151
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
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
|
-
|
|
177
|
+
const identifier = addStyle(style2);
|
|
178
|
+
classNames.push(identifier);
|
|
150
179
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
})
|
|
221
|
+
});
|
|
175
222
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
242
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
253
|
-
|
|
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
|
-
})
|
|
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
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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))
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
313
|
-
if (!parent) throw new Error(
|
|
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
|
-
|
|
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(" "))
|
|
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
|
}
|