@builder.io/mitosis 0.11.6 → 0.12.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.
@@ -1 +1 @@
1
- export declare const tryFormat: (str: string, parser: string) => string;
1
+ export declare const tryFormat: (str: string, parser: string, htmlWhitespaceSensitivity?: 'css' | 'strict' | 'ignore') => string;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.tryFormat = void 0;
4
4
  const standalone_1 = require("prettier/standalone");
5
- const tryFormat = (str, parser) => {
5
+ const tryFormat = (str, parser, htmlWhitespaceSensitivity = 'css') => {
6
6
  try {
7
7
  return (0, standalone_1.format)(str, {
8
8
  parser,
@@ -13,7 +13,7 @@ const tryFormat = (str, parser) => {
13
13
  require('prettier/parser-html'),
14
14
  require('prettier/parser-babel'),
15
15
  ],
16
- htmlWhitespaceSensitivity: 'ignore',
16
+ htmlWhitespaceSensitivity,
17
17
  });
18
18
  }
19
19
  catch (err) {
@@ -6,13 +6,13 @@ const format_1 = require("../../../generators/angular/helpers/format");
6
6
  const get_outputs_1 = require("../../../generators/angular/helpers/get-outputs");
7
7
  const get_refs_1 = require("../../../generators/angular/helpers/get-refs");
8
8
  const get_styles_1 = require("../../../generators/angular/helpers/get-styles");
9
+ const hooks_1 = require("../../../generators/angular/helpers/hooks");
9
10
  const blocks_1 = require("../../../generators/angular/signals/blocks");
10
11
  const helpers_2 = require("../../../generators/angular/signals/helpers");
11
12
  const get_computed_1 = require("../../../generators/angular/signals/helpers/get-computed");
12
13
  const get_inputs_1 = require("../../../generators/angular/signals/helpers/get-inputs");
13
14
  const get_code_processor_plugins_1 = require("../../../generators/angular/signals/plugins/get-code-processor-plugins");
14
15
  const types_1 = require("../../../generators/angular/types");
15
- const on_mount_1 = require("../../../generators/helpers/on-mount");
16
16
  const dedent_1 = require("../../../helpers/dedent");
17
17
  const event_handlers_1 = require("../../../helpers/event-handlers");
18
18
  const fast_clone_1 = require("../../../helpers/fast-clone");
@@ -97,15 +97,6 @@ const componentToAngularSignals = (userOptions = {}) => {
97
97
  if (shouldUseSanitizer) {
98
98
  injectables.push('protected sanitizer: DomSanitizer');
99
99
  }
100
- if (json.hooks.onMount.length > 0) {
101
- json.compileContext.angular.hooks.ngAfterViewInit = {
102
- code: `
103
- if (typeof window !== 'undefined') {
104
- ${(0, on_mount_1.stringifySingleScopeOnMount)(json)}
105
- }
106
- `,
107
- };
108
- }
109
100
  // HTML
110
101
  let template = json.children
111
102
  .map((item) => {
@@ -125,7 +116,8 @@ const componentToAngularSignals = (userOptions = {}) => {
125
116
  })
126
117
  .join('\n');
127
118
  if (options.prettier !== false) {
128
- template = (0, format_1.tryFormat)(template, 'html');
119
+ // We need to use 'strict' mode for Angular otherwise it could add spaces around some content
120
+ template = (0, format_1.tryFormat)(template, 'html', 'strict');
129
121
  }
130
122
  const { components: dynamicComponents, dynamicTemplate } = (0, helpers_1.traverseToGetAllDynamicComponents)(json, options, {
131
123
  childComponents,
@@ -215,6 +207,10 @@ Please add a initial value for every state property even if it's \`undefined\`.`
215
207
  domRefs.size !== 0 ||
216
208
  ((_y = (_x = (_w = (_v = json.compileContext) === null || _v === void 0 ? void 0 : _v.angular) === null || _w === void 0 ? void 0 : _w.extra) === null || _x === void 0 ? void 0 : _x.spreadRefs) === null || _y === void 0 ? void 0 : _y.length) > 0;
217
209
  // Imports
210
+ const emptyOnMount = (0, is_hook_empty_1.isHookEmpty)(json.hooks.onMount);
211
+ const emptyOnUnMount = (0, is_hook_empty_1.isHookEmpty)(json.hooks.onUnMount);
212
+ const AfterViewInit = Boolean(!emptyOnMount || withAttributePassing);
213
+ const OnDestroy = !emptyOnUnMount;
218
214
  const coreImports = (0, helpers_2.getAngularCoreImportsAsString)({
219
215
  refs: domRefs.size !== 0,
220
216
  input: props.length !== 0,
@@ -224,11 +220,25 @@ Please add a initial value for every state property even if it's \`undefined\`.`
224
220
  signal: dataString.length !== 0 || hasDynamicComponents,
225
221
  computed: gettersString.length !== 0,
226
222
  onPush,
223
+ AfterViewInit,
224
+ OnDestroy,
227
225
  viewChild: importsViewChild,
228
226
  viewContainerRef: hasDynamicComponents,
229
227
  templateRef: hasDynamicComponents,
230
228
  renderer: usesRenderer2,
231
229
  });
230
+ // Hooks
231
+ if (!emptyOnMount) {
232
+ (0, hooks_1.addCodeNgAfterViewInit)(json, json.hooks.onMount.map((onMount) => onMount.code).join('\n'));
233
+ }
234
+ // Angular interfaces
235
+ const angularInterfaces = [];
236
+ if (AfterViewInit) {
237
+ angularInterfaces.push('AfterViewInit');
238
+ }
239
+ if (OnDestroy) {
240
+ angularInterfaces.push('OnDestroy');
241
+ }
232
242
  let str = (0, dedent_1.dedent) `
233
243
  import { ${coreImports} } from '@angular/core';
234
244
  import { CommonModule } from '@angular/common';
@@ -260,7 +270,7 @@ Please add a initial value for every state property even if it's \`undefined\`.`
260
270
  .map(([k, v]) => `${k}: ${v}`)
261
271
  .join(',')}
262
272
  })
263
- export ${options.defaultExportComponents ? 'default ' : ''}class ${json.name} {
273
+ export ${options.defaultExportComponents ? 'default ' : ''}class ${json.name} ${angularInterfaces.length ? ` implements ${angularInterfaces.join(',')}` : ''} {
264
274
  ${(0, lodash_1.uniq)(json.compileContext.angular.extra.importCalls)
265
275
  .map((importCall) => `protected readonly ${importCall} = ${importCall};`)
266
276
  .join('\n')}
@@ -336,17 +346,19 @@ Please add a initial value for every state property even if it's \`undefined\`.`
336
346
  .filter(([_, value]) => !(0, is_hook_empty_1.isHookEmpty)(value))
337
347
  .map(([key, value]) => {
338
348
  return `${key}() {
349
+ if (typeof window !== 'undefined') {
339
350
  ${value.code}
351
+ }
340
352
  }`;
341
353
  })
342
354
  .join('\n')
343
355
  : ''}
344
356
 
345
- ${json.hooks.onUnMount
346
- ? `ngOnDestroy() {
357
+ ${emptyOnUnMount
358
+ ? ''
359
+ : `ngOnDestroy() {
347
360
  ${((_10 = json.hooks.onUnMount) === null || _10 === void 0 ? void 0 : _10.code) || ''}
348
- }`
349
- : ''}
361
+ }`}
350
362
 
351
363
  }
352
364
  `;
@@ -1,9 +1,11 @@
1
- export declare const getAngularCoreImportsAsString: ({ refs, output, input, model, onPush, effect, signal, computed, viewChild, viewContainerRef, templateRef, renderer, }: {
1
+ export declare const getAngularCoreImportsAsString: ({ refs, output, input, model, onPush, effect, signal, AfterViewInit, OnDestroy, computed, viewChild, viewContainerRef, templateRef, renderer, }: {
2
2
  refs: boolean;
3
3
  output: boolean;
4
4
  input: boolean;
5
5
  model: boolean;
6
6
  onPush: boolean;
7
+ AfterViewInit: boolean;
8
+ OnDestroy: boolean;
7
9
  effect: boolean;
8
10
  signal: boolean;
9
11
  computed: boolean;
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAngularCoreImportsAsString = void 0;
4
- const getAngularCoreImportsAsString = ({ refs, output, input, model, onPush, effect, signal, computed, viewChild, viewContainerRef, templateRef, renderer, }) => {
4
+ const getAngularCoreImportsAsString = ({ refs, output, input, model, onPush, effect, signal, AfterViewInit, OnDestroy, computed, viewChild, viewContainerRef, templateRef, renderer, }) => {
5
5
  const angularCoreImports = {
6
6
  Component: true,
7
7
  viewChild: refs || viewChild,
8
8
  ElementRef: refs || viewChild,
9
9
  ViewContainerRef: viewContainerRef,
10
10
  TemplateRef: templateRef,
11
- Renderer2: !!renderer,
11
+ Renderer2: renderer,
12
12
  model,
13
13
  output,
14
14
  input,
@@ -16,6 +16,8 @@ const getAngularCoreImportsAsString = ({ refs, output, input, model, onPush, eff
16
16
  signal,
17
17
  computed,
18
18
  ChangeDetectionStrategy: onPush,
19
+ OnDestroy,
20
+ AfterViewInit,
19
21
  InputSignal: input,
20
22
  ModelSignal: model,
21
23
  };
@@ -462,147 +462,6 @@ const componentMappers = {
462
462
  children: (block.children || []).map((child) => (0, exports.builderElementToMitosisNode)(updateBindings(child, 'state.$index', 'index'), options)),
463
463
  });
464
464
  },
465
- Text: (block, options) => {
466
- var _a, _b, _c;
467
- let css = getCssFromBlock(block);
468
- const styleString = (0, exports.getStyleStringFromBlock)(block, options);
469
- const actionBindings = getActionBindingsFromBlock(block, options);
470
- const localizedValues = {};
471
- const blockBindings = {
472
- ...mapBuilderBindingsToMitosisBindingWithCode(block.bindings, options),
473
- ...mapBuilderBindingsToMitosisBindingWithCode((_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings, options),
474
- };
475
- const bindings = {
476
- ...(0, lodash_1.omitBy)(blockBindings, (value, key) => {
477
- if (key === 'component.options.text') {
478
- return true;
479
- }
480
- if (key && key.includes('style')) {
481
- return true;
482
- }
483
- return false;
484
- }),
485
- ...actionBindings,
486
- ...(styleString && {
487
- style: { code: styleString },
488
- }),
489
- ...(Object.keys(css).length && {
490
- css: { code: JSON.stringify(css) },
491
- }),
492
- };
493
- const properties = { ...block.properties };
494
- for (const key in properties) {
495
- if (typeof properties[key] === 'object' &&
496
- properties[key] !== null &&
497
- properties[key]['@type'] === '@builder.io/core:LocalizedValue') {
498
- const localizedValue = properties[key];
499
- localizedValues[`properties.${key}`] = localizedValue;
500
- properties[key] = localizedValue.Default;
501
- }
502
- }
503
- if (options.includeBuilderExtras && block.id)
504
- properties['builder-id'] = block.id;
505
- if (block.class)
506
- properties['class'] = block.class;
507
- if (block.layerName) {
508
- properties.$name = block.layerName;
509
- }
510
- // Add data attributes for Builder layer properties
511
- const dataAttributes = {};
512
- if (block.layerLocked !== undefined) {
513
- dataAttributes['data-builder-layerLocked'] = String(block.layerLocked);
514
- }
515
- if (block.groupLocked !== undefined) {
516
- dataAttributes['data-builder-groupLocked'] = String(block.groupLocked);
517
- }
518
- const innerBindings = {};
519
- const componentOptionsText = blockBindings['component.options.text'];
520
- if (componentOptionsText) {
521
- innerBindings[options.preserveTextBlocks ? 'innerHTML' : '_text'] = (0, bindings_1.createSingleBinding)({
522
- code: wrapBindingIfNeeded(componentOptionsText.code, options),
523
- });
524
- }
525
- let text = ((_b = block.component.options) === null || _b === void 0 ? void 0 : _b.text) || '';
526
- if (typeof text === 'object' &&
527
- text !== null &&
528
- text['@type'] === '@builder.io/core:LocalizedValue') {
529
- localizedValues['component.options.text'] = (_c = block.component.options) === null || _c === void 0 ? void 0 : _c.text;
530
- text = text.Default;
531
- }
532
- // Builder uses {{}} for bindings, but Mitosis expects {} so we need to convert
533
- const innerProperties = innerBindings._text
534
- ? {}
535
- : {
536
- [options.preserveTextBlocks ? 'innerHTML' : '_text']: text.replace(/\{\{(.*?)\}\}/g, '{$1}'),
537
- };
538
- if (options.preserveTextBlocks) {
539
- return (0, create_mitosis_node_1.createMitosisNode)({
540
- name: block.tagName || 'div',
541
- bindings,
542
- properties: {
543
- ...properties,
544
- ...dataAttributes,
545
- },
546
- meta: (0, exports.getMetaFromBlock)(block, options),
547
- ...(Object.keys(localizedValues).length && { localizedValues }),
548
- children: [
549
- (0, create_mitosis_node_1.createMitosisNode)({
550
- bindings: innerBindings,
551
- properties: {
552
- ...innerProperties,
553
- class: 'builder-text',
554
- },
555
- ...(Object.keys(localizedValues).length && { localizedValues }),
556
- }),
557
- ],
558
- });
559
- }
560
- // Disabling for now
561
- const assumeLink = false;
562
- const finalProperties = {
563
- ...(assumeLink
564
- ? {
565
- href: '...',
566
- }
567
- : {}),
568
- ...properties,
569
- ...dataAttributes,
570
- };
571
- const finalTagname = block.tagName || (assumeLink ? 'a' : 'div');
572
- if ((block.tagName && block.tagName !== 'div') ||
573
- hasStyles(block) ||
574
- hasComponent(block) ||
575
- hasBindings(block) ||
576
- hasProperties(block)) {
577
- return (0, create_mitosis_node_1.createMitosisNode)({
578
- name: finalTagname,
579
- bindings,
580
- properties: finalProperties,
581
- meta: (0, exports.getMetaFromBlock)(block, options),
582
- children: [
583
- (0, create_mitosis_node_1.createMitosisNode)({
584
- bindings: innerBindings,
585
- properties: innerProperties,
586
- ...(Object.keys(localizedValues).length && { localizedValues }),
587
- }),
588
- ],
589
- });
590
- }
591
- return (0, create_mitosis_node_1.createMitosisNode)({
592
- name: finalTagname,
593
- properties: {
594
- ...finalProperties,
595
- ...properties,
596
- ...innerProperties,
597
- },
598
- bindings: {
599
- ...bindings,
600
- ...innerBindings,
601
- },
602
- meta: (0, exports.getMetaFromBlock)(block, options),
603
- ...(Object.keys(localizedValues).length && { localizedValues }),
604
- });
605
- },
606
465
  };
607
466
  const processBoundLogic = (code) => {
608
467
  const ast = babel.parse(code);
@@ -625,7 +484,7 @@ const processBoundLogic = (code) => {
625
484
  return code;
626
485
  };
627
486
  const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
628
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
487
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
629
488
  const { includeSpecialBindings = true } = options;
630
489
  const localizedValues = {};
631
490
  if (((_a = block.component) === null || _a === void 0 ? void 0 : _a.name) === 'Core:Fragment') {
@@ -904,33 +763,6 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
904
763
  },
905
764
  ...(Object.keys(localizedValues).length && { localizedValues }),
906
765
  });
907
- // Has single text node child
908
- const firstChild = (_w = block.children) === null || _w === void 0 ? void 0 : _w[0];
909
- if (((_x = block.children) === null || _x === void 0 ? void 0 : _x.length) === 1 &&
910
- ((_y = firstChild === null || firstChild === void 0 ? void 0 : firstChild.component) === null || _y === void 0 ? void 0 : _y.name) === 'Text' &&
911
- !options.preserveTextBlocks) {
912
- const textProperties = (0, exports.builderElementToMitosisNode)(firstChild, options);
913
- const parsedNodeCss = json5_1.default.parse(((_z = node.bindings.css) === null || _z === void 0 ? void 0 : _z.code) || '{}');
914
- const parsedTextCss = json5_1.default.parse(((_0 = textProperties.bindings.css) === null || _0 === void 0 ? void 0 : _0.code) || '{}');
915
- const mergedCss = combineStyles(parsedNodeCss, parsedTextCss);
916
- // Don't merge if text has styling that matters
917
- const doNotMerge =
918
- // Text has flex alignment
919
- ['end', 'right', 'center'].includes(parsedTextCss.alignSelf) ||
920
- // Text has specific styling
921
- parsedTextCss.backgroundColor ||
922
- parsedTextCss.opacity ||
923
- parsedTextCss.background;
924
- if (!doNotMerge) {
925
- return (0, lodash_1.merge)({}, textProperties, node, {
926
- bindings: {
927
- ...(Object.keys(mergedCss).length && {
928
- css: { code: json5_1.default.stringify(mergedCss) },
929
- }),
930
- },
931
- });
932
- }
933
- }
934
766
  node.children = children.concat((block.children || []).map((item) => (0, exports.builderElementToMitosisNode)(item, options)));
935
767
  return node;
936
768
  };
@@ -492,6 +492,52 @@ exports.components = {
492
492
  });
493
493
  return wrapOutput(node, videoContainer, components);
494
494
  },
495
+ Text: (node) => {
496
+ if (node.properties.text) {
497
+ node.properties._text = node.properties.text;
498
+ delete node.properties.text;
499
+ }
500
+ if (node.bindings.text) {
501
+ node.bindings._text = node.bindings.text;
502
+ delete node.bindings.text;
503
+ }
504
+ const { _text: propText, $tagName: nodeTagName, ...outerProps } = node.properties;
505
+ const { _text: bindingText, ...outerBindings } = node.bindings;
506
+ const hasBindings = Object.keys(outerProps).length > 0;
507
+ const hasProperties = Object.keys(outerBindings).length > 0;
508
+ /**
509
+ * If there are things we need to reflect on the text then we must
510
+ * render a wrapper div so we can put it on that element.
511
+ */
512
+ if (hasBindings || hasProperties) {
513
+ /**
514
+ * Text binding needs to be on the inner text node. Some generators
515
+ * will skip rendering other bindings if they see a _text binding,
516
+ * so the _text binding needs to go on the inner text node, and all
517
+ * other bindings need to go on the wrapper div so they get generated.
518
+ */
519
+ return (0, create_mitosis_node_1.createMitosisNode)({
520
+ name: 'div',
521
+ bindings: outerBindings,
522
+ properties: outerProps,
523
+ children: [
524
+ (0, create_mitosis_node_1.createMitosisNode)({
525
+ ...node,
526
+ properties: {
527
+ $tagName: nodeTagName,
528
+ ...(propText ? { _text: propText } : {}),
529
+ },
530
+ bindings: bindingText ? { _text: bindingText } : {},
531
+ name: nodeTagName !== null && nodeTagName !== void 0 ? nodeTagName : 'div',
532
+ }),
533
+ ],
534
+ });
535
+ }
536
+ return (0, create_mitosis_node_1.createMitosisNode)({
537
+ ...node,
538
+ name: nodeTagName !== null && nodeTagName !== void 0 ? nodeTagName : 'div',
539
+ });
540
+ },
495
541
  };
496
542
  const compileAwayBuilderComponentsFromTree = (tree, components) => {
497
543
  (0, legacy_1.default)(tree).forEach(function (item) {
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.11.6",
25
+ "version": "0.12.1",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {