@builder.io/mitosis 0.0.56-95 → 0.0.56-98

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 (30) hide show
  1. package/dist/src/__tests__/data/blocks/builder-render-block.raw.d.ts +6 -0
  2. package/dist/src/__tests__/data/blocks/builder-render-block.raw.jsx +90 -0
  3. package/dist/src/__tests__/data/blocks/builder-render-content.raw.d.ts +10 -0
  4. package/dist/src/__tests__/data/blocks/builder-render-content.raw.jsx +69 -0
  5. package/dist/src/__tests__/data/default-props/default-props.raw.d.ts +7 -0
  6. package/dist/src/__tests__/data/default-props/default-props.raw.jsx +23 -0
  7. package/dist/src/__tests__/data/types/component-with-default-values-types.raw.d.ts +5 -0
  8. package/dist/src/__tests__/data/types/component-with-default-values-types.raw.jsx +9 -0
  9. package/dist/src/__tests__/shared.js +3 -0
  10. package/dist/src/constants/hooks.d.ts +1 -0
  11. package/dist/src/constants/hooks.js +1 -0
  12. package/dist/src/generators/angular.js +7 -2
  13. package/dist/src/generators/lit/generate.d.ts +1 -0
  14. package/dist/src/generators/lit/generate.js +5 -3
  15. package/dist/src/generators/qwik/component-generator.js +7 -0
  16. package/dist/src/generators/qwik/convertMethodToFunction.d.ts +1 -0
  17. package/dist/src/generators/qwik/convertMethodToFunction.js +161 -0
  18. package/dist/src/generators/react.js +5 -6
  19. package/dist/src/generators/svelte.js +4 -0
  20. package/dist/src/generators/vue.js +28 -16
  21. package/dist/src/helpers/get-custom-imports.js +7 -1
  22. package/dist/src/helpers/styles/helpers.d.ts +0 -1
  23. package/dist/src/index.d.ts +3 -0
  24. package/dist/src/index.js +3 -1
  25. package/dist/src/parsers/builder.d.ts +2 -0
  26. package/dist/src/parsers/jsx/jsx.js +19 -4
  27. package/dist/src/types/mitosis-component.d.ts +1 -0
  28. package/dist/tsconfig.build.tsbuildinfo +1 -1
  29. package/dist/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +2 -2
