@builder.io/mitosis 0.0.56-64 → 0.0.56-68

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.
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var mitosis_1 = require("@builder.io/mitosis");
4
4
  function MyBasicForShowComponent() {
5
5
  var state = (0, mitosis_1.useStore)({
6
- name: "PatrickJS",
6
+ name: 'PatrickJS',
7
7
  names: ['Steve', 'PatrickJS'],
8
8
  });
9
9
  return (<div>
@@ -45,7 +45,7 @@ var _fake_4 = require("@fake");
45
45
  function FormComponent(props) {
46
46
  var _a;
47
47
  var state = (0, mitosis_1.useStore)({
48
- state: "unsubmitted",
48
+ state: 'unsubmitted',
49
49
  // TODO: separate response and error?
50
50
  responseData: null,
51
51
  formErrorMessage: '',
@@ -0,0 +1 @@
1
+ export declare type Foo = null;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import type { Foo } from './foo-type';
2
+ import type { Foo as Foo2 } from './type-export.lite';
3
+ export declare type TypeDependencyProps = {
4
+ foo: Foo;
5
+ foo2: Foo2;
6
+ };
7
+ export default function TypeDependency(props: TypeDependencyProps): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function TypeDependency(props) {
4
+ return <div>{props.foo}</div>;
5
+ }
6
+ exports.default = TypeDependency;
@@ -0,0 +1,3 @@
1
+ export interface Foo {
2
+ }
3
+ export default function MyComponent(): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ function MyComponent() {
4
+ return <div></div>;
5
+ }
6
+ exports.default = MyComponent;
@@ -7,6 +7,7 @@ var basicOnMountUpdate = require('./data/basic-onMount-update.raw');
7
7
  var basicContext = require('./data/basic-context.raw');
8
8
  var basicOutputsMeta = require('./data/basic-outputs-meta.raw');
9
9
  var basicOutputs = require('./data/basic-outputs.raw');
10
+ var subComponent = require('./data/sub-component.lite.jsx');
10
11
  var basic = require('./data/basic.raw');
11
12
  var basicMitosis = require('./data/basic-custom-mitosis-package.raw');
12
13
  var basicChildComponent = require('./data/basic-child-component.raw');
@@ -21,6 +22,7 @@ var preserveExportOrLocalStatement = require('./data/basic-preserve-export-or-lo
21
22
  var propsType = require('./data/types/component-props-type.raw');
22
23
  var propsInterface = require('./data/types/component-props-interface.raw');
23
24
  var preserveTyping = require('./data/types/preserve-typing.raw');
25
+ var typeDependency = require('./data/types/type-dependency.raw');
24
26
  var classRaw = require('./data/styles/class.raw');
25
27
  var className = require('./data/styles/className.raw');
26
28
  var classAndClassName = require('./data/styles/class-and-className.raw');
@@ -86,6 +88,8 @@ var BASIC_TESTS = {
86
88
  propsType: propsType,
87
89
  propsInterface: propsInterface,
88
90
  preserveTyping: preserveTyping,
91
+ typeDependency: typeDependency,
92
+ subComponent: subComponent,
89
93
  propsDestructure: propsDestructure,
90
94
  'onInit & onMount': onInitonMount,
91
95
  'Basic Context': basicContext,
@@ -1,4 +1,5 @@
1
1
  import { BaseTranspilerOptions, Transpiler } from '../types/transpiler';
2
2
  export interface ToSolidOptions extends BaseTranspilerOptions {
3
+ stylesType?: 'styled-components' | 'style-tag';
3
4
  }
4
- export declare const componentToSolid: (options?: ToSolidOptions) => Transpiler;
5
+ export declare const componentToSolid: (_options?: ToSolidOptions) => Transpiler;
@@ -48,6 +48,7 @@ var babel_transform_1 = require("../helpers/babel-transform");
48
48
  var core_1 = require("@babel/core");
49
49
  var lodash_1 = require("lodash");
50
50
  var state_1 = require("../helpers/state");
51
+ var collect_css_1 = require("../helpers/styles/collect-css");
51
52
  // Transform <foo.bar key="value" /> to <component :is="foo.bar" key="value" />
52
53
  function processDynamicComponents(json, options) {
53
54
  var found = false;
@@ -71,7 +72,7 @@ function getContextString(component, options) {
71
72
  }
72
73
  // This should really be a preprocessor mapping the `class` attribute binding based on what other values have
73
74
  // to make this more pluggable
74
- var collectClassString = function (json) {
75
+ var collectClassString = function (json, options) {
75
76
  var _a, _b, _c;
76
77
  var staticClasses = [];
77
78
  if (json.properties.class) {
@@ -91,7 +92,9 @@ var collectClassString = function (json) {
91
92
  dynamicClasses.push(json.bindings.className.code);
92
93
  delete json.bindings.className;
93
94
  }
94
- if (typeof ((_c = json.bindings.css) === null || _c === void 0 ? void 0 : _c.code) === 'string' && json.bindings.css.code.trim().length > 4) {
95
+ if (typeof ((_c = json.bindings.css) === null || _c === void 0 ? void 0 : _c.code) === 'string' &&
96
+ json.bindings.css.code.trim().length > 4 &&
97
+ options.stylesType === 'styled-components') {
95
98
  dynamicClasses.push("css(".concat(json.bindings.css.code, ")"));
96
99
  }
97
100
  delete json.bindings.css;
@@ -137,7 +140,7 @@ var blockToSolid = function (json, options) {
137
140
  if (json.name === 'Show' && json.meta.else) {
138
141
  str += "fallback={".concat(blockToSolid(json.meta.else, options), "}");
139
142
  }
140
- var classString = collectClassString(json);
143
+ var classString = collectClassString(json, options);
141
144
  if (classString) {
142
145
  str += " class=".concat(classString, " ");
143
146
  }
@@ -222,23 +225,25 @@ function addProviderComponents(json, options) {
222
225
  ];
223
226
  }
224
227
  }
225
- var componentToSolid = function (options) {
226
- if (options === void 0) { options = {}; }
228
+ var componentToSolid = function (_options) {
229
+ if (_options === void 0) { _options = {}; }
227
230
  return function (_a) {
228
231
  var _b, _c, _d;
229
232
  var component = _a.component;
233
+ var options = __assign({ stylesType: 'styled-components' }, _options);
230
234
  var json = (0, fast_clone_1.fastClone)(component);
231
235
  if (options.plugins) {
232
236
  json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
233
237
  }
234
238
  addProviderComponents(json, options);
235
239
  var componentHasStyles = (0, helpers_1.hasCss)(json);
236
- var addWrapper = json.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes).length !== 1;
240
+ var addWrapper = json.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes).length !== 1 || options.stylesType === 'style-tag';
237
241
  if (options.plugins) {
238
242
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
239
243
  }
240
244
  (0, strip_meta_properties_1.stripMetaProperties)(json);
241
245
  var foundDynamicComponents = processDynamicComponents(json, options);
246
+ var css = options.stylesType === 'style-tag' && (0, collect_css_1.collectCss)(json);
242
247
  var stateString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json);
243
248
  var hasState = (0, state_1.checkHasState)(json);
244
249
  var componentsUsed = (0, get_components_used_1.getComponentsUsed)(json);
@@ -251,9 +256,11 @@ var componentToSolid = function (options) {
251
256
  hasForComponent ? 'For' : undefined,
252
257
  ((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? 'onMount' : undefined
253
258
  ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect'] : []), true).filter(Boolean);
254
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n export default ", ";\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n export default ", ";\n "])), solidJSImports.length > 0
259
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "])), solidJSImports.length > 0
255
260
  ? "import { \n ".concat(solidJSImports.map(function (item) { return item; }).join(', '), "\n } from 'solid-js';")
256
- : '', !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", !hasState ? '' : "import { createMutable } from 'solid-js/store';", !componentHasStyles ? '' : "import { css } from \"solid-styled-components\";", (0, render_imports_1.renderPreComponent)({ component: json, target: 'solid' }), json.name, !hasState ? '' : "const state = createMutable(".concat(stateString, ");"), getRefsString(json), getContextString(json, options), !((_d = json.hooks.onMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), json.hooks.onUpdate
261
+ : '', !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", !hasState ? '' : "import { createMutable } from 'solid-js/store';", !componentHasStyles && options.stylesType === 'styled-components'
262
+ ? ''
263
+ : "import { css } from \"solid-styled-components\";", (0, render_imports_1.renderPreComponent)({ component: json, target: 'solid' }), json.name, !hasState ? '' : "const state = createMutable(".concat(stateString, ");"), getRefsString(json), getContextString(json, options), !((_d = json.hooks.onMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), json.hooks.onUpdate
257
264
  ? json.hooks.onUpdate
258
265
  .map(function (hook, index) {
259
266
  if (hook.deps) {
@@ -269,7 +276,10 @@ var componentToSolid = function (options) {
269
276
  : '', addWrapper ? '<>' : '', json.children
270
277
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
271
278
  .map(function (item) { return blockToSolid(item, options); })
272
- .join('\n'), addWrapper ? '</>' : '', json.name);
279
+ .join('\n'), options.stylesType === 'style-tag' && css && css.trim().length > 4
280
+ ? // We add the jsx attribute so prettier formats this nicely
281
+ "<style jsx>{`".concat(css, "`}</style>")
282
+ : '', addWrapper ? '</>' : '', json.name);
273
283
  // HACK: for some reason we are generating `state.state.foo` instead of `state.foo`
274
284
  // need a full fix, but this unblocks a lot in the short term
275
285
  str = str.replace(/state\.state\./g, 'state.');
@@ -279,7 +289,7 @@ var componentToSolid = function (options) {
279
289
  if (options.prettier !== false) {
280
290
  str = (0, standalone_1.format)(str, {
281
291
  parser: 'typescript',
282
- plugins: [require('prettier/parser-typescript')],
292
+ plugins: [require('prettier/parser-typescript'), require('prettier/parser-postcss')],
283
293
  });
284
294
  }
285
295
  if (options.plugins) {
@@ -64,6 +64,7 @@ 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
66
  var get_custom_imports_1 = require("../helpers/get-custom-imports");
67
+ var slots_1 = require("../helpers/slots");
67
68
  function encodeQuotes(string) {
68
69
  return string.replace(/"/g, '&quot;');
69
70
  }
@@ -122,7 +123,7 @@ var NODE_MAPPERS = {
122
123
  switch (options.vueVersion) {
123
124
  case 3:
124
125
  return "\n <template ".concat(SPECIAL_PROPERTIES.V_IF, "=\"").concat(encodeQuotes(ifValue), "\">\n ").concat(json.children.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'), "\n </template>\n ").concat((0, is_mitosis_node_1.isMitosisNode)(json.meta.else)
125
- ? "\n <template ".concat(SPECIAL_PROPERTIES.V_ELSE, "> \n ").concat((0, exports.blockToVue)(json.meta.else, options), "\n </template>")
126
+ ? "\n <template ".concat(SPECIAL_PROPERTIES.V_ELSE, ">\n ").concat((0, exports.blockToVue)(json.meta.else, options), "\n </template>")
126
127
  : '', "\n ");
127
128
  case 2:
128
129
  // Vue 2 can only handle one root element, so we just take the first one.
@@ -165,7 +166,7 @@ var NODE_MAPPERS = {
165
166
  var elseString = firstChildOfFirstChild
166
167
  ? (0, function_1.pipe)(firstChildOfFirstChild, addPropertiesToJson((_c = {}, _c[SPECIAL_PROPERTIES.V_ELSE] = '', _c)), function (block) { return (0, exports.blockToVue)(block, options); })
167
168
  : '';
168
- return "\n \n ".concat(ifString, "\n \n ").concat(elseIfString, "\n \n ").concat(elseString, "\n \n ");
169
+ return "\n\n ".concat(ifString, "\n\n ").concat(elseIfString, "\n\n ").concat(elseString, "\n\n ");
169
170
  }
170
171
  else {
171
172
  var ifString = firstChild
@@ -259,7 +260,7 @@ var blockToVue = function (node, options, scope) {
259
260
  return nodeMapper(node, options, scope);
260
261
  }
261
262
  if ((0, is_children_1.default)(node)) {
262
- return "<slot></slot>";
263
+ return "<slot/>";
263
264
  }
264
265
  if (node.name === 'style') {
265
266
  // Vue doesn't allow <style>...</style> in templates, but does support the synonymous
@@ -270,8 +271,13 @@ var blockToVue = function (node, options, scope) {
270
271
  if (node.properties._text) {
271
272
  return "".concat(node.properties._text);
272
273
  }
273
- if ((_a = node.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
274
- return "{{".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(node.bindings._text.code), "}}");
274
+ var textCode = (_a = node.bindings._text) === null || _a === void 0 ? void 0 : _a.code;
275
+ if (textCode) {
276
+ var strippedTextCode = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(textCode);
277
+ if ((0, slots_1.isSlotProperty)(strippedTextCode)) {
278
+ return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(strippedTextCode).toLowerCase(), "\"/>");
279
+ }
280
+ return "{{".concat(strippedTextCode, "}}");
275
281
  }
276
282
  var str = '';
277
283
  str += "<".concat(node.name, " ");
@@ -452,7 +458,7 @@ var componentToVue = function (userOptions) {
452
458
  .join('\n');
453
459
  var includeClassMapHelper = template.includes('_classStringToObject');
454
460
  if (includeClassMapHelper) {
455
- functionsString = functionsString.replace(/}\s*$/, "_classStringToObject(str) {\n const obj = {};\n if (typeof str !== 'string') { return obj }\n const classNames = str.trim().split(/\\s+/); \n for (const name of classNames) {\n obj[name] = true;\n } \n return obj;\n } }");
461
+ functionsString = functionsString.replace(/}\s*$/, "_classStringToObject(str) {\n const obj = {};\n if (typeof str !== 'string') { return obj }\n const classNames = str.trim().split(/\\s+/);\n for (const name of classNames) {\n obj[name] = true;\n }\n return obj;\n } }");
456
462
  }
457
463
  if (localVarAsFunc.length) {
458
464
  functionsString = functionsString.replace(/}\s*$/, "".concat(localVarAsFunc.join(','), "}"));
@@ -4,10 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.tryParseJson = void 0;
7
- var json_6_1 = __importDefault(require("json-6"));
7
+ var json5_1 = __importDefault(require("json5"));
8
8
  var tryParseJson = function (jsonStr) {
9
9
  try {
10
- return json_6_1.default.parse(jsonStr);
10
+ return json5_1.default.parse(jsonStr);
11
11
  }
12
12
  catch (err) {
13
13
  console.error('Could not JSON5 parse object:\n', jsonStr);
@@ -1 +1,2 @@
1
1
  export declare const isSlotProperty: (key: string) => boolean;
2
+ export declare const stripSlotPrefix: (key: string) => string;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isSlotProperty = void 0;
3
+ exports.stripSlotPrefix = exports.isSlotProperty = void 0;
4
4
  var SLOT_PREFIX = 'slot';
5
5
  var isSlotProperty = function (key) { return key.startsWith(SLOT_PREFIX); };
6
6
  exports.isSlotProperty = isSlotProperty;
7
+ var stripSlotPrefix = function (key) { return key.substring(SLOT_PREFIX.length); };
8
+ exports.stripSlotPrefix = stripSlotPrefix;
@@ -1,6 +1,7 @@
1
1
  import * as babel from '@babel/core';
2
2
  import { Context } from './types';
3
3
  export declare const getPropsTypeRef: (node: babel.types.FunctionDeclaration) => string | undefined;
4
+ export declare const isTypeImport: (node: babel.Node) => boolean;
4
5
  export declare const isTypeOrInterface: (node: babel.Node) => boolean;
5
6
  export declare const collectTypes: (node: babel.Node, context: Context) => void;
6
7
  export declare const collectInterfaces: (node: babel.Node, context: Context) => void;
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.collectInterfaces = exports.collectTypes = exports.isTypeOrInterface = exports.getPropsTypeRef = void 0;
29
+ exports.collectInterfaces = exports.collectTypes = exports.isTypeOrInterface = exports.isTypeImport = exports.getPropsTypeRef = void 0;
30
30
  var babel = __importStar(require("@babel/core"));
31
31
  var generator_1 = __importDefault(require("@babel/generator"));
32
32
  var types = babel.types;
@@ -46,6 +46,14 @@ var getPropsTypeRef = function (node) {
46
46
  return undefined;
47
47
  };
48
48
  exports.getPropsTypeRef = getPropsTypeRef;
49
+ var isTypeImport = function (node) {
50
+ var _a;
51
+ return types.isImportDeclaration(node) &&
52
+ node.importKind === 'type' &&
53
+ // Babel adds an implicit JSX type import that we don't want
54
+ ((_a = node.specifiers[0]) === null || _a === void 0 ? void 0 : _a.local.name) !== 'JSX';
55
+ };
56
+ exports.isTypeImport = isTypeImport;
49
57
  var isTypeOrInterface = function (node) {
50
58
  return types.isTSTypeAliasDeclaration(node) ||
51
59
  types.isTSInterfaceDeclaration(node) ||
@@ -506,6 +506,17 @@ function parseJsx(jsx, options) {
506
506
  component: (0, create_mitosis_component_1.createMitosisComponent)(),
507
507
  };
508
508
  var keepStatements = path.node.body.filter(function (statement) { return isImportOrDefaultExport(statement) || (0, component_types_1.isTypeOrInterface)(statement); });
509
+ for (var _i = 0, _a = path.node.body; _i < _a.length; _i++) {
510
+ var statement = _a[_i];
511
+ if ((0, component_types_1.isTypeImport)(statement)) {
512
+ var importDeclaration = statement;
513
+ // Remove .lite from path if exists, as that will be stripped
514
+ if (importDeclaration.source.value.endsWith('.lite')) {
515
+ importDeclaration.source.value = importDeclaration.source.value.replace(/\.lite$/, '');
516
+ }
517
+ (0, component_types_1.collectTypes)(statement, context);
518
+ }
519
+ }
509
520
  var exportsOrLocalVariables = path.node.body.filter(function (statement) {
510
521
  return !isImportOrDefaultExport(statement) &&
511
522
  !(0, component_types_1.isTypeOrInterface)(statement) &&
@@ -14,7 +14,7 @@ export declare const targets: {
14
14
  liquid: (options?: import("./generators/liquid").ToLiquidOptions) => import(".").Transpiler;
15
15
  react: (reactOptions?: import("./generators/react").ToReactOptions) => import(".").Transpiler;
16
16
  reactNative: (options?: import("./generators/react-native").ToReactNativeOptions) => import(".").Transpiler;
17
- solid: (options?: import("./generators/solid").ToSolidOptions) => import(".").Transpiler;
17
+ solid: (_options?: import("./generators/solid").ToSolidOptions) => import(".").Transpiler;
18
18
  svelte: ({ plugins, ...userProvidedOptions }?: import("./generators/svelte").ToSvelteOptions) => import(".").Transpiler;
19
19
  swift: (options?: import("./generators/swift-ui").ToSwiftOptions) => import(".").Transpiler;
20
20
  template: (options?: import("./generators/template").ToTemplateOptions) => import(".").Transpiler;