@builder.io/mitosis 0.5.27 → 0.5.29

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.
@@ -5,6 +5,7 @@ const on_mount_1 = require("../../generators/helpers/on-mount");
5
5
  const blocks_1 = require("../../generators/stencil/blocks");
6
6
  const helpers_1 = require("../../generators/stencil/helpers");
7
7
  const get_code_processor_plugins_1 = require("../../generators/stencil/plugins/get-code-processor-plugins");
8
+ const dash_case_1 = require("../../helpers/dash-case");
8
9
  const dedent_1 = require("../../helpers/dedent");
9
10
  const fast_clone_1 = require("../../helpers/fast-clone");
10
11
  const get_child_components_1 = require("../../helpers/get-child-components");
@@ -15,6 +16,7 @@ const map_refs_1 = require("../../helpers/map-refs");
15
16
  const merge_options_1 = require("../../helpers/merge-options");
16
17
  const strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
17
18
  const collect_css_1 = require("../../helpers/styles/collect-css");
19
+ const attribute_passing_1 = require("../../helpers/web-components/attribute-passing");
18
20
  const plugins_1 = require("../../modules/plugins");
19
21
  const standalone_1 = require("prettier/standalone");
20
22
  const componentToStencil = (_options = {
@@ -55,15 +57,17 @@ const componentToStencil = (_options = {
55
57
  functions: true,
56
58
  getters: true,
57
59
  });
58
- const refs = json.refs
60
+ let refs = json.refs
59
61
  ? Object.entries(json.refs)
60
- .map(([key, value]) => {
61
- var _a;
62
- return `private ${key}!: ${(_a = value.typeParameter) !== null && _a !== void 0 ? _a : 'HTMLElement'}`;
63
- })
62
+ .map(([key, value]) => { var _a; return `private ${key}!: ${(_a = value.typeParameter) !== null && _a !== void 0 ? _a : 'HTMLElement'};`; })
64
63
  .join('\n')
65
64
  : '';
66
65
  const wrap = (0, helpers_1.needsWrap)(json.children);
66
+ const withAttributePassing = !wrap && (0, attribute_passing_1.shouldAddAttributePassing)(json, options);
67
+ const rootRef = (0, attribute_passing_1.getAddAttributePassingRef)(json, options);
68
+ if (withAttributePassing && !refs.includes(rootRef)) {
69
+ refs += `\nprivate ${rootRef}!: HTMLElement;`;
70
+ }
67
71
  if (options.prettier !== false) {
68
72
  try {
69
73
  css = (0, standalone_1.format)(css, {
@@ -101,10 +105,15 @@ const componentToStencil = (_options = {
101
105
  ${(0, helpers_1.getPropsAsCode)(props, defaultProps, json.propsTypeRef)}
102
106
  ${dataString}
103
107
  ${methodsString}
108
+
109
+ ${withAttributePassing ? (0, attribute_passing_1.getAttributePassingString)(true) : ''}
104
110
 
105
- ${!json.hooks.onMount.length
106
- ? ''
107
- : `componentDidLoad() { ${(0, on_mount_1.stringifySingleScopeOnMount)(json)} }`}
111
+ ${`componentDidLoad() {
112
+ ${withAttributePassing
113
+ ? `this.enableAttributePassing(this.${rootRef}, "${(0, dash_case_1.dashCase)(json.name)}");`
114
+ : ''}
115
+ ${json.hooks.onMount.length ? (0, on_mount_1.stringifySingleScopeOnMount)(json) : ''}
116
+ }`}
108
117
  ${!((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code)
109
118
  ? ''
110
119
  : `disconnectedCallback() { ${json.hooks.onUnMount.code} }`}
@@ -116,7 +125,13 @@ const componentToStencil = (_options = {
116
125
  return (${wrap ? '<Host>' : ''}
117
126
 
118
127
  ${json.children
119
- .map((item) => (0, blocks_1.blockToStencil)(item, options, true, childComponents))
128
+ .map((item) => (0, blocks_1.blockToStencil)({
129
+ json: item,
130
+ options,
131
+ insideJsx: true,
132
+ childComponents,
133
+ rootRef: withAttributePassing && rootRef === attribute_passing_1.ROOT_REF ? rootRef : undefined, // only pass rootRef if it's not the default
134
+ }))
120
135
  .join('\n')}
121
136
 
122
137
  ${wrap ? '</Host>' : ''})
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Minimal implementation of lodash's _.set
3
+ * https://lodash.com/docs/4.17.15#set
4
+ *
5
+ * See ./set.test.ts for usage examples
6
+ */
7
+ export declare const set: (obj: any, _path: string | string[], value: any) => any;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.set = void 0;
4
+ /**
5
+ * Minimal implementation of lodash's _.set
6
+ * https://lodash.com/docs/4.17.15#set
7
+ *
8
+ * See ./set.test.ts for usage examples
9
+ */
10
+ const set = (obj, _path, value) => {
11
+ if (Object(obj) !== obj) {
12
+ return obj;
13
+ }
14
+ const path = Array.isArray(_path)
15
+ ? _path
16
+ : _path.toString().match(/[^.[\]]+/g);
17
+ path
18
+ .slice(0, -1)
19
+ .reduce((a, c, i) => Object(a[c]) === a[c]
20
+ ? a[c]
21
+ : (a[c] = Math.abs(Number(path[i + 1])) >> 0 === +path[i + 1] ? [] : {}), obj)[path[path.length - 1]] = value;
22
+ return obj;
23
+ };
24
+ exports.set = set;
@@ -0,0 +1,6 @@
1
+ import { MitosisComponent } from '../../types/mitosis-component';
2
+ import { BaseTranspilerOptions } from '../../types/transpiler';
3
+ export declare const ROOT_REF = "_root";
4
+ export declare const getAttributePassingString: (typescript?: boolean) => string;
5
+ export declare const shouldAddAttributePassing: (json: MitosisComponent, options: BaseTranspilerOptions) => boolean | undefined;
6
+ export declare const getAddAttributePassingRef: (json: MitosisComponent, options: BaseTranspilerOptions) => string;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAddAttributePassingRef = exports.shouldAddAttributePassing = exports.getAttributePassingString = exports.ROOT_REF = void 0;
4
+ exports.ROOT_REF = '_root';
5
+ const getAttributePassingString = (typescript) => {
6
+ return ('/**\n' +
7
+ ' * Passes `aria-*`, `data-*` & `class` attributes to correct child. Used in angular and stencil.\n' +
8
+ ' * @param element the ref for the component\n' +
9
+ ' * @param customElementSelector the custom element like `my-component`\n' +
10
+ ' */\n' +
11
+ `private enableAttributePassing(element${typescript ? ': HTMLElement | null' : ''}, customElementSelector${typescript ? ': string' : ''}) {
12
+ ` +
13
+ ' const parent = element?.closest(customElementSelector);\n' +
14
+ ' if (element && parent) {\n' +
15
+ ' const attributes = parent.attributes;\n' +
16
+ ' for (let i = 0; i < attributes.length; i++) {\n' +
17
+ ' const attr = attributes.item(i);\n' +
18
+ " if (attr && (attr.name.startsWith('data-') || attr.name.startsWith('aria-'))) {\n" +
19
+ ' element.setAttribute(attr.name, attr.value);\n' +
20
+ ' parent.removeAttribute(attr.name);\n' +
21
+ ' }\n' +
22
+ " if (attr && attr.name === 'class') {\n" +
23
+ " const isWebComponent = attr.value.includes('hydrated');\n" +
24
+ " const value = attr.value.replace('hydrated', '').trim();\n" +
25
+ " const currentClass = element.getAttribute('class');\n" +
26
+ " element.setAttribute(attr.name, `${currentClass ? `${currentClass} ` : ''}${value}`);\n" +
27
+ ' if (isWebComponent) {\n' +
28
+ ' // Stencil is using this class for lazy loading component\n' +
29
+ " parent.setAttribute('class', 'hydrated');\n" +
30
+ ' } else {\n' +
31
+ ' parent.removeAttribute(attr.name);\n' +
32
+ ' }\n' +
33
+ ' }\n' +
34
+ ' }\n' +
35
+ ' }\n' +
36
+ '};');
37
+ };
38
+ exports.getAttributePassingString = getAttributePassingString;
39
+ const shouldAddAttributePassing = (json, options) => {
40
+ var _a, _b, _c, _d;
41
+ const metaAttributePassingAsString = String((_b = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.attributePassing) === null || _b === void 0 ? void 0 : _b.enabled);
42
+ if (((_c = options.attributePassing) === null || _c === void 0 ? void 0 : _c.enabled) && metaAttributePassingAsString !== 'false') {
43
+ return false;
44
+ }
45
+ if (metaAttributePassingAsString === 'true') {
46
+ return true;
47
+ }
48
+ return (_d = options.attributePassing) === null || _d === void 0 ? void 0 : _d.enabled;
49
+ };
50
+ exports.shouldAddAttributePassing = shouldAddAttributePassing;
51
+ const getAddAttributePassingRef = (json, options) => {
52
+ var _a, _b, _c;
53
+ return (((_b = (_a = json.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.attributePassing) === null || _b === void 0 ? void 0 : _b.customRef) ||
54
+ ((_c = options.attributePassing) === null || _c === void 0 ? void 0 : _c.customRef) ||
55
+ exports.ROOT_REF);
56
+ };
57
+ exports.getAddAttributePassingRef = getAddAttributePassingRef;
@@ -444,10 +444,11 @@ const componentMappers = {
444
444
  });
445
445
  },
446
446
  Text: (block, options) => {
447
- var _a;
447
+ var _a, _b, _c;
448
448
  let css = getCssFromBlock(block);
449
449
  const styleString = getStyleStringFromBlock(block, options);
450
450
  const actionBindings = getActionBindingsFromBlock(block, options);
451
+ const localizedValues = {};
451
452
  const blockBindings = {
452
453
  ...mapBuilderBindingsToMitosisBindingWithCode(block.bindings),
453
454
  ...mapBuilderBindingsToMitosisBindingWithCode((_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings),
@@ -471,6 +472,15 @@ const componentMappers = {
471
472
  }),
472
473
  };
473
474
  const properties = { ...block.properties };
475
+ for (const key in properties) {
476
+ if (typeof properties[key] === 'object' &&
477
+ properties[key] !== null &&
478
+ properties[key]['@type'] === '@builder.io/core:LocalizedValue') {
479
+ const localizedValue = properties[key];
480
+ localizedValues[`properties.${key}`] = localizedValue;
481
+ properties[key] = localizedValue.Default;
482
+ }
483
+ }
474
484
  if (options.includeBuilderExtras && block.id)
475
485
  properties['builder-id'] = block.id;
476
486
  if (block.class)
@@ -485,7 +495,13 @@ const componentMappers = {
485
495
  code: wrapBindingIfNeeded(componentOptionsText.code, options),
486
496
  });
487
497
  }
488
- const text = block.component.options.text;
498
+ let text = ((_b = block.component.options) === null || _b === void 0 ? void 0 : _b.text) || '';
499
+ if (typeof text === 'object' &&
500
+ text !== null &&
501
+ text['@type'] === '@builder.io/core:LocalizedValue') {
502
+ localizedValues['component.options.text'] = (_c = block.component.options) === null || _c === void 0 ? void 0 : _c.text;
503
+ text = text.Default;
504
+ }
489
505
  // Builder uses {{}} for bindings, but Mitosis expects {} so we need to convert
490
506
  const innerProperties = innerBindings._text
491
507
  ? {}
@@ -498,6 +514,7 @@ const componentMappers = {
498
514
  bindings,
499
515
  properties,
500
516
  meta: (0, exports.getMetaFromBlock)(block, options),
517
+ ...(Object.keys(localizedValues).length && { localizedValues }),
501
518
  children: [
502
519
  (0, create_mitosis_node_1.createMitosisNode)({
503
520
  bindings: innerBindings,
@@ -505,6 +522,7 @@ const componentMappers = {
505
522
  ...innerProperties,
506
523
  class: 'builder-text',
507
524
  },
525
+ ...(Object.keys(localizedValues).length && { localizedValues }),
508
526
  }),
509
527
  ],
510
528
  });
@@ -534,6 +552,7 @@ const componentMappers = {
534
552
  (0, create_mitosis_node_1.createMitosisNode)({
535
553
  bindings: innerBindings,
536
554
  properties: innerProperties,
555
+ ...(Object.keys(localizedValues).length && { localizedValues }),
537
556
  }),
538
557
  ],
539
558
  });
@@ -550,12 +569,14 @@ const componentMappers = {
550
569
  ...innerBindings,
551
570
  },
552
571
  meta: (0, exports.getMetaFromBlock)(block, options),
572
+ ...(Object.keys(localizedValues).length && { localizedValues }),
553
573
  });
554
574
  },
555
575
  };
556
576
  const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
557
577
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
558
578
  const { includeSpecialBindings = true } = options;
579
+ const localizedValues = {};
559
580
  if (((_a = block.component) === null || _a === void 0 ? void 0 : _a.name) === 'Core:Fragment') {
560
581
  block.component.name = 'Fragment';
561
582
  }
@@ -671,11 +692,27 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
671
692
  }),
672
693
  ...(options.includeBuilderExtras && getBuilderPropsForSymbol(block)),
673
694
  };
695
+ for (const key in properties) {
696
+ if (typeof properties[key] === 'object' &&
697
+ properties[key] !== null &&
698
+ properties[key]['@type'] === '@builder.io/core:LocalizedValue') {
699
+ const localizedValue = properties[key];
700
+ localizedValues[`properties.${key}`] = localizedValue;
701
+ properties[key] = localizedValue.Default;
702
+ }
703
+ }
674
704
  if (block.layerName) {
675
705
  properties.$name = block.layerName;
676
706
  }
677
- if (block.linkUrl) {
678
- properties.href = block.linkUrl;
707
+ const linkUrl = block.linkUrl;
708
+ if (linkUrl) {
709
+ if (typeof linkUrl === 'object' && linkUrl['@type'] === '@builder.io/core:LocalizedValue') {
710
+ properties.href = linkUrl.Default;
711
+ localizedValues['linkUrl'] = linkUrl;
712
+ }
713
+ else {
714
+ properties.href = linkUrl;
715
+ }
679
716
  }
680
717
  if ((_o = block.component) === null || _o === void 0 ? void 0 : _o.options) {
681
718
  for (const key in block.component.options) {
@@ -694,6 +731,11 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
694
731
  else if (typeof value === 'string') {
695
732
  properties[key] = value;
696
733
  }
734
+ else if (typeof value === 'object' &&
735
+ value['@type'] === '@builder.io/core:LocalizedValue') {
736
+ properties[key] = value.Default;
737
+ localizedValues[`component.options.${key}`] = value;
738
+ }
697
739
  else if (valueIsArrayOfBuilderElements) {
698
740
  const childrenElements = value
699
741
  .filter((item) => {
@@ -748,6 +790,7 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
748
790
  ...slots,
749
791
  },
750
792
  meta: (0, exports.getMetaFromBlock)(block, options),
793
+ ...(Object.keys(localizedValues).length && { localizedValues }),
751
794
  });
752
795
  // Has single text node child
753
796
  const firstChild = (_r = block.children) === null || _r === void 0 ? void 0 : _r[0];
@@ -14,6 +14,9 @@ export declare function parseText(node: TemplateNode): {
14
14
  slots?: {
15
15
  [key: string]: import("../../..").MitosisNode[];
16
16
  } | undefined;
17
+ localizedValues?: {
18
+ [index: string]: import("../../..").BuilderLocalizedValue;
19
+ } | undefined;
17
20
  } | {
18
21
  name: string;
19
22
  properties: {
@@ -33,4 +36,7 @@ export declare function parseText(node: TemplateNode): {
33
36
  slots?: {
34
37
  [key: string]: import("../../..").MitosisNode[];
35
38
  } | undefined;
39
+ localizedValues?: {
40
+ [index: string]: import("../../..").BuilderLocalizedValue;
41
+ } | undefined;
36
42
  };
@@ -15,7 +15,7 @@ import { SvelteMetadata } from '../generators/svelte/types';
15
15
  import { SwiftMetadata } from '../generators/swift/types';
16
16
  import { TaroMetadata } from '../generators/taro/types';
17
17
  import { TemplateMetadata } from '../generators/template/types';
18
- import { ReactMetadata, Target, VueMetadata } from '..';
18
+ import { AttributePassingType, ReactMetadata, Target, VueMetadata } from '..';
19
19
  type Targets = typeof import('../targets').targets;
20
20
  type TargetOptions = {
21
21
  [K in Target]?: Partial<NonNullable<Parameters<Targets[K]>[0]>>;
@@ -30,6 +30,8 @@ export type ComponentMetadata = {
30
30
  forwardRef?: string;
31
31
  /** Enables shadowDom for web-components */
32
32
  isAttachedToShadowDom?: boolean;
33
+ /** Enables/disables attribute passing for frameworks with custom elements like angular and stencil */
34
+ attributePassing?: AttributePassingType;
33
35
  alpine?: AlpineMetadata;
34
36
  angular?: AngularMetadata;
35
37
  builder?: BuilderMetadata;
@@ -28,6 +28,11 @@ type BindingProperties = {
28
28
  export type Binding = {
29
29
  code: string;
30
30
  } & BindingProperties;
31
+ export type BuilderLocalizedValue = {
32
+ '@type': '@builder.io/core:LocalizedValue';
33
+ Default: string;
34
+ [index: string]: string;
35
+ };
31
36
  export type BaseNode = {
32
37
  '@type': '@builder.io/mitosis/node';
33
38
  meta: JSONObject;
@@ -68,6 +73,13 @@ export type BaseNode = {
68
73
  slots?: {
69
74
  [key: string]: MitosisNode[];
70
75
  };
76
+ /**
77
+ * Key-value store of localized values
78
+ * It is used when a Builder content block has localized values.
79
+ */
80
+ localizedValues?: {
81
+ [index: string]: BuilderLocalizedValue;
82
+ };
71
83
  };
72
84
  export type SpecialNodesNames = 'For' | 'Fragment' | 'Show' | 'Slot';
73
85
  export type ForNode = BaseNode & {
@@ -9,6 +9,10 @@ export type Transpiler<R = string> = (args: TranspilerArgs) => R;
9
9
  * This type guarantees that all code generators receive the same base options
10
10
  */
11
11
  export type TranspilerGenerator<X extends BaseTranspilerOptions, Y = string> = (args?: X) => Transpiler<Y>;
12
+ export type AttributePassingType = {
13
+ enabled: boolean;
14
+ customRef?: string;
15
+ };
12
16
  export interface BaseTranspilerOptions {
13
17
  experimental?: {
14
18
  [key: string]: any;
@@ -25,6 +29,8 @@ export interface BaseTranspilerOptions {
25
29
  * Enable `typescript` output
26
30
  */
27
31
  typescript?: boolean;
32
+ /** Enables/disables attribute passing for frameworks with custom elements like angular and stencil */
33
+ attributePassing?: AttributePassingType;
28
34
  /**
29
35
  * Preserves explicit filename extensions in import statements.
30
36
  */
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.5.27",
25
+ "version": "0.5.29",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {