@builder.io/mitosis 0.5.31 → 0.5.32
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.
|
@@ -21,6 +21,7 @@ const builder_1 = require("../../parsers/builder");
|
|
|
21
21
|
const symbol_processor_1 = require("../../symbols/symbol-processor");
|
|
22
22
|
const core_1 = require("@babel/core");
|
|
23
23
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
24
|
+
const parser_1 = require("@babel/parser");
|
|
24
25
|
const json5_1 = __importDefault(require("json5"));
|
|
25
26
|
const lodash_1 = require("lodash");
|
|
26
27
|
const legacy_1 = __importDefault(require("neotraverse/legacy"));
|
|
@@ -285,6 +286,134 @@ const processLocalizedValues = (element, node) => {
|
|
|
285
286
|
}
|
|
286
287
|
return element;
|
|
287
288
|
};
|
|
289
|
+
/**
|
|
290
|
+
* Turns a stringified object into an object that can be looped over.
|
|
291
|
+
* Since values in the stringified object could be JS expressions, all
|
|
292
|
+
* values in the resulting object will remain strings.
|
|
293
|
+
* @param input - The stringified object
|
|
294
|
+
*/
|
|
295
|
+
const parseJSObject = (input) => {
|
|
296
|
+
var _a;
|
|
297
|
+
const unparsed = [];
|
|
298
|
+
let parsed = {};
|
|
299
|
+
try {
|
|
300
|
+
const ast = (0, parser_1.parseExpression)(`(${input})`, {
|
|
301
|
+
plugins: ['jsx', 'typescript'],
|
|
302
|
+
sourceType: 'module',
|
|
303
|
+
});
|
|
304
|
+
if (ast.type !== 'ObjectExpression') {
|
|
305
|
+
return { parsed, unparsed: input };
|
|
306
|
+
}
|
|
307
|
+
for (const prop of ast.properties) {
|
|
308
|
+
/**
|
|
309
|
+
* If the object includes spread or method, we stop. We can't really break the component into Key/Value
|
|
310
|
+
* and the whole expression is considered dynamic. We return `false` to signify that.
|
|
311
|
+
*/
|
|
312
|
+
if (prop.type === 'ObjectMethod' || prop.type === 'SpreadElement') {
|
|
313
|
+
if (!!prop.start && !!prop.end) {
|
|
314
|
+
if (typeof input === 'string') {
|
|
315
|
+
unparsed.push(input.slice(prop.start - 1, prop.end - 1));
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Ignore shorthand objects when processing incomplete objects. Otherwise we may
|
|
322
|
+
* create identifiers unintentionally.
|
|
323
|
+
* Example: When accounting for shorthand objects, "{ color" would become
|
|
324
|
+
* { color: color } thus creating a "color" identifier that does not exist.
|
|
325
|
+
*/
|
|
326
|
+
if (prop.type === 'ObjectProperty') {
|
|
327
|
+
if ((_a = prop.extra) === null || _a === void 0 ? void 0 : _a.shorthand) {
|
|
328
|
+
if (typeof input === 'string') {
|
|
329
|
+
unparsed.push(input.slice(prop.start - 1, prop.end - 1));
|
|
330
|
+
}
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
let key = '';
|
|
334
|
+
if (prop.key.type === 'Identifier') {
|
|
335
|
+
key = prop.key.name;
|
|
336
|
+
}
|
|
337
|
+
else if (prop.key.type === 'StringLiteral') {
|
|
338
|
+
key = prop.key.value;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (typeof input === 'string') {
|
|
344
|
+
const [val, err] = extractValue(input, prop.value);
|
|
345
|
+
if (err === null) {
|
|
346
|
+
parsed[key] = val;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
parsed,
|
|
353
|
+
unparsed: unparsed.length > 0 ? `{${unparsed.join('\n')}}` : undefined,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
catch (err) {
|
|
357
|
+
return {
|
|
358
|
+
parsed,
|
|
359
|
+
unparsed: unparsed.length > 0 ? `{${unparsed.join('\n')}}` : undefined,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
const extractValue = (input, node) => {
|
|
364
|
+
var _a, _b;
|
|
365
|
+
const start = (_a = node === null || node === void 0 ? void 0 : node.loc) === null || _a === void 0 ? void 0 : _a.start;
|
|
366
|
+
const end = (_b = node === null || node === void 0 ? void 0 : node.loc) === null || _b === void 0 ? void 0 : _b.end;
|
|
367
|
+
const startIndex = start !== undefined && 'index' in start && typeof start['index'] === 'number'
|
|
368
|
+
? start['index']
|
|
369
|
+
: undefined;
|
|
370
|
+
const endIndex = end !== undefined && 'index' in end && typeof end['index'] === 'number'
|
|
371
|
+
? end['index']
|
|
372
|
+
: undefined;
|
|
373
|
+
if (startIndex === undefined || endIndex === undefined || node === null) {
|
|
374
|
+
const err = `bad value: ${node}`;
|
|
375
|
+
return [null, err];
|
|
376
|
+
}
|
|
377
|
+
const value = input.slice(startIndex - 1, endIndex - 1);
|
|
378
|
+
return [value, null];
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* Maps and styles that are bound with dynamic values onto their respective
|
|
382
|
+
* binding keys for Builder elements. This function also maps media queries
|
|
383
|
+
* with dynamic values.
|
|
384
|
+
* @param - bindings - The bindings object that has your styles. This param
|
|
385
|
+
* will be modified in-place, and the old "style" key will be removed.
|
|
386
|
+
*/
|
|
387
|
+
const mapBoundStyles = (bindings) => {
|
|
388
|
+
const styles = bindings['style'];
|
|
389
|
+
if (!styles) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const { parsed } = parseJSObject(styles.code);
|
|
393
|
+
for (const key in parsed) {
|
|
394
|
+
const mediaQueryMatch = key.match(media_sizes_1.mediaQueryRegex);
|
|
395
|
+
if (mediaQueryMatch) {
|
|
396
|
+
const { parsed: mParsed } = parseJSObject(parsed[key]);
|
|
397
|
+
const [_, pixelSize] = mediaQueryMatch;
|
|
398
|
+
const size = media_sizes_1.sizes.getSizeForWidth(Number(pixelSize));
|
|
399
|
+
for (const mKey in mParsed) {
|
|
400
|
+
bindings[`responsiveStyles.${size}.${mKey}`] = {
|
|
401
|
+
code: mParsed[mKey],
|
|
402
|
+
bindingType: 'expression',
|
|
403
|
+
type: 'single',
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
bindings[`style.${key}`] = {
|
|
409
|
+
code: parsed[key],
|
|
410
|
+
bindingType: 'expression',
|
|
411
|
+
type: 'single',
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
delete bindings['style'];
|
|
416
|
+
};
|
|
288
417
|
const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
|
|
289
418
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
290
419
|
const mapper = !_internalOptions.skipMapper && componentMappers[json.name];
|
|
@@ -334,6 +463,9 @@ const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
|
|
|
334
463
|
actionBody;
|
|
335
464
|
delete bindings[key];
|
|
336
465
|
}
|
|
466
|
+
if (key === 'style') {
|
|
467
|
+
mapBoundStyles(bindings);
|
|
468
|
+
}
|
|
337
469
|
}
|
|
338
470
|
const builderBindings = {};
|
|
339
471
|
const componentOptions = omitMetaProperties(json.properties);
|