@builder.io/mitosis 0.0.56-42 → 0.0.56-46

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.
@@ -37,6 +37,7 @@ var strip_meta_properties_1 = require("../helpers/strip-meta-properties");
37
37
  var remove_surrounding_block_1 = require("../helpers/remove-surrounding-block");
38
38
  var indent_1 = require("../helpers/indent");
39
39
  var slots_1 = require("../helpers/slots");
40
+ var get_custom_imports_1 = require("../helpers/get-custom-imports");
40
41
  var mappers = {
41
42
  Fragment: function (json, options, blockOptions) {
42
43
  return "<div>".concat(json.children
@@ -141,7 +142,7 @@ var blockToAngular = function (json, options, blockOptions) {
141
142
  contextVars: contextVars,
142
143
  outputVars: outputVars,
143
144
  domRefs: domRefs,
144
- });
145
+ }).replace(/"/g, '&quot;');
145
146
  if (key.startsWith('on')) {
146
147
  var event_1 = key.replace('on', '').toLowerCase();
147
148
  if (event_1 === 'change' && json.name === 'input' /* todo: other tags */) {
@@ -165,10 +166,10 @@ var blockToAngular = function (json, options, blockOptions) {
165
166
  needsToRenderSlots.push("".concat(useValue.replace(/(\/\>)|\>/, " ".concat(lowercaseKey, ">"))));
166
167
  }
167
168
  else if (BINDINGS_MAPPER[key]) {
168
- str += " [".concat(BINDINGS_MAPPER[key], "]=\"").concat(useValue.replace(/"/g, "\\'"), "\" ");
169
+ str += " [".concat(BINDINGS_MAPPER[key], "]=\"").concat(useValue, "\" ");
169
170
  }
170
171
  else {
171
- str += " [".concat(key, "]='").concat(useValue, "' ");
172
+ str += " [".concat(key, "]=\"").concat(useValue, "\" ");
172
173
  }
173
174
  }
174
175
  if (jsx_1.selfClosingTags.has(json.name)) {
@@ -207,6 +208,7 @@ var componentToAngular = function (options) {
207
208
  }
208
209
  });
209
210
  });
211
+ var customImports = (0, get_custom_imports_1.getCustomImports)(json);
210
212
  var _q = component.exports, localExports = _q === void 0 ? {} : _q;
211
213
  var localExportVars = Object.keys(localExports)
212
214
  .filter(function (key) { return localExports[key].usedInLocal; })
@@ -283,12 +285,12 @@ var componentToAngular = function (options) {
283
285
  });
284
286
  },
285
287
  });
286
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n selector: '", "',\n template: `\n ", "\n `,\n ", "\n })\n export default class ", " {\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "], ["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n selector: '", "',\n template: \\`\n ", "\n \\`,\n ", "\n })\n export default class ", " {\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "])), outputs.length ? 'Output, EventEmitter, \n' : '', ((_g = options === null || options === void 0 ? void 0 : options.experimental) === null || _g === void 0 ? void 0 : _g.inject) ? 'Inject, forwardRef,' : '', domRefs.size ? ', ViewChild, ElementRef' : '', props.size ? ', Input' : '', options.standalone ? "import { CommonModule } from '@angular/common';" : '', json.types ? json.types.join('\n') : '', json.interfaces ? (_h = json.interfaces) === null || _h === void 0 ? void 0 : _h.join('\n') : '', (0, render_imports_1.renderPreComponent)({ component: json, target: 'angular' }), options.standalone
288
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n selector: '", "',\n template: `\n ", "\n `,\n ", "\n })\n export default class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "], ["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n selector: '", "',\n template: \\`\n ", "\n \\`,\n ", "\n })\n export default class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "])), outputs.length ? 'Output, EventEmitter, \n' : '', ((_g = options === null || options === void 0 ? void 0 : options.experimental) === null || _g === void 0 ? void 0 : _g.inject) ? 'Inject, forwardRef,' : '', domRefs.size ? ', ViewChild, ElementRef' : '', props.size ? ', Input' : '', options.standalone ? "import { CommonModule } from '@angular/common';" : '', json.types ? json.types.join('\n') : '', json.interfaces ? (_h = json.interfaces) === null || _h === void 0 ? void 0 : _h.join('\n') : '', (0, render_imports_1.renderPreComponent)({ component: json, target: 'angular' }), options.standalone
287
289
  ? // TODO: also add child component imports here as well
288
290
  "\n standalone: true,\n imports: [CommonModule],\n "
