@builder.io/mitosis 0.7.6 → 0.9.0

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.
Files changed (71) hide show
  1. package/dist/src/generators/angular/{blocks.d.ts → classic/blocks.d.ts} +3 -3
  2. package/dist/src/generators/angular/{blocks.js → classic/blocks.js} +14 -14
  3. package/dist/src/generators/angular/classic/component.d.ts +3 -0
  4. package/dist/src/generators/angular/classic/component.js +356 -0
  5. package/dist/src/generators/angular/classic/plugins/get-class-properties-plugin.d.ts +2 -0
  6. package/dist/src/generators/angular/classic/plugins/get-class-properties-plugin.js +138 -0
  7. package/dist/src/generators/angular/classic/plugins/get-code-processor-plugins.d.ts +8 -0
  8. package/dist/src/generators/angular/classic/plugins/get-code-processor-plugins.js +60 -0
  9. package/dist/src/generators/angular/component.d.ts +3 -0
  10. package/dist/src/generators/angular/component.js +27 -0
  11. package/dist/src/generators/angular/helpers/format.d.ts +1 -0
  12. package/dist/src/generators/angular/helpers/format.js +24 -0
  13. package/dist/src/generators/angular/helpers/get-inputs.d.ts +7 -0
  14. package/dist/src/generators/angular/helpers/get-inputs.js +18 -0
  15. package/dist/src/generators/angular/helpers/get-outputs.d.ts +7 -0
  16. package/dist/src/generators/angular/helpers/get-outputs.js +23 -0
  17. package/dist/src/generators/angular/helpers/get-refs.d.ts +8 -0
  18. package/dist/src/generators/angular/helpers/get-refs.js +40 -0
  19. package/dist/src/generators/angular/helpers/get-styles.d.ts +6 -0
  20. package/dist/src/generators/angular/helpers/get-styles.js +17 -0
  21. package/dist/src/generators/angular/helpers/index.d.ts +15 -0
  22. package/dist/src/generators/angular/helpers/index.js +126 -2
  23. package/dist/src/generators/angular/{parse-selector.d.ts → helpers/parse-selector.d.ts} +1 -1
  24. package/dist/src/generators/angular/{parse-selector.js → helpers/parse-selector.js} +3 -3
  25. package/dist/src/generators/angular/index.d.ts +1 -3
  26. package/dist/src/generators/angular/index.js +1 -692
  27. package/dist/src/generators/angular/signals/blocks.d.ts +10 -0
  28. package/dist/src/generators/angular/signals/blocks.js +242 -0
  29. package/dist/src/generators/angular/signals/component.d.ts +3 -0
  30. package/dist/src/generators/angular/signals/component.js +279 -0
  31. package/dist/src/generators/angular/signals/helpers/get-inputs.d.ts +7 -0
  32. package/dist/src/generators/angular/signals/helpers/get-inputs.js +15 -0
  33. package/dist/src/generators/angular/signals/helpers/index.d.ts +9 -0
  34. package/dist/src/generators/angular/signals/helpers/index.js +22 -0
  35. package/dist/src/generators/angular/signals/plugins/get-code-processor-plugins.d.ts +4 -0
  36. package/dist/src/generators/angular/signals/plugins/get-code-processor-plugins.js +194 -0
  37. package/dist/src/generators/angular/types.d.ts +23 -2
  38. package/dist/src/generators/angular/types.js +1 -0
  39. package/dist/src/generators/mitosis/generator.js +11 -27
  40. package/dist/src/generators/qwik/component-generator.js +1 -0
  41. package/dist/src/generators/solid/index.js +1 -0
  42. package/dist/src/generators/stencil/blocks.js +2 -1
  43. package/dist/src/generators/stencil/component.js +3 -2
  44. package/dist/src/generators/stencil/helpers/index.d.ts +0 -5
  45. package/dist/src/generators/stencil/helpers/index.js +2 -33
  46. package/dist/src/generators/stencil/plugins/get-code-processor-plugins.d.ts +1 -1
  47. package/dist/src/generators/stencil/plugins/get-code-processor-plugins.js +3 -2
  48. package/dist/src/generators/svelte/svelte.js +2 -0
  49. package/dist/src/generators/vue/vue.js +2 -0
  50. package/dist/src/helpers/babel-transform.js +1 -1
  51. package/dist/src/helpers/class-components.d.ts +13 -0
  52. package/dist/src/helpers/class-components.js +51 -0
  53. package/dist/src/helpers/get-state-object-string.d.ts +4 -0
  54. package/dist/src/helpers/get-state-object-string.js +22 -5
  55. package/dist/src/helpers/is-children.js +1 -1
  56. package/dist/src/helpers/map-refs.d.ts +7 -1
  57. package/dist/src/helpers/map-refs.js +21 -21
  58. package/dist/src/helpers/plugins/process-code/index.d.ts +1 -1
  59. package/dist/src/helpers/plugins/process-code/index.js +16 -2
  60. package/dist/src/helpers/plugins/process-code/types.d.ts +3 -2
  61. package/dist/src/helpers/render-imports.d.ts +5 -0
  62. package/dist/src/helpers/replace-identifiers.js +1 -4
  63. package/dist/src/helpers/traverse-nodes.d.ts +1 -1
  64. package/dist/src/parsers/builder/builder.d.ts +6 -0
  65. package/dist/src/parsers/builder/builder.js +25 -3
  66. package/dist/src/parsers/jsx/function-parser.js +15 -0
  67. package/dist/src/parsers/svelte/html/text.d.ts +18 -0
  68. package/dist/src/types/mitosis-component.d.ts +2 -0
  69. package/dist/src/types/mitosis-node.d.ts +7 -2
  70. package/dist/src/types/mitosis-node.js +8 -3
  71. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
- import { AngularBlockOptions, ToAngularOptions } from '../../generators/angular/types';
2
- import { MitosisComponent } from '../../types/mitosis-component';
3
- import { MitosisNode } from '../../types/mitosis-node';
1
+ import { AngularBlockOptions, ToAngularOptions } from '../../../generators/angular/types';
2
+ import { MitosisComponent } from '../../../types/mitosis-component';
3
+ import { MitosisNode } from '../../../types/mitosis-node';
4
4
  export declare const blockToAngular: ({ root, json, options, blockOptions, rootRef, }: {
5
5
  root: MitosisComponent;
6
6
  json: MitosisNode;
@@ -4,19 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.blockToAngular = void 0;
7
- const html_tags_1 = require("../../constants/html_tags");
8
- const helpers_1 = require("../../generators/angular/helpers");
9
- const hooks_1 = require("../../generators/angular/helpers/hooks");
10
- const parse_selector_1 = require("../../generators/angular/parse-selector");
11
- const bindings_1 = require("../../helpers/bindings");
12
- const event_handlers_1 = require("../../helpers/event-handlers");
13
- const get_tag_name_1 = require("../../helpers/get-tag-name");
14
- const is_children_1 = __importDefault(require("../../helpers/is-children"));
15
- const is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
16
- const remove_surrounding_block_1 = require("../../helpers/remove-surrounding-block");
17
- const slots_1 = require("../../helpers/slots");
18
- const symbol_processor_1 = require("../../symbols/symbol-processor");
19
- const mitosis_node_1 = require("../../types/mitosis-node");
7
+ const html_tags_1 = require("../../../constants/html_tags");
8
+ const helpers_1 = require("../../../generators/angular/helpers");
9
+ const hooks_1 = require("../../../generators/angular/helpers/hooks");
10
+ const parse_selector_1 = require("../../../generators/angular/helpers/parse-selector");
11
+ const bindings_1 = require("../../../helpers/bindings");
12
+ const event_handlers_1 = require("../../../helpers/event-handlers");
13
+ const get_tag_name_1 = require("../../../helpers/get-tag-name");
14
+ const is_children_1 = __importDefault(require("../../../helpers/is-children"));
15
+ const is_mitosis_node_1 = require("../../../helpers/is-mitosis-node");
16
+ const remove_surrounding_block_1 = require("../../../helpers/remove-surrounding-block");
17
+ const slots_1 = require("../../../helpers/slots");
18
+ const symbol_processor_1 = require("../../../symbols/symbol-processor");
19
+ const mitosis_node_1 = require("../../../types/mitosis-node");
20
20
  const function_1 = require("fp-ts/function");
21
21
  const lodash_1 = require("lodash");
22
22
  const mappers = {
@@ -304,7 +304,7 @@ const blockToAngular = ({ root, json, options = {}, blockOptions = {
304
304
  const selector = json.meta.selector || (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.selector);
305
305
  if (selector) {
306
306
  try {
307
- ({ element, classNames, attributes } = (0, parse_selector_1.parse)(`${selector}`));
307
+ ({ element, classNames, attributes } = (0, parse_selector_1.parseSelector)(`${selector}`));
308
308
  }
309
309
  catch (_r) {
310
310
  element = tagName !== null && tagName !== void 0 ? tagName : (0, lodash_1.kebabCase)(json.name);
@@ -0,0 +1,3 @@
1
+ import { ToAngularOptions } from '../../../generators/angular/types';
2
+ import { TranspilerGenerator } from '../../../types/transpiler';
3
+ export declare const componentToAngularClassic: TranspilerGenerator<ToAngularOptions>;
@@ -0,0 +1,356 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.componentToAngularClassic = void 0;
4
+ const blocks_1 = require("../../../generators/angular/classic/blocks");
5
+ const get_class_properties_plugin_1 = require("../../../generators/angular/classic/plugins/get-class-properties-plugin");
6
+ const get_code_processor_plugins_1 = require("../../../generators/angular/classic/plugins/get-code-processor-plugins");
7
+ const helpers_1 = require("../../../generators/angular/helpers");
8
+ const get_inputs_1 = require("../../../generators/angular/helpers/get-inputs");
9
+ const get_outputs_1 = require("../../../generators/angular/helpers/get-outputs");
10
+ const get_refs_1 = require("../../../generators/angular/helpers/get-refs");
11
+ const get_styles_1 = require("../../../generators/angular/helpers/get-styles");
12
+ const types_1 = require("../../../generators/angular/types");
13
+ const on_mount_1 = require("../../../generators/helpers/on-mount");
14
+ const dedent_1 = require("../../../helpers/dedent");
15
+ const fast_clone_1 = require("../../../helpers/fast-clone");
16
+ const get_child_components_1 = require("../../../helpers/get-child-components");
17
+ const get_components_used_1 = require("../../../helpers/get-components-used");
18
+ const get_custom_imports_1 = require("../../../helpers/get-custom-imports");
19
+ const get_prop_functions_1 = require("../../../helpers/get-prop-functions");
20
+ const get_props_1 = require("../../../helpers/get-props");
21
+ const get_props_ref_1 = require("../../../helpers/get-props-ref");
22
+ const get_refs_2 = require("../../../helpers/get-refs");
23
+ const get_state_object_string_1 = require("../../../helpers/get-state-object-string");
24
+ const get_typed_function_1 = require("../../../helpers/get-typed-function");
25
+ const is_upper_case_1 = require("../../../helpers/is-upper-case");
26
+ const merge_options_1 = require("../../../helpers/merge-options");
27
+ const render_imports_1 = require("../../../helpers/render-imports");
28
+ const strip_meta_properties_1 = require("../../../helpers/strip-meta-properties");
29
+ const attribute_passing_1 = require("../../../helpers/web-components/attribute-passing");
30
+ const plugins_1 = require("../../../modules/plugins");
31
+ const lodash_1 = require("lodash");
32
+ const format_1 = require("../helpers/format");
33
+ const componentToAngularClassic = (userOptions = {}) => {
34
+ return ({ component: _component }) => {
35
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
36
+ // Make a copy we can safely mutate, similar to babel's toolchain
37
+ let json = (0, fast_clone_1.fastClone)(_component);
38
+ const useMetadata = (_a = json.meta) === null || _a === void 0 ? void 0 : _a.useMetadata;
39
+ const contextVars = Object.keys(((_b = json === null || json === void 0 ? void 0 : json.context) === null || _b === void 0 ? void 0 : _b.get) || {});
40
+ const metaOutputVars = (useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.outputs) || [];
41
+ const outputVars = (0, lodash_1.uniq)([...metaOutputVars, ...(0, get_prop_functions_1.getPropFunctions)(json)]);
42
+ const options = (0, merge_options_1.initializeOptions)({
43
+ target: 'angular',
44
+ component: _component,
45
+ defaults: types_1.DEFAULT_ANGULAR_OPTIONS,
46
+ userOptions,
47
+ });
48
+ options.plugins = (0, get_code_processor_plugins_1.getCodeProcessorPlugins)({ json, options, outputVars, contextVars });
49
+ if (options.state === 'class-properties') {
50
+ options.plugins.push((0, get_class_properties_plugin_1.getClassPropertiesPlugin)());
51
+ }
52
+ if (options.plugins) {
53
+ json = (0, plugins_1.runPreJsonPlugins)({ json, plugins: options.plugins });
54
+ }
55
+ // Prepare Props
56
+ const [forwardProp, hasPropRef] = (0, get_props_ref_1.getPropsRef)(json, true);
57
+ const props = (0, get_props_1.getProps)(json);
58
+ // prevent jsx props from showing up as @Input
59
+ if (hasPropRef) {
60
+ props.delete(forwardProp);
61
+ }
62
+ props.delete('children');
63
+ // remove props for outputs
64
+ for (const variableName of outputVars) {
65
+ props.delete(variableName);
66
+ }
67
+ const childComponents = (0, get_child_components_1.getChildComponents)(json);
68
+ const customImports = (0, get_custom_imports_1.getCustomImports)(json);
69
+ const { exports: localExports = {} } = json;
70
+ const localExportVars = Object.keys(localExports)
71
+ .filter((key) => localExports[key].usedInLocal)
72
+ .map((key) => `${key} = ${key};`);
73
+ // Context handling
74
+ const injectables = contextVars.map((variableName) => {
75
+ var _a, _b, _c, _d;
76
+ const variableType = (_a = json === null || json === void 0 ? void 0 : json.context) === null || _a === void 0 ? void 0 : _a.get[variableName].name;
77
+ if ((_b = options === null || options === void 0 ? void 0 : options.experimental) === null || _b === void 0 ? void 0 : _b.injectables) {
78
+ return (_c = options === null || options === void 0 ? void 0 : options.experimental) === null || _c === void 0 ? void 0 : _c.injectables(variableName, variableType);
79
+ }
80
+ if ((_d = options === null || options === void 0 ? void 0 : options.experimental) === null || _d === void 0 ? void 0 : _d.inject) {
81
+ return `@Inject(forwardRef(() => ${variableType})) public ${variableName}: ${variableType}`;
82
+ }
83
+ return `public ${variableName} : ${variableType}`;
84
+ });
85
+ // Handle refs
86
+ const withAttributePassing = (0, attribute_passing_1.shouldAddAttributePassing)(json, options);
87
+ const rootRef = (0, attribute_passing_1.getAddAttributePassingRef)(json, options);
88
+ const domRefs = (0, get_refs_1.getDomRefs)({ json, options, withAttributePassing, rootRef });
89
+ const jsRefs = Object.keys(json.refs).filter((ref) => !domRefs.has(ref));
90
+ if (options.plugins) {
91
+ json = (0, plugins_1.runPostJsonPlugins)({ json, plugins: options.plugins });
92
+ }
93
+ // CSS
94
+ const styles = (0, get_styles_1.getAngularStyles)({ json, options });
95
+ const helperFunctions = new Set();
96
+ const shouldUseSanitizer = !((_c = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _c === void 0 ? void 0 : _c.sanitizeInnerHTML) && (0, helpers_1.traverseAndCheckIfInnerHTMLIsUsed)(json);
97
+ const changeDetectionStrategy = (_d = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _d === void 0 ? void 0 : _d.changeDetection;
98
+ let template = json.children
99
+ .map((item) => {
100
+ var _a, _b, _c, _d;
101
+ const tmpl = (0, blocks_1.blockToAngular)({
102
+ root: json,
103
+ json: item,
104
+ options,
105
+ rootRef: withAttributePassing && rootRef === attribute_passing_1.ROOT_REF ? rootRef : undefined, // only pass rootRef if it's not the default
106
+ blockOptions: {
107
+ childComponents,
108
+ 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 : [],
109
+ 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 : [],
110
+ sanitizeInnerHTML: !shouldUseSanitizer,
111
+ },
112
+ });
113
+ if (options.state === 'inline-with-wrappers') {
114
+ (0, helpers_1.getAppropriateTemplateFunctionKeys)(tmpl).forEach((key) => helperFunctions.add((0, helpers_1.HELPER_FUNCTIONS)(options.typescript)[key]));
115
+ }
116
+ return tmpl;
117
+ })
118
+ .join('\n');
119
+ if (options.prettier !== false) {
120
+ template = (0, format_1.tryFormat)(template, 'html');
121
+ }
122
+ (0, strip_meta_properties_1.stripMetaProperties)(json);
123
+ const { components: dynamicComponents, dynamicTemplate } = (0, helpers_1.traverseToGetAllDynamicComponents)(json, options, {
124
+ childComponents,
125
+ 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 : [],
126
+ 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 : [],
127
+ });
128
+ (0, helpers_1.transformState)(json);
129
+ const dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
130
+ format: 'class',
131
+ withType: options.typescript,
132
+ valueMapper: (code, type, typeParameter) => {
133
+ let value = code;
134
+ if (type !== 'data') {
135
+ value = (0, get_typed_function_1.getTypedFunction)(code, options.typescript, typeParameter);
136
+ }
137
+ return (0, helpers_1.processAngularCode)({
138
+ replaceWith: 'this',
139
+ contextVars,
140
+ outputVars,
141
+ domRefs: Array.from(domRefs),
142
+ })(value);
143
+ },
144
+ });
145
+ const refsForObjSpread = (0, get_refs_2.getRefs)(json, 'spreadRef');
146
+ // Preparing built in component metadata parameters
147
+ const componentsUsed = Array.from((0, get_components_used_1.getComponentsUsed)(json)).filter((item) => item.length && (0, is_upper_case_1.isUpperCase)(item[0]) && !types_1.BUILT_IN_COMPONENTS.has(item));
148
+ const componentMetadata = {
149
+ selector: ((_j = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _j === void 0 ? void 0 : _j.selector)
150
+ ? `'${(_k = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _k === void 0 ? void 0 : _k.selector}'`
151
+ : `'${(0, lodash_1.kebabCase)(json.name || 'my-component')}'`,
152
+ template: `\`
153
+ ${(0, helpers_1.getTemplateFormat)(dynamicTemplate)}
154
+ ${(0, helpers_1.getTemplateFormat)(template)}
155
+ \``,
156
+ ...(changeDetectionStrategy === 'OnPush'
157
+ ? {
158
+ changeDetection: 'ChangeDetectionStrategy.OnPush',
159
+ }
160
+ : {}),
161
+ ...(styles
162
+ ? {
163
+ styles: `[\`${styles}\`]`,
164
+ }
165
+ : {}),
166
+ ...(options.standalone
167
+ ? // TODO: also add child component imports here as well
168
+ {
169
+ standalone: 'true',
170
+ imports: `[${['CommonModule', ...componentsUsed].join(', ')}]`,
171
+ }
172
+ : {}),
173
+ };
174
+ // Taking into consideration what user has passed in options and allowing them to override the default generated metadata
175
+ Object.entries(json.meta.angularConfig || {}).forEach(([key, value]) => {
176
+ componentMetadata[key] = value;
177
+ });
178
+ const hasConstructor = Boolean(injectables.length) ||
179
+ dynamicComponents.size ||
180
+ refsForObjSpread.size ||
181
+ shouldUseSanitizer;
182
+ const angularCoreImports = [
183
+ ...(outputVars.length ? ['Output', 'EventEmitter'] : []),
184
+ ...(((_l = options === null || options === void 0 ? void 0 : options.experimental) === null || _l === void 0 ? void 0 : _l.inject) ? ['Inject', 'forwardRef'] : []),
185
+ 'Component',
186
+ ...(domRefs.size || dynamicComponents.size || refsForObjSpread.size
187
+ ? ['ViewChild', 'ElementRef']
188
+ : []),
189
+ ...(refsForObjSpread.size ? ['Renderer2'] : []),
190
+ ...(props.size ? ['Input'] : []),
191
+ ...(dynamicComponents.size ? ['ViewContainerRef', 'TemplateRef'] : []),
192
+ ...(((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.length) && options.typescript ? ['SimpleChanges'] : []),
193
+ ...(changeDetectionStrategy === 'OnPush' ? ['ChangeDetectionStrategy'] : []),
194
+ ].join(', ');
195
+ const constructorInjectables = [
196
+ ...injectables,
197
+ ...(dynamicComponents.size
198
+ ? [`private vcRef${options.typescript ? ': ViewContainerRef' : ''}`]
199
+ : []),
200
+ ...(refsForObjSpread.size
201
+ ? [`private renderer${options.typescript ? ': Renderer2' : ''}`]
202
+ : []),
203
+ ...(shouldUseSanitizer
204
+ ? [`protected sanitizer${options.typescript ? ': DomSanitizer' : ''}`]
205
+ : []),
206
+ ].join(',\n');
207
+ let str = (0, dedent_1.dedent) `
208
+ import { ${angularCoreImports} } from '@angular/core';
209
+ ${shouldUseSanitizer ? `import { DomSanitizer } from '@angular/platform-browser';` : ''}
210
+ ${options.standalone ? `import { CommonModule } from '@angular/common';` : ''}
211
+
212
+ ${json.types ? json.types.join('\n') : ''}
213
+ ${(0, helpers_1.getDefaultProps)(json)}
214
+ ${(0, render_imports_1.renderPreComponent)({
215
+ explicitImportFileExtension: options.explicitImportFileExtension,
216
+ component: json,
217
+ target: 'angular',
218
+ excludeMitosisComponents: !options.standalone && !options.preserveImports,
219
+ preserveFileExtensions: options.preserveFileExtensions,
220
+ componentsUsed,
221
+ importMapper: options === null || options === void 0 ? void 0 : options.importMapper,
222
+ })}
223
+
224
+ @Component({
225
+ ${Object.entries(componentMetadata)
226
+ .map(([k, v]) => `${k}: ${v}`)
227
+ .join(',')}
228
+ })
229
+ export default class ${json.name} {
230
+ ${localExportVars.join('\n')}
231
+ ${customImports.map((name) => `${name} = ${name}`).join('\n')}
232
+
233
+ ${(0, get_inputs_1.getInputs)({
234
+ json,
235
+ options,
236
+ props: Array.from(props),
237
+ })}
238
+ ${(0, get_outputs_1.getOutputs)({ json, outputVars })}
239
+
240
+ ${[...Array.from(domRefs), ...Array.from(refsForObjSpread)]
241
+ .map((refName) => `@ViewChild('${refName}') ${refName}${options.typescript ? '!: ElementRef' : ''}`)
242
+ .join('\n')}
243
+
244
+ ${Array.from(dynamicComponents)
245
+ .map((component) => `@ViewChild('${component
246
+ .split('.')[1]
247
+ .toLowerCase()}Template', { static: true }) ${component
248
+ .split('.')[1]
249
+ .toLowerCase()}TemplateRef${options.typescript ? '!: TemplateRef<any>' : ''}`)
250
+ .join('\n')}
251
+
252
+ ${dynamicComponents.size ? `myContent${options.typescript ? '?: any[][];' : ''}` : ''}
253
+ ${refsForObjSpread.size
254
+ ? `_listenerFns = new Map${options.typescript ? '<string, () => void>' : ''}()`
255
+ : ''}
256
+
257
+ ${dataString}
258
+
259
+ ${helperFunctions.size ? Array.from(helperFunctions).join('\n') : ''}
260
+
261
+ ${jsRefs
262
+ .map((ref) => {
263
+ const argument = json.refs[ref].argument;
264
+ const typeParameter = json.refs[ref].typeParameter;
265
+ return `private _${ref}${typeParameter ? `: ${typeParameter}` : ''}${argument
266
+ ? ` = ${(0, helpers_1.processAngularCode)({
267
+ replaceWith: 'this.',
268
+ contextVars,
269
+ outputVars,
270
+ domRefs: Array.from(domRefs),
271
+ })(argument)}`
272
+ : ''};`;
273
+ })
274
+ .join('\n')}
275
+
276
+ ${!hasConstructor ? '' : `constructor(\n${constructorInjectables}) {}`}
277
+
278
+ ${withAttributePassing ? (0, attribute_passing_1.getAttributePassingString)(options.typescript) : ''}
279
+
280
+ ${!json.hooks.onMount.length && !dynamicComponents.size && !((_o = json.hooks.onInit) === null || _o === void 0 ? void 0 : _o.code)
281
+ ? ''
282
+ : `ngOnInit() {
283
+ ${!((_p = json.hooks) === null || _p === void 0 ? void 0 : _p.onInit)
284
+ ? ''
285
+ : `
286
+ ${(_q = json.hooks.onInit) === null || _q === void 0 ? void 0 : _q.code}
287
+ `}
288
+ ${json.hooks.onMount.length > 0
289
+ ? `
290
+ if (typeof window !== 'undefined') {
291
+ ${(0, on_mount_1.stringifySingleScopeOnMount)(json)}
292
+ }
293
+ `
294
+ : ''}
295
+ ${dynamicComponents.size
296
+ ? `
297
+ this.myContent = [${Array.from(dynamicComponents)
298
+ .map((component) => `this.vcRef.createEmbeddedView(this.${component
299
+ .split('.')[1]
300
+ .toLowerCase()}TemplateRef).rootNodes`)
301
+ .join(', ')}];
302
+ `
303
+ : ''}
304
+ }`}
305
+
306
+ ${
307
+ // hooks specific to Angular
308
+ ((_s = (_r = json.compileContext) === null || _r === void 0 ? void 0 : _r.angular) === null || _s === void 0 ? void 0 : _s.hooks)
309
+ ? Object.entries((_u = (_t = json.compileContext) === null || _t === void 0 ? void 0 : _t.angular) === null || _u === void 0 ? void 0 : _u.hooks)
310
+ .map(([key, value]) => {
311
+ return `${key}() {
312
+ ${value.code}
313
+ }`;
314
+ })
315
+ .join('\n')
316
+ : ''}
317
+
318
+ ${!((_v = json.hooks.onUpdate) === null || _v === void 0 ? void 0 : _v.length)
319
+ ? ''
320
+ : `ngOnChanges(changes${options.typescript ? ': SimpleChanges' : ''}) {
321
+ if (typeof window !== 'undefined') {
322
+ ${(_w = json.hooks.onUpdate) === null || _w === void 0 ? void 0 : _w.reduce((code, hook) => {
323
+ code += hook.code;
324
+ return code + '\n';
325
+ }, '')}
326
+ }
327
+ }
328
+ `}
329
+
330
+ ${!json.hooks.onUnMount && !refsForObjSpread.size
331
+ ? ''
332
+ : `ngOnDestroy() {
333
+ ${((_x = json.hooks.onUnMount) === null || _x === void 0 ? void 0 : _x.code) || ''}
334
+ ${refsForObjSpread.size
335
+ ? `for (const fn of this._listenerFns.values()) { fn(); }`
336
+ : ''}
337
+ }`}
338
+
339
+ }
340
+ `;
341
+ if (options.standalone !== true) {
342
+ str = (0, helpers_1.generateNgModule)(str, json.name, componentsUsed, json, options.bootstrapMapper);
343
+ }
344
+ if (options.plugins) {
345
+ str = (0, plugins_1.runPreCodePlugins)({ json, code: str, plugins: options.plugins });
346
+ }
347
+ if (options.prettier !== false) {
348
+ str = (0, format_1.tryFormat)(str, 'typescript');
349
+ }
350
+ if (options.plugins) {
351
+ str = (0, plugins_1.runPostCodePlugins)({ json, code: str, plugins: options.plugins });
352
+ }
353
+ return str;
354
+ };
355
+ };
356
+ exports.componentToAngularClassic = componentToAngularClassic;
@@ -0,0 +1,2 @@
1
+ import { MitosisPlugin } from '../../../../types/plugins';
2
+ export declare const getClassPropertiesPlugin: () => MitosisPlugin;
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getClassPropertiesPlugin = void 0;
7
+ const hooks_1 = require("../../../../generators/angular/helpers/hooks");
8
+ const bindings_1 = require("../../../../helpers/bindings");
9
+ const event_handlers_1 = require("../../../../helpers/event-handlers");
10
+ const is_children_1 = __importDefault(require("../../../../helpers/is-children"));
11
+ const is_mitosis_node_1 = require("../../../../helpers/is-mitosis-node");
12
+ const traverse_nodes_1 = require("../../../../helpers/traverse-nodes");
13
+ const isASimpleProperty = (code) => {
14
+ const expressions = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
15
+ const invalidChars = ['{', '}', '(', ')', 'typeof'];
16
+ return !invalidChars.some((char) => code.includes(char)) && !expressions.includes(code);
17
+ };
18
+ const generateNewBindingName = (index, name) => `node_${index}_${name.replaceAll('.', '_').replaceAll('-', '_')}`;
19
+ const handleBindings = (json, item, index, forName, indexName) => {
20
+ var _a, _b, _c, _d, _e, _f;
21
+ for (const key in item.bindings) {
22
+ if (key.startsWith('"') ||
23
+ key.startsWith('$') ||
24
+ key === 'css' ||
25
+ key === 'ref' ||
26
+ isASimpleProperty(item.bindings[key].code)) {
27
+ continue;
28
+ }
29
+ const newBindingName = generateNewBindingName(index, item.name);
30
+ if (forName) {
31
+ if (item.name === 'For')
32
+ continue;
33
+ if (key === 'key')
34
+ continue;
35
+ if ((0, event_handlers_1.checkIsEvent)(key)) {
36
+ const { arguments: cusArgs = ['event'] } = item.bindings[key];
37
+ const eventBindingName = `${generateNewBindingName(index, item.name)}_event`;
38
+ if (((_a = item.bindings[key]) === null || _a === void 0 ? void 0 : _a.code.trim().startsWith('{')) &&
39
+ ((_b = item.bindings[key]) === null || _b === void 0 ? void 0 : _b.code.trim().endsWith('}'))) {
40
+ const forAndIndex = `${forName ? `, ${forName}` : ''}${indexName ? `, ${indexName}` : ''}`;
41
+ const eventArgs = `${cusArgs.join(', ')}${forAndIndex}`;
42
+ json.state[eventBindingName] = {
43
+ code: `(${eventArgs}) => ${item.bindings[key].code}`,
44
+ type: 'function',
45
+ };
46
+ item.bindings[key].code = `state.${eventBindingName}(${eventArgs})`;
47
+ json.state[newBindingName] = {
48
+ code: `(${eventArgs}) => (${item.bindings[key].code})`,
49
+ type: 'function',
50
+ };
51
+ item.bindings[key].code = `state.${newBindingName}($${eventArgs})`;
52
+ }
53
+ }
54
+ else {
55
+ json.state[newBindingName] = {
56
+ code: `(${forName}${indexName ? `, ${indexName}` : ''}) => (${item.bindings[key].code})`,
57
+ type: 'function',
58
+ };
59
+ item.bindings[key].code = `state.${newBindingName}(${forName}${indexName ? `, ${indexName}` : ''})`;
60
+ }
61
+ }
62
+ else if ((_c = item.bindings[key]) === null || _c === void 0 ? void 0 : _c.code) {
63
+ if (((_d = item.bindings[key]) === null || _d === void 0 ? void 0 : _d.type) !== 'spread' && !(0, event_handlers_1.checkIsEvent)(key)) {
64
+ json.state[newBindingName] = { code: 'null', type: 'property' };
65
+ (0, hooks_1.makeReactiveState)(json, newBindingName, `this.${newBindingName} = ${item.bindings[key].code}`);
66
+ item.bindings[key].code = `state.${newBindingName}`;
67
+ }
68
+ else if ((0, event_handlers_1.checkIsEvent)(key)) {
69
+ const { arguments: cusArgs = ['event'] } = item.bindings[key];
70
+ if (((_e = item.bindings[key]) === null || _e === void 0 ? void 0 : _e.code.trim().startsWith('{')) &&
71
+ ((_f = item.bindings[key]) === null || _f === void 0 ? void 0 : _f.code.trim().endsWith('}'))) {
72
+ json.state[newBindingName] = {
73
+ code: `(${cusArgs.join(', ')}) => ${item.bindings[key].code}`,
74
+ type: 'function',
75
+ };
76
+ item.bindings[key].code = `state.${newBindingName}(${cusArgs.join(', ')})`;
77
+ }
78
+ }
79
+ else {
80
+ (0, hooks_1.makeReactiveState)(json, newBindingName, `state.${newBindingName} = {...(${item.bindings[key].code})}`);
81
+ item.bindings[newBindingName] = item.bindings[key];
82
+ item.bindings[key].code = `state.${newBindingName}`;
83
+ delete item.bindings[key];
84
+ }
85
+ }
86
+ index++;
87
+ }
88
+ return index;
89
+ };
90
+ const handleProperties = (json, item, index) => {
91
+ for (const key in item.properties) {
92
+ if (key.startsWith('$') || isASimpleProperty(item.properties[key])) {
93
+ continue;
94
+ }
95
+ const newBindingName = generateNewBindingName(index, item.name);
96
+ json.state[newBindingName] = { code: '`' + `${item.properties[key]}` + '`', type: 'property' };
97
+ item.bindings[key] = (0, bindings_1.createSingleBinding)({ code: `state.${newBindingName}` });
98
+ delete item.properties[key];
99
+ index++;
100
+ }
101
+ return index;
102
+ };
103
+ const handleAngularBindings = (json, item, index, { forName, indexName } = {}) => {
104
+ if ((0, is_children_1.default)({ node: item }))
105
+ return index;
106
+ index = handleBindings(json, item, index, forName, indexName);
107
+ index = handleProperties(json, item, index);
108
+ return index;
109
+ };
110
+ const getClassPropertiesPlugin = () => () => ({
111
+ json: {
112
+ pre: (json) => {
113
+ let lastId = 0;
114
+ (0, traverse_nodes_1.traverseNodes)(json, (item) => {
115
+ if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
116
+ if (item.name === 'For') {
117
+ const forName = item.scope.forName;
118
+ const indexName = item.scope.indexName;
119
+ (0, traverse_nodes_1.traverseNodes)(item, (child) => {
120
+ if ((0, is_mitosis_node_1.isMitosisNode)(child)) {
121
+ child._traversed = true;
122
+ lastId = handleAngularBindings(json, child, lastId, {
123
+ forName,
124
+ indexName,
125
+ });
126
+ }
127
+ });
128
+ }
129
+ else if (!item._traversed) {
130
+ lastId = handleAngularBindings(json, item, lastId);
131
+ }
132
+ }
133
+ });
134
+ return json;
135
+ },
136
+ },
137
+ });
138
+ exports.getClassPropertiesPlugin = getClassPropertiesPlugin;
@@ -0,0 +1,8 @@
1
+ import { ToAngularOptions } from '../../../../generators/angular/types';
2
+ import { MitosisComponent } from '../../../../types/mitosis-component';
3
+ export declare const getCodeProcessorPlugins: ({ json, contextVars, options, outputVars, }: {
4
+ json: MitosisComponent;
5
+ options: ToAngularOptions;
6
+ contextVars: string[];
7
+ outputVars: string[];
8
+ }) => import("../../../..").MitosisPlugin[];
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCodeProcessorPlugins = void 0;
4
+ const html_tags_1 = require("../../../../constants/html_tags");
5
+ const helpers_1 = require("../../../../generators/angular/helpers");
6
+ const get_refs_1 = require("../../../../helpers/get-refs");
7
+ const process_code_1 = require("../../../../helpers/plugins/process-code");
8
+ const replace_identifiers_1 = require("../../../../helpers/replace-identifiers");
9
+ const function_1 = require("fp-ts/function");
10
+ const getCodeProcessorPlugins = ({ json, contextVars, options, outputVars, }) => {
11
+ return [
12
+ ...(options.plugins || []),
13
+ (0, process_code_1.CODE_PROCESSOR_PLUGIN)((codeType, _, node) => {
14
+ switch (codeType) {
15
+ case 'hooks':
16
+ return (0, function_1.flow)((0, helpers_1.processAngularCode)({
17
+ replaceWith: 'this',
18
+ contextVars,
19
+ outputVars,
20
+ domRefs: Array.from((0, get_refs_1.getRefs)(json)),
21
+ }), (code) => {
22
+ const allMethodNames = Object.entries(json.state)
23
+ .filter(([_, value]) => (value === null || value === void 0 ? void 0 : value.type) === 'function' || (value === null || value === void 0 ? void 0 : value.type) === 'method')
24
+ .map(([key]) => key);
25
+ return (0, replace_identifiers_1.replaceIdentifiers)({
26
+ code,
27
+ from: allMethodNames,
28
+ to: (name) => `this.${name}`,
29
+ });
30
+ });
31
+ case 'bindings':
32
+ return (code, key) => {
33
+ var _a;
34
+ // we create a separate state property for spread binding and use ref to attach the attributes
35
+ // so we need to use `this.` inside the class to access state and props
36
+ const isSpreadAttributeBinding = ((_a = node === null || node === void 0 ? void 0 : node.bindings[key]) === null || _a === void 0 ? void 0 : _a.type) === 'spread' && html_tags_1.VALID_HTML_TAGS.includes(node.name.trim());
37
+ // If we have a For loop with "key" it will be transformed to
38
+ // trackOfXXX, we need to use "this" for state properties
39
+ const isKey = key === 'key';
40
+ const newLocal = (0, helpers_1.processAngularCode)({
41
+ contextVars: [],
42
+ outputVars,
43
+ domRefs: [], // the template doesn't need the this keyword.
44
+ replaceWith: isKey || isSpreadAttributeBinding ? 'this' : undefined,
45
+ })(code);
46
+ return newLocal.replace(/"/g, '&quot;');
47
+ };
48
+ case 'hooks-deps':
49
+ case 'hooks-deps-array':
50
+ case 'state':
51
+ case 'context-set':
52
+ case 'properties':
53
+ case 'dynamic-jsx-elements':
54
+ case 'types':
55
+ return (code) => code;
56
+ }
57
+ }),
58
+ ];
59
+ };
60
+ exports.getCodeProcessorPlugins = getCodeProcessorPlugins;
@@ -0,0 +1,3 @@
1
+ import { ToAngularOptions } from '../../generators/angular/types';
2
+ import { TranspilerGenerator } from '../../types/transpiler';
3
+ export declare const componentToAngular: TranspilerGenerator<ToAngularOptions>;