@builder.io/mitosis 0.5.38 → 0.6.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.
|
@@ -189,6 +189,11 @@ const stringifyBinding = (node, options, blockOptions) => ([key, binding]) => {
|
|
|
189
189
|
// standard html elements need the attr to satisfy the compiler in many cases: eg: svg elements and [fill]
|
|
190
190
|
return ` [attr.${keyToUse}]="${code}" `;
|
|
191
191
|
}
|
|
192
|
+
else if (keyToUse === 'innerHTML') {
|
|
193
|
+
return blockOptions.sanitizeInnerHTML
|
|
194
|
+
? ` [innerHTML]="${code}" `
|
|
195
|
+
: ` [innerHTML]="sanitizer.bypassSecurityTrustHtml(${code})" `;
|
|
196
|
+
}
|
|
192
197
|
else {
|
|
193
198
|
const codeToUse = options.state === 'inline-with-wrappers' ? processCodeBlockInTemplate(code) : code;
|
|
194
199
|
return `[${keyToUse}]="${codeToUse}"`;
|
|
@@ -113,6 +113,20 @@ const traverseToGetAllDynamicComponents = (json, options, blockOptions) => {
|
|
|
113
113
|
dynamicTemplate,
|
|
114
114
|
};
|
|
115
115
|
};
|
|
116
|
+
const traverseAndCheckIfInnerHTMLIsUsed = (json) => {
|
|
117
|
+
let innerHTMLIsUsed = false;
|
|
118
|
+
(0, legacy_1.default)(json).forEach((item) => {
|
|
119
|
+
if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
|
|
120
|
+
Object.keys(item.bindings).forEach((key) => {
|
|
121
|
+
if (key === 'innerHTML') {
|
|
122
|
+
innerHTMLIsUsed = true;
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return innerHTMLIsUsed;
|
|
129
|
+
};
|
|
116
130
|
/**
|
|
117
131
|
* Prefixes state identifiers with this.
|
|
118
132
|
* e.g. state.foo --> this.foo
|
|
@@ -264,7 +278,7 @@ const classPropertiesPlugin = () => ({
|
|
|
264
278
|
},
|
|
265
279
|
});
|
|
266
280
|
const componentToAngular = (userOptions = {}) => ({ component: _component }) => {
|
|
267
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
281
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
|
|
268
282
|
// Make a copy we can safely mutate, similar to babel's toolchain
|
|
269
283
|
let json = (0, fast_clone_1.fastClone)(_component);
|
|
270
284
|
const useMetadata = (_a = json.meta) === null || _a === void 0 ? void 0 : _a.useMetadata;
|
|
@@ -397,6 +411,8 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
397
411
|
css = tryFormat(css, 'css');
|
|
398
412
|
}
|
|
399
413
|
const helperFunctions = new Set();
|
|
414
|
+
const shouldUseSanitizer = !((_c = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _c === void 0 ? void 0 : _c.sanitizeInnerHTML) && traverseAndCheckIfInnerHTMLIsUsed(json);
|
|
415
|
+
const changeDetectionStrategy = (_d = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _d === void 0 ? void 0 : _d.changeDetection;
|
|
400
416
|
let template = json.children
|
|
401
417
|
.map((item) => {
|
|
402
418
|
var _a, _b, _c, _d;
|
|
@@ -409,6 +425,7 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
409
425
|
childComponents,
|
|
410
426
|
nativeAttributes: (_b = (_a = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _a === void 0 ? void 0 : _a.nativeAttributes) !== null && _b !== void 0 ? _b : [],
|
|
411
427
|
nativeEvents: (_d = (_c = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _c === void 0 ? void 0 : _c.nativeEvents) !== null && _d !== void 0 ? _d : [],
|
|
428
|
+
sanitizeInnerHTML: !shouldUseSanitizer,
|
|
412
429
|
},
|
|
413
430
|
});
|
|
414
431
|
if (options.state === 'inline-with-wrappers') {
|
|
@@ -423,8 +440,8 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
423
440
|
(0, strip_meta_properties_1.stripMetaProperties)(json);
|
|
424
441
|
const { components: dynamicComponents, dynamicTemplate } = traverseToGetAllDynamicComponents(json, options, {
|
|
425
442
|
childComponents,
|
|
426
|
-
nativeAttributes: (
|
|
427
|
-
nativeEvents: (
|
|
443
|
+
nativeAttributes: (_f = (_e = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _e === void 0 ? void 0 : _e.nativeAttributes) !== null && _f !== void 0 ? _f : [],
|
|
444
|
+
nativeEvents: (_h = (_g = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _g === void 0 ? void 0 : _g.nativeEvents) !== null && _h !== void 0 ? _h : [],
|
|
428
445
|
});
|
|
429
446
|
(0, helpers_2.transformState)(json);
|
|
430
447
|
const dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
@@ -448,13 +465,18 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
448
465
|
const styles = css.length ? [hostDisplayCss, css].join('\n') : hostDisplayCss;
|
|
449
466
|
// Preparing built in component metadata parameters
|
|
450
467
|
const componentMetadata = {
|
|
451
|
-
selector: ((
|
|
452
|
-
? `'${(
|
|
468
|
+
selector: ((_j = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _j === void 0 ? void 0 : _j.selector)
|
|
469
|
+
? `'${(_k = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _k === void 0 ? void 0 : _k.selector}'`
|
|
453
470
|
: `'${(0, lodash_1.kebabCase)(json.name || 'my-component')}'`,
|
|
454
471
|
template: `\`
|
|
455
472
|
${(0, indent_1.indent)(dynamicTemplate, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${')}
|
|
456
473
|
${(0, indent_1.indent)(template, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${')}
|
|
457
474
|
\``,
|
|
475
|
+
...(changeDetectionStrategy === 'OnPush'
|
|
476
|
+
? {
|
|
477
|
+
changeDetection: 'ChangeDetectionStrategy.OnPush',
|
|
478
|
+
}
|
|
479
|
+
: {}),
|
|
458
480
|
...(styles
|
|
459
481
|
? {
|
|
460
482
|
styles: `[\`${(0, indent_1.indent)(styles, 8)}\`]`,
|
|
@@ -472,10 +494,13 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
472
494
|
Object.entries(json.meta.angularConfig || {}).forEach(([key, value]) => {
|
|
473
495
|
componentMetadata[key] = value;
|
|
474
496
|
});
|
|
475
|
-
const hasConstructor = Boolean(injectables.length) ||
|
|
497
|
+
const hasConstructor = Boolean(injectables.length) ||
|
|
498
|
+
dynamicComponents.size ||
|
|
499
|
+
refsForObjSpread.size ||
|
|
500
|
+
shouldUseSanitizer;
|
|
476
501
|
const angularCoreImports = [
|
|
477
502
|
...(outputs.length ? ['Output', 'EventEmitter'] : []),
|
|
478
|
-
...(((
|
|
503
|
+
...(((_l = options === null || options === void 0 ? void 0 : options.experimental) === null || _l === void 0 ? void 0 : _l.inject) ? ['Inject', 'forwardRef'] : []),
|
|
479
504
|
'Component',
|
|
480
505
|
...(domRefs.size || dynamicComponents.size || refsForObjSpread.size
|
|
481
506
|
? ['ViewChild', 'ElementRef']
|
|
@@ -483,10 +508,24 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
483
508
|
...(refsForObjSpread.size ? ['Renderer2'] : []),
|
|
484
509
|
...(props.size ? ['Input'] : []),
|
|
485
510
|
...(dynamicComponents.size ? ['ViewContainerRef', 'TemplateRef'] : []),
|
|
486
|
-
...(((
|
|
511
|
+
...(((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.length) && options.typescript ? ['SimpleChanges'] : []),
|
|
512
|
+
...(changeDetectionStrategy === 'OnPush' ? ['ChangeDetectionStrategy'] : []),
|
|
487
513
|
].join(', ');
|
|
514
|
+
const constructorInjectables = [
|
|
515
|
+
...injectables,
|
|
516
|
+
...(dynamicComponents.size
|
|
517
|
+
? [`private vcRef${options.typescript ? ': ViewContainerRef' : ''}`]
|
|
518
|
+
: []),
|
|
519
|
+
...(refsForObjSpread.size
|
|
520
|
+
? [`private renderer${options.typescript ? ': Renderer2' : ''}`]
|
|
521
|
+
: []),
|
|
522
|
+
...(shouldUseSanitizer
|
|
523
|
+
? [`protected sanitizer${options.typescript ? ': DomSanitizer' : ''}`]
|
|
524
|
+
: []),
|
|
525
|
+
].join(',\n');
|
|
488
526
|
let str = (0, dedent_1.dedent) `
|
|
489
527
|
import { ${angularCoreImports} } from '@angular/core';
|
|
528
|
+
${shouldUseSanitizer ? `import { DomSanitizer } from '@angular/platform-browser';` : ''}
|
|
490
529
|
${options.standalone ? `import { CommonModule } from '@angular/common';` : ''}
|
|
491
530
|
|
|
492
531
|
${json.types ? json.types.join('\n') : ''}
|
|
@@ -565,24 +604,17 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
565
604
|
})
|
|
566
605
|
.join('\n')}
|
|
567
606
|
|
|
568
|
-
${!hasConstructor
|
|
569
|
-
? ''
|
|
570
|
-
: `constructor(\n${injectables.join(',\n')}${dynamicComponents.size
|
|
571
|
-
? `\nprivate vcRef${options.typescript ? ': ViewContainerRef' : ''},\n`
|
|
572
|
-
: ''}${refsForObjSpread.size
|
|
573
|
-
? `\nprivate renderer${options.typescript ? ': Renderer2' : ''},\n`
|
|
574
|
-
: ''}) {}
|
|
575
|
-
`}
|
|
607
|
+
${!hasConstructor ? '' : `constructor(\n${constructorInjectables}) {}`}
|
|
576
608
|
|
|
577
609
|
${withAttributePassing ? (0, attribute_passing_1.getAttributePassingString)(options.typescript) : ''}
|
|
578
610
|
|
|
579
|
-
${!json.hooks.onMount.length && !dynamicComponents.size && !((
|
|
611
|
+
${!json.hooks.onMount.length && !dynamicComponents.size && !((_o = json.hooks.onInit) === null || _o === void 0 ? void 0 : _o.code)
|
|
580
612
|
? ''
|
|
581
613
|
: `ngOnInit() {
|
|
582
|
-
${!((
|
|
614
|
+
${!((_p = json.hooks) === null || _p === void 0 ? void 0 : _p.onInit)
|
|
583
615
|
? ''
|
|
584
616
|
: `
|
|
585
|
-
${(
|
|
617
|
+
${(_q = json.hooks.onInit) === null || _q === void 0 ? void 0 : _q.code}
|
|
586
618
|
`}
|
|
587
619
|
${json.hooks.onMount.length > 0
|
|
588
620
|
? `
|
|
@@ -604,19 +636,21 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
604
636
|
|
|
605
637
|
${
|
|
606
638
|
// hooks specific to Angular
|
|
607
|
-
((
|
|
608
|
-
? Object.entries((
|
|
639
|
+
((_s = (_r = json.compileContext) === null || _r === void 0 ? void 0 : _r.angular) === null || _s === void 0 ? void 0 : _s.hooks)
|
|
640
|
+
? Object.entries((_u = (_t = json.compileContext) === null || _t === void 0 ? void 0 : _t.angular) === null || _u === void 0 ? void 0 : _u.hooks)
|
|
641
|
+
.map(([key, value]) => {
|
|
609
642
|
return `${key}() {
|
|
610
643
|
${value.code}
|
|
611
644
|
}`;
|
|
612
645
|
})
|
|
646
|
+
.join('\n')
|
|
613
647
|
: ''}
|
|
614
648
|
|
|
615
|
-
${!((
|
|
649
|
+
${!((_v = json.hooks.onUpdate) === null || _v === void 0 ? void 0 : _v.length)
|
|
616
650
|
? ''
|
|
617
651
|
: `ngOnChanges(changes${options.typescript ? ': SimpleChanges' : ''}) {
|
|
618
652
|
if (typeof window !== 'undefined') {
|
|
619
|
-
${(
|
|
653
|
+
${(_w = json.hooks.onUpdate) === null || _w === void 0 ? void 0 : _w.reduce((code, hook) => {
|
|
620
654
|
code += hook.code;
|
|
621
655
|
return code + '\n';
|
|
622
656
|
}, '')}
|
|
@@ -627,7 +661,7 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
627
661
|
${!json.hooks.onUnMount && !refsForObjSpread.size
|
|
628
662
|
? ''
|
|
629
663
|
: `ngOnDestroy() {
|
|
630
|
-
${((
|
|
664
|
+
${((_x = json.hooks.onUnMount) === null || _x === void 0 ? void 0 : _x.code) || ''}
|
|
631
665
|
${refsForObjSpread.size
|
|
632
666
|
? `for (const fn of this._listenerFns.values()) { fn(); }`
|
|
633
667
|
: ''}
|
|
@@ -31,6 +31,15 @@ export type AngularMetadata = {
|
|
|
31
31
|
* Overwrite default selector for component. Default will be kebab case (MyComponent -> my-component)
|
|
32
32
|
*/
|
|
33
33
|
selector?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Overwrite default change detection strategy.
|
|
36
|
+
* `OnPush` adds `changeDetection: ChangeDetectionStrategy.OnPush` to the component metadata.
|
|
37
|
+
*/
|
|
38
|
+
changeDetection?: 'Default' | 'OnPush';
|
|
39
|
+
/**
|
|
40
|
+
* Overwrite default sanitizeInnerHTML. Default is `false`
|
|
41
|
+
*/
|
|
42
|
+
sanitizeInnerHTML?: boolean;
|
|
34
43
|
};
|
|
35
44
|
export type AngularBlockOptions = {
|
|
36
45
|
childComponents?: string[];
|
|
@@ -25,13 +25,17 @@ const collectStyles = (json, options = {}) => {
|
|
|
25
25
|
const componentIndexes = {};
|
|
26
26
|
const componentHashes = {};
|
|
27
27
|
(0, legacy_1.default)(json).forEach(function (item) {
|
|
28
|
-
var _a;
|
|
28
|
+
var _a, _b;
|
|
29
29
|
if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
|
|
30
30
|
if ((0, helpers_1.nodeHasCss)(item)) {
|
|
31
31
|
const value = (0, helpers_1.parseCssObject)((_a = item.bindings.css) === null || _a === void 0 ? void 0 : _a.code);
|
|
32
32
|
delete item.bindings.css;
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
// Clean the name by keeping only alphanumeric characters, underscores, and dashes
|
|
34
|
+
const cleanedName = (_b = item.properties.$name) === null || _b === void 0 ? void 0 : _b.replace(/[^a-zA-Z0-9_-]/g, '');
|
|
35
|
+
// Remove leading numbers or dashes
|
|
36
|
+
const normalizedName = cleanedName === null || cleanedName === void 0 ? void 0 : cleanedName.replace(/^[0-9-]+/, '');
|
|
37
|
+
const componentName = normalizedName
|
|
38
|
+
? (0, dash_case_1.dashCase)(normalizedName)
|
|
35
39
|
: /^h\d$/.test(item.name || '') // don't dashcase h1 into h-1
|
|
36
40
|
? item.name
|
|
37
41
|
: (0, dash_case_1.dashCase)(item.name || 'div');
|