289
291
  : '', (0, lodash_1.kebabCase)(json.name || 'my-component'), (0, indent_1.indent)(template, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${'), css.length
290
292
  ? "styles: [\n `".concat((0, indent_1.indent)(css, 8), "`\n ],")
291
- : '', component.name, localExportVars.join('\n'), Array.from(props)
293
+ : '', component.name, localExportVars.join('\n'), customImports.map(function (name) { return "".concat(name, " = ").concat(name); }).join('\n'), Array.from(props)
292
294
  .filter(function (item) { return !(0, slots_1.isSlotProperty)(item) && item !== 'children'; })
293
295
  .map(function (item) {
294
296
  var propType = propsTypeRef ? "".concat(propsTypeRef, "[\"").concat(item, "\"]") : 'any';
@@ -63,6 +63,7 @@ var process_http_requests_1 = require("../helpers/process-http-requests");
63
63
  var patterns_1 = require("../helpers/patterns");
64
64
  var method_literal_prefix_1 = require("../constants/method-literal-prefix");
65
65
  var function_1 = require("fp-ts/lib/function");
66
+ var get_custom_imports_1 = require("../helpers/get-custom-imports");
66
67
  function encodeQuotes(string) {
67
68
  return string.replace(/"/g, '&quot;');
68
69
  }
@@ -430,20 +431,13 @@ var componentToVue = function (userOptions) {
430
431
  functions: true,
431
432
  valueMapper: function (code) { return processBinding(code, options, component); },
432
433
  });
433
- var blocksString = JSON.stringify(component.children);
434
434
  // Component references to include in `component: { YourComponent, ... }
435
435
  var componentsUsed = Array.from((0, get_components_used_1.getComponentsUsed)(component))
436
436
  .filter(function (name) { return name.length && !name.includes('.') && name[0].toUpperCase() === name[0]; })
437
437
  // Strip out components that compile away
438
438
  .filter(function (name) { return !['For', 'Show', 'Fragment', component.name].includes(name); });
439
439
  // Append refs to data as { foo, bar, etc }
440
- dataString = dataString.replace(/}$/, "".concat(component.imports
441
- .map(function (thisImport) { return Object.keys(thisImport.imports).join(','); })
442
- // Make sure actually used in template
443
- .filter(function (key) { return Boolean(key && blocksString.includes(key)); })
444
- // Don't include component imports
445
- .filter(function (key) { return !componentsUsed.includes(key); })
446
- .join(','), "}"));
440
+ dataString = dataString.replace(/}$/, "".concat((0, get_custom_imports_1.getCustomImports)(component).join(','), "}"));
447
441
  if (localVarAsData.length) {
448
442
  dataString = dataString.replace(/}$/, "".concat(localVarAsData.join(','), "}"));
449
443
  }
@@ -0,0 +1,15 @@
1
+ import { MitosisComponent } from '../types/mitosis-component';
2
+ /**
3
+ * Return custom imports of basic values (aka things
4
+ * that are not ClassCase like components and types)
5
+ *
6
+ * For for code like:
7
+ * import { foo, bar } from './constants'
8
+ *
9
+ * Will return:
10
+ * ['foo', 'bar' ]
11
+ *
12
+ * This also filters for strings that appears to be actually used
13
+ * by the template
14
+ */
15
+ export declare function getCustomImports(json: MitosisComponent): string[];
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCustomImports = void 0;
4
+ var is_upper_case_1 = require("./is-upper-case");
5
+ /**
6
+ * Return custom imports of basic values (aka things
7
+ * that are not ClassCase like components and types)
8
+ *
9
+ * For for code like:
10
+ * import { foo, bar } from './constants'
11
+ *
12
+ * Will return:
13
+ * ['foo', 'bar' ]
14
+ *
15
+ * This also filters for strings that appears to be actually used
16
+ * by the template
17
+ */
18
+ function getCustomImports(json) {
19
+ var blocksString = JSON.stringify(json.children);
20
+ var customImports = json.imports
21
+ .map(function (item) {
22
+ return Object.keys(item.imports).filter(function (item) { return item && !(0, is_upper_case_1.isUpperCase)(item[0]); });
23
+ })
24
+ .flat()
25
+ // This is imperfect. Basically, if the string of this import name
26
+ // doesn't occur at all, it's definitely not used. If it does, it might.
27
+ // So this simple check helps us ~90% of the time not over-add imports
28
+ // to templates. Arguably "good enough" for now, as there is generally no
29
+ // consequence to over adding here, and it would be a lot more performance expensive
30
+ // during compilation to do a complete AST parse and look for real references
31
+ .filter(function (item) { return blocksString.includes(item); });
32
+ return customImports;
33
+ }
34
+ exports.getCustomImports = getCustomImports;
@@ -889,8 +889,7 @@ function parseJsx(jsx, options) {
889
889
  for (var _i = 0, _a = path.node.specifiers; _i < _a.length; _i++) {
890
890
  var specifier = _a[_i];
891
891
  if (types.isImportSpecifier(specifier)) {
892
- importObject.imports[specifier.imported.name] =
893
- specifier.local.name;
892
+ importObject.imports[specifier.local.name] = specifier.imported.name;
894
893
  }
895
894
  else if (types.isImportDefaultSpecifier(specifier)) {
896
895
  importObject.imports[specifier.local.name] = 'default';