@@ -0,0 +1,6 @@
1
+ import { BuilderBlock } from '../types/builder-block';
2
+ export declare type RenderBlockProps = {
3
+ block: BuilderBlock;
4
+ index: number;
5
+ };
6
+ export default function RenderBlock(props: RenderBlockProps): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
@@ -0,0 +1,90 @@
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
+ var mitosis_1 = require("@builder.io/mitosis");
7
+ var get_block_component_options_1 = require("../functions/get-block-component-options");
8
+ var get_block_properties_1 = require("../functions/get-block-properties");
9
+ var get_block_styles_1 = require("../functions/get-block-styles");
10
+ var get_block_tag_1 = require("../functions/get-block-tag");
11
+ var register_component_1 = require("../functions/register-component");
12
+ var builder_context_lite_1 = __importDefault(require("../context/builder.context.lite"));
13
+ var get_block_actions_1 = require("../functions/get-block-actions");
14
+ var get_processed_block_1 = require("../functions/get-processed-block");
15
+ var block_styles_lite_1 = __importDefault(require("./block-styles.lite"));
16
+ function RenderBlock(props) {
17
+ var builderContext = (0, mitosis_1.useContext)(builder_context_lite_1.default);
18
+ var state = (0, mitosis_1.useStore)({
19
+ get component() {
20
+ var _a, _b;
21
+ var componentName = (_a = state.useBlock.component) === null || _a === void 0 ? void 0 : _a.name;
22
+ if (!componentName) {
23
+ return null;
24
+ }
25
+ var ref = register_component_1.components[(_b = state.useBlock.component) === null || _b === void 0 ? void 0 : _b.name];
26
+ if (componentName && !ref) {
27
+ // TODO: Public doc page with more info about this message
28
+ console.warn("\n Could not find a registered component named \"".concat(componentName, "\".\n If you registered it, is the file that registered it imported by the file that needs to render it?"));
29
+ }
30
+ return ref;
31
+ },
32
+ get componentInfo() {
33
+ var _a;
34
+ return (_a = state.component) === null || _a === void 0 ? void 0 : _a.info;
35
+ },
36
+ get componentRef() {
37
+ var _a;
38
+ return (_a = state.component) === null || _a === void 0 ? void 0 : _a.component;
39
+ },
40
+ get tagName() {
41
+ return (0, get_block_tag_1.getBlockTag)(state.useBlock);
42
+ },
43
+ get properties() {
44
+ return (0, get_block_properties_1.getBlockProperties)(state.useBlock);
45
+ },
46
+ get useBlock() {
47
+ return (0, get_processed_block_1.getProcessedBlock)({
48
+ block: props.block,
49
+ state: builderContext.state,
50
+ context: builderContext.context,
51
+ });
52
+ },
53
+ get actions() {
54
+ return (0, get_block_actions_1.getBlockActions)({
55
+ block: state.useBlock,
56
+ state: builderContext.state,
57
+ context: builderContext.context,
58
+ });
59
+ },
60
+ get css() {
61
+ return (0, get_block_styles_1.getBlockStyles)(state.useBlock);
62
+ },
63
+ get componentOptions() {
64
+ return (0, get_block_component_options_1.getBlockComponentOptions)(state.useBlock);
65
+ },
66
+ });
67
+ return (<>
68
+ {/* TODO: add the below back when support `else` */}
69
+ {/* <Show when={state.componentInfo?.noWrap}>
70
+ <state.componentRef
71
+ attributes={state.properties}
72
+ {...state.componentInfo?.options}
73
+ style={state.css}
74
+ children={state.useBlock.children}
75
+ />
76
+ </Show>
77
+ <Show when={!state.componentInfo?.noWrap}> */}
78
+ <state.tagName {...state.properties} style={state.css}>
79
+ <block_styles_lite_1.default block={state.useBlock}/>
80
+ {state.componentRef && (<state.componentRef {...state.componentOptions} children={state.useBlock.children}/>)}
81
+ <mitosis_1.Show when={!state.componentRef && state.useBlock.children && state.useBlock.children.length}>
82
+ <mitosis_1.For each={state.useBlock.children}>
83
+ {function (child, index) { return <RenderBlock index={index} block={child}/>; }}
84
+ </mitosis_1.For>
85
+ </mitosis_1.Show>
86
+ </state.tagName>
87
+ {/* </Show> */}
88
+ </>);
89
+ }
90
+ exports.default = RenderBlock;
@@ -0,0 +1,10 @@
1
+ import { BuilderContent, GetContentOptions } from '@builder.io/sdk';
2
+ declare type RenderContentProps = {
3
+ model: string;
4
+ options?: GetContentOptions;
5
+ content?: BuilderContent;
6
+ children: any;
7
+ contentLoaded?: (content: BuilderContent) => void;
8
+ };
9
+ export declare function RenderContent(props: RenderContentProps): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
10
+ export {};
@@ -0,0 +1,69 @@
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.RenderContent = void 0;
7
+ var mitosis_1 = require("@builder.io/mitosis");
8
+ var sdk_1 = require("@builder.io/sdk");
9
+ var builder_render_block_raw_1 = __importDefault(require("./builder-render-block.raw"));
10
+ function RenderContent(props) {
11
+ var content = props.content;
12
+ var state = (0, mitosis_1.useStore)({
13
+ get css() {
14
+ return '';
15
+ },
16
+ onWindowMessage: function (event) {
17
+ var _a;
18
+ var message = event.data;
19
+ if (!message) {
20
+ return;
21
+ }
22
+ switch (message.type) {
23
+ case 'builder.patchUpdates': {
24
+ var data = message.data;
25
+ if (!content) {
26
+ return;
27
+ }
28
+ if (!(data && data.data)) {
29
+ break;
30
+ }
31
+ var patches = data.data[(_a = content.data) === null || _a === void 0 ? void 0 : _a.id];
32
+ if (!(patches && patches.length)) {
33
+ return;
34
+ }
35
+ if (props.contentLoaded) {
36
+ props.contentLoaded(content);
37
+ }
38
+ break;
39
+ }
40
+ }
41
+ },
42
+ });
43
+ (0, mitosis_1.onMount)(function () {
44
+ if (sdk_1.Builder.isEditing) {
45
+ addEventListener('message', state.onWindowMessage);
46
+ }
47
+ });
48
+ (0, mitosis_1.onUnMount)(function () {
49
+ if (sdk_1.Builder.isEditing) {
50
+ removeEventListener('message', state.onWindowMessage);
51
+ }
52
+ });
53
+ return (<>
54
+ <mitosis_1.Show when={!content}>{props.children}</mitosis_1.Show>
55
+ <mitosis_1.Show when={content}>
56
+ <div data-builder-model-name={props.model} data-builder-component={content.name} data-builder-content-id={content.id} data-builder-variation-id={content.testVariationId || content.id}>
57
+ <mitosis_1.Show when={state.css}>
58
+ <style innerHTML={state.css}/>¸
59
+ </mitosis_1.Show>
60
+ <mitosis_1.Show when={content.data.blocks}>
61
+ <mitosis_1.For each={content.data.blocks}>
62
+ {function (block, index) { return <builder_render_block_raw_1.default block={block} index={index}/>; }}
63
+ </mitosis_1.For>
64
+ </mitosis_1.Show>
65
+ </div>
66
+ </mitosis_1.Show>
67
+ </>);
68
+ }
69
+ exports.RenderContent = RenderContent;
@@ -0,0 +1,7 @@
1
+ export interface ButtonProps {
2
+ attributes?: any;
3
+ text?: string;
4
+ link?: string;
5
+ openLinkInNewTab?: boolean;
6
+ }
7
+ export default function Button(props: ButtonProps): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var mitosis_1 = require("@builder.io/mitosis");
4
+ function Button(props) {
5
+ (0, mitosis_1.useDefaultProps)({
6
+ text: 'default text',
7
+ link: 'https://builder.io/',
8
+ openLinkInNewTab: false,
9
+ });
10
+ return (<>
11
+ <mitosis_1.Show when={props.link}>
12
+ <a {...props.attributes} href={props.link} target={props.openLinkInNewTab ? '_blank' : undefined}>
13
+ {props.text}
14
+ </a>
15
+ </mitosis_1.Show>
16
+ <mitosis_1.Show when={!props.link}>
17
+ <button {...props.attributes} type="button">
18
+ {props.text}
19
+ </button>
20
+ </mitosis_1.Show>
21
+ </>);
22
+ }
23
+ exports.default = Button;
@@ -0,0 +1,5 @@
1
+ declare type Props = {
2
+ name: string;
3
+ };
4
+ export default function ComponentWithTypes(props: Props): import("@builder.io/mitosis/jsx-runtime").JSX.Element;
5
+ export {};
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var DEFAULT_VALUES = {
4
+ name: 'Sami',
5
+ };
6
+ function ComponentWithTypes(props) {
7
+ return <div> Hello {props.name || DEFAULT_VALUES.name}</div>;
8
+ }
9
+ exports.default = ComponentWithTypes;
@@ -24,6 +24,7 @@ var propsType = require('./data/types/component-props-type.raw');
24
24
  var propsInterface = require('./data/types/component-props-interface.raw');
25
25
  var preserveTyping = require('./data/types/preserve-typing.raw');
26
26
  var typeDependency = require('./data/types/type-dependency.raw');
27
+ var defaultProps = require('./data/default-props/default-props.raw');
27
28
  var classRaw = require('./data/styles/class.raw');
28
29
  var className = require('./data/styles/className.raw');
29
30
  var classAndClassName = require('./data/styles/class-and-className.raw');
@@ -88,8 +89,10 @@ var BASIC_TESTS = {
88
89
  onMount: onMount,
89
90
  propsType: propsType,
90
91
  propsInterface: propsInterface,
92
+ defaultProps: defaultProps,
91
93
  preserveTyping: preserveTyping,
92
94
  typeDependency: typeDependency,
95
+ defaultValsWithTypes: require('./data/types/component-with-default-values-types.raw'),
93
96
  subComponent: subComponent,
94
97
  nestedStyles: nestedStyles,
95
98
  propsDestructure: propsDestructure,
@@ -3,4 +3,5 @@ export declare const HOOKS: {
3
3
  readonly STATE: "useState";
4
4
  readonly CONTEXT: "useContext";
5
5
  readonly REF: "useRef";
6
+ readonly DEFAULT_PROPS: "useDefaultProps";
6
7
  };
@@ -6,4 +6,5 @@ exports.HOOKS = {
6
6
  STATE: 'useState',
7
7
  CONTEXT: 'useContext',
8
8
  REF: 'useRef',
9
+ DEFAULT_PROPS: 'useDefaultProps',
9
10
  };
@@ -18,6 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.componentToAngular = exports.blockToAngular = void 0;
20
20
  var dedent_1 = __importDefault(require("dedent"));
21
+ var json5_1 = __importDefault(require("json5"));
21
22
  var standalone_1 = require("prettier/standalone");
22
23
  var collect_css_1 = require("../helpers/styles/collect-css");
23
24
  var fast_clone_1 = require("../helpers/fast-clone");
@@ -292,7 +293,7 @@ var componentToAngular = function (options) {
292
293
  });
293
294
  },
294
295
  });
295
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { ", " ", " Component ", "", " } from '@angular/core';\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 @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') : '', (0, render_imports_1.renderPreComponent)({
296
+ 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.defaultProps ? '' : "const defaultProps = ".concat(json5_1.default.stringify(json.defaultProps), "\n"), (0, render_imports_1.renderPreComponent)({
296
297
  component: json,
297
298
  target: 'angular',
298
299
  excludeMitosisComponents: !options.standalone,
@@ -305,7 +306,11 @@ var componentToAngular = function (options) {
305
306
  .filter(function (item) { return !(0, slots_1.isSlotProperty)(item) && item !== 'children'; })
306
307
  .map(function (item) {
307
308
  var propType = propsTypeRef ? "".concat(propsTypeRef, "[\"").concat(item, "\"]") : 'any';
308
- return "@Input() ".concat(item, ": ").concat(propType);
309
+ var propDeclaration = "@Input() ".concat(item, ": ").concat(propType);
310
+ if (json.defaultProps && json.defaultProps.hasOwnProperty(item)) {
311
+ propDeclaration += " = defaultProps[\"".concat(item, "\"]");
312
+ }
313
+ return propDeclaration;
309
314
  })
310
315
  .join('\n'), outputs.join('\n'), Array.from(domRefs)
311
316
  .map(function (refName) { return "@ViewChild('".concat(refName, "') ").concat(refName, ": ElementRef"); })
@@ -1,4 +1,5 @@
1
1
  import { BaseTranspilerOptions, Transpiler } from '../../types/transpiler';
2
2
  export interface ToLitOptions extends BaseTranspilerOptions {
3
+ useShadowDom?: boolean;
3
4
  }
4
5
  export declare const componentToLit: (options?: ToLitOptions) => Transpiler;
@@ -175,9 +175,11 @@ var componentToLit = function (options) {
175
175
  html = html.replace(/\n{3,}/g, '\n\n');
176
176
  }
177
177
  }
178
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n import { LitElement, html, css } from 'lit';\n import { customElement, property, state, query } from 'lit/decorators.js';\n\n ", "\n ", "\n\n @customElement('", "')\n export default class ", " extends LitElement {\n // Do not use Shadow DOM\n createRenderRoot() {\n return this;\n }\n\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html`\n ", "\n `\n }\n }\n "], ["\n ", "\n import { LitElement, html, css } from 'lit';\n import { customElement, property, state, query } from 'lit/decorators.js';\n\n ", "\n ", "\n\n @customElement('", "')\n export default class ", " extends LitElement {\n // Do not use Shadow DOM\n createRenderRoot() {\n return this;\n }\n\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html\\`\n ", "\n \\`\n }\n }\n "])), (0, render_imports_1.renderPreComponent)({ component: json, target: 'lit' }), json.types ? json.types.join('\n') : '', hasSpread
178
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n import { LitElement, html, css } from 'lit';\n import { customElement, property, state, query } from 'lit/decorators.js';\n\n ", "\n ", "\n\n @customElement('", "')\n export default class ", " extends LitElement {\n ", "\n\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html`\n ", "\n }\n ", "\n `\n }\n }\n "], ["\n ", "\n import { LitElement, html, css } from 'lit';\n import { customElement, property, state, query } from 'lit/decorators.js';\n\n ", "\n ", "\n\n @customElement('", "')\n export default class ", " extends LitElement {\n ", "\n\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html\\`\n ", "\n }\n ", "\n \\`\n }\n }\n "])), (0, render_imports_1.renderPreComponent)({ component: json, target: 'lit' }), json.types ? json.types.join('\n') : '', hasSpread
179
179
  ? "\n const spread = (properties) =>\n directive((part) => {\n for (const property in properties) {\n const value = properties[attr];\n part.element[property] = value;\n }\n });\n "
180
- : '', ((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.tagName) || getCustomTagName(json.name, options), json.name, css.length
180
+ : '', ((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.tagName) || getCustomTagName(json.name, options), json.name, options.useShadowDom
181
+ ? ''
182
+ : "\n createRenderRoot() {\n return this;\n }\n ", options.useShadowDom && css.length
181
183
  ? "static styles = css`\n ".concat((0, indent_1.indent)(css, 8), "`;")
182
184
  : '', Array.from(domRefs)
183
185
  .map(function (refName) { return "\n @query('[ref=\"".concat(refName, "\"]')\n ").concat((0, lodash_1.camelCase)(refName), "!: HTMLElement;\n "); })
@@ -189,7 +191,7 @@ var componentToLit = function (options) {
189
191
  ? ''
190
192
  : "disconnectedCallback() { ".concat(processBinding(json.hooks.onUnMount.code), " }"), !((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length)
191
193
  ? ''
192
- : "updated() { \n ".concat(json.hooks.onUpdate.map(function (hook) { return processBinding(hook.code); }).join('\n\n'), " \n }"), (0, indent_1.indent)(html, 8));
194
+ : "updated() { \n ".concat(json.hooks.onUpdate.map(function (hook) { return processBinding(hook.code); }).join('\n\n'), " \n }"), options.useShadowDom || !css.length ? '' : "<style>".concat(css, "</style>"), (0, indent_1.indent)(html, 8));
193
195
  if (options.plugins) {
194
196
  str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
195
197
  }
@@ -18,6 +18,7 @@ var add_prevent_default_1 = require("./add-prevent-default");
18
18
  var convert_method_to_function_1 = require("./convert-method-to-function");
19
19
  var jsx_1 = require("./jsx");
20
20
  var src_generator_1 = require("./src-generator");
21
+ var plugins_1 = require("../../modules/plugins");
21
22
  Error.stackTraceLimit = 9999;
22
23
  // TODO(misko): styles are not processed.
23
24
  var DEBUG = false;
@@ -28,7 +29,13 @@ var componentToQwik = function (userOptions) {
28
29
  var _component = _a.component, path = _a.path;
29
30
  // Make a copy we can safely mutate, similar to babel's toolchain
30
31
  var component = (0, fast_clone_1.fastClone)(_component);
32
+ if (userOptions.plugins) {
33
+ component = (0, plugins_1.runPreJsonPlugins)(component, userOptions.plugins);
34
+ }
31
35
  (0, add_prevent_default_1.addPreventDefault)(component);
36
+ if (userOptions.plugins) {
37
+ component = (0, plugins_1.runPostJsonPlugins)(component, userOptions.plugins);
38
+ }
32
39
  var file = new src_generator_1.File(component.name + '.js', {
33
40
  isPretty: true,
34
41
  isJSX: true,
@@ -0,0 +1 @@
1
+ export declare function convertMethodToFunction(code: string, methods: Record<string, 'method' | 'getter'>, lexicalArgs: string[]): string;
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertMethodToFunction = void 0;
4
+ function convertMethodToFunction(code, methods, lexicalArgs) {
5
+ var out = [];
6
+ var idx = 0;
7
+ var lastIdx = idx;
8
+ var end = code.length;
9
+ var templateDepth = 0;
10
+ var mode = "code" /* Mode.code */;
11
+ var braceDepth = 0;
12
+ var stringEndBraceDepth = -1;
13
+ var stringEndBraceDepthQueue = [];
14
+ var lastCh = null;
15
+ while (idx < end) {
16
+ var ch = code.charCodeAt(idx++);
17
+ // console.log(mode, code[idx - 1]);
18
+ switch (mode) {
19
+ case "code" /* Mode.code */:
20
+ if (ch === QUOTE_DOUBLE) {
21
+ mode = "stringDouble" /* Mode.stringDouble */;
22
+ }
23
+ else if (ch === QUOTE_SINGLE) {
24
+ mode = "stringSingle" /* Mode.stringSingle */;
25
+ }
26
+ else if (ch === QUOTE_BACK_TICK) {
27
+ mode = "stringTemplate" /* Mode.stringTemplate */;
28
+ templateDepth++;
29
+ }
30
+ else if (ch === OPEN_BRACE) {
31
+ braceDepth++;
32
+ }
33
+ else if (lastCh == FORWARD_SLASH && ch == FORWARD_SLASH) {
34
+ mode = "commentSingleline" /* Mode.commentSingleline */;
35
+ }
36
+ else if (lastCh == FORWARD_SLASH && ch == STAR) {
37
+ mode = "commentMultiline" /* Mode.commentMultiline */;
38
+ }
39
+ else if (ch === CLOSE_BRACE) {
40
+ braceDepth--;
41
+ if (braceDepth === stringEndBraceDepth) {
42
+ stringEndBraceDepth = stringEndBraceDepthQueue.pop();
43
+ templateDepth--;
44
+ mode = "stringTemplate" /* Mode.stringTemplate */;
45
+ }
46
+ }
47
+ else if (isWord(ch, code, idx, 'this') || isWord(ch, code, idx, 'state')) {
48
+ idx--;
49
+ flush();
50
+ consumeIdent();
51
+ if (code.charCodeAt(idx) == DOT) {
52
+ idx++;
53
+ var propEndIdx = findIdentEnd();
54
+ var identifier = code.substring(idx, propEndIdx);
55
+ if (identifier in methods) {
56
+ var isGetter = code.charCodeAt(propEndIdx) !== OPEN_PAREN;
57
+ lastIdx = idx = propEndIdx + (isGetter ? 0 : 1);
58
+ if (isGetter) {
59
+ out.push(identifier, "(".concat(lexicalArgs.join(','), ")"));
60
+ }
61
+ else {
62
+ out.push(identifier, "(".concat(lexicalArgs.join(','), ","));
63
+ }
64
+ }
65
+ else {
66
+ flush();
67
+ }
68
+ }
69
+ }
70
+ break;
71
+ case "commentSingleline" /* Mode.commentSingleline */:
72
+ if (ch == EOL)
73
+ mode = "code" /* Mode.code */;
74
+ break;
75
+ case "commentMultiline" /* Mode.commentMultiline */:
76
+ if (lastCh == STAR && ch == FORWARD_SLASH)
77
+ mode = "code" /* Mode.code */;
78
+ break;
79
+ case "stringSingle" /* Mode.stringSingle */:
80
+ if (lastCh !== BACKSLASH && ch == QUOTE_SINGLE)
81
+ mode = "code" /* Mode.code */;
82
+ break;
83
+ case "stringDouble" /* Mode.stringDouble */:
84
+ if (lastCh !== BACKSLASH && ch == QUOTE_DOUBLE)
85
+ mode = "code" /* Mode.code */;
86
+ break;
87
+ case "stringTemplate" /* Mode.stringTemplate */:
88
+ if (lastCh !== BACKSLASH && ch == QUOTE_BACK_TICK) {
89
+ mode = "code" /* Mode.code */;
90
+ templateDepth--;
91
+ }
92
+ else if (lastCh === DOLLAR && ch == OPEN_BRACE) {
93
+ templateDepth++;
94
+ mode = "code" /* Mode.code */;
95
+ stringEndBraceDepthQueue.push(stringEndBraceDepth);
96
+ stringEndBraceDepth = braceDepth;
97
+ braceDepth++;
98
+ }
99
+ break;
100
+ }
101
+ lastCh = ch;
102
+ }
103
+ flush();
104
+ return out.join('');
105
+ function flush() {
106
+ out.push(code.substring(lastIdx, idx));
107
+ lastIdx = idx;
108
+ }
109
+ function findIdentEnd() {
110
+ var scanIdx = idx;
111
+ while (isIdentCh(code.charCodeAt(scanIdx)) && scanIdx < end) {
112
+ scanIdx++;
113
+ }
114
+ return scanIdx;
115
+ }
116
+ function consumeIdent() {
117
+ while (isIdentCh(code.charCodeAt(idx))) {
118
+ idx++;
119
+ }
120
+ }
121
+ }
122
+ exports.convertMethodToFunction = convertMethodToFunction;
123
+ function isIdentCh(ch) {
124
+ return ((CHAR_0 <= ch && ch <= CHAR_9) ||
125
+ (CHAR_a <= ch && ch <= CHAR_z) ||
126
+ (CHAR_A <= ch && ch <= CHAR_Z) ||
127
+ ch === UNDERSCORE ||
128
+ ch === DOLLAR);
129
+ }
130
+ function isWord(ch, code, idx, word) {
131
+ if (ch !== word.charCodeAt(0))
132
+ return false;
133
+ for (var i = 1; i < word.length; i++) {
134
+ if (code.charCodeAt(idx + i - 1) !== word.charCodeAt(i)) {
135
+ return false;
136
+ }
137
+ }
138
+ if (isIdentCh(code.charCodeAt(idx + word.length - 1))) {
139
+ return false;
140
+ }
141
+ return true;
142
+ }
143
+ var QUOTE_DOUBLE = '"'.charCodeAt(0);
144
+ var QUOTE_SINGLE = "'".charCodeAt(0);
145
+ var QUOTE_BACK_TICK = '`'.charCodeAt(0);
146
+ var BACKSLASH = "\\".charCodeAt(0);
147
+ var FORWARD_SLASH = "/".charCodeAt(0);
148
+ var EOL = "\n".charCodeAt(0);
149
+ var STAR = "*".charCodeAt(0);
150
+ var CHAR_0 = "0".charCodeAt(0);
151
+ var CHAR_9 = "9".charCodeAt(0);
152
+ var CHAR_a = "a".charCodeAt(0);
153
+ var CHAR_z = "z".charCodeAt(0);
154
+ var CHAR_A = "A".charCodeAt(0);
155
+ var CHAR_Z = "Z".charCodeAt(0);
156
+ var UNDERSCORE = "_".charCodeAt(0);
157
+ var DOLLAR = "$".charCodeAt(0);
158
+ var DOT = ".".charCodeAt(0);
159
+ var OPEN_PAREN = '('.charCodeAt(0);
160
+ var OPEN_BRACE = '{'.charCodeAt(0);
161
+ var CLOSE_BRACE = '}'.charCodeAt(0);
@@ -499,11 +499,8 @@ var _componentToReact = function (json, options, isSubComponent) {
499
499
  isRootSpecialNode(json);
500
500
  var _o = getRefsString(json, allRefs, options), hasStateArgument = _o[0], refsString = _o[1];
501
501
  var nativeStyles = stylesType === 'react-native' && componentHasStyles && (0, react_native_1.collectReactNativeStyles)(json);
502
- var propsArgs = 'props';
503
- if (json.propsTypeRef) {
504
- propsArgs = "props: ".concat(json.propsTypeRef);
505
- }
506
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n "])), options.preact
502
+ var propsArgs = "props: ".concat(json.propsTypeRef || 'any');
503
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "])), options.preact
507
504
  ? "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n "
508
505
  : options.type !== 'native'
509
506
  ? "import * as React from 'react';"
@@ -533,7 +530,9 @@ var _componentToReact = function (json, options, isSubComponent) {
533
530
  ? "<style jsx>{`".concat(css, "`}</style>")
534
531
  : componentHasStyles && stylesType === 'style-tag'
535
532
  ? "<style>{`".concat(css, "`}</style>")
536
- : '', wrap ? closeFrag(options) : '', isForwardRef ? ')' : '', !nativeStyles
533
+ : '', wrap ? closeFrag(options) : '', isForwardRef ? ')' : '', !json.defaultProps
534
+ ? ''
535
+ : "".concat(json.name || 'MyComponent', ".defaultProps = ").concat(json5_1.default.stringify(json.defaultProps), ";"), !nativeStyles
537
536
  ? ''
538
537
  : "\n const styles = StyleSheet.create(".concat(json5_1.default.stringify(nativeStyles), ");\n "), styledComponentsCode ? styledComponentsCode : '');
539
538
  str = (0, replace_new_lines_in_strings_1.stripNewlinesInStrings)(str);
@@ -66,6 +66,7 @@ var function_literal_prefix_1 = require("../constants/function-literal-prefix");
66
66
  var method_literal_prefix_1 = require("../constants/method-literal-prefix");
67
67
  var patterns_1 = require("../helpers/patterns");
68
68
  var is_upper_case_1 = require("../helpers/is-upper-case");
69
+ var json5_1 = __importDefault(require("json5"));
69
70
  var mappers = {
70
71
  Fragment: function (_a) {
71
72
  var _b;
@@ -350,6 +351,9 @@ var componentToSvelte = function (_a) {
350
351
  if (json.propsTypeRef && json.propsTypeRef !== 'any') {
351
352
  propDeclaration += ": ".concat(json.propsTypeRef.split(' |')[0], "['").concat(name, "']");
352
353
  }
354
+ if (json.defaultProps && json.defaultProps.hasOwnProperty(name)) {
355
+ propDeclaration += "=".concat(json5_1.default.stringify(json.defaultProps[name]));
356
+ }
353
357
  propDeclaration += ';';
354
358
  return propDeclaration;
355
359
  })