@builder.io/mitosis 0.0.56-55 → 0.0.56-57

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/builder.d.ts +1 -1
  2. package/dist/src/generators/builder.js +2 -1
  3. package/dist/src/generators/context/react.js +1 -1
  4. package/dist/src/generators/context/solid.js +1 -1
  5. package/dist/src/generators/context/svelte.js +1 -1
  6. package/dist/src/generators/marko/generate.js +4 -22
  7. package/dist/src/generators/mitosis.js +2 -1
  8. package/dist/src/generators/qwik/component-generator.js +134 -62
  9. package/dist/src/generators/qwik/convert-method-to-function.d.ts +1 -0
  10. package/dist/src/generators/qwik/convert-method-to-function.js +162 -0
  11. package/dist/src/generators/qwik/directives.js +10 -3
  12. package/dist/src/generators/qwik/handlers.js +0 -3
  13. package/dist/src/generators/qwik/jsx.js +43 -22
  14. package/dist/src/generators/qwik/src-generator.js +12 -0
  15. package/dist/src/generators/react.d.ts +2 -0
  16. package/dist/src/generators/react.js +22 -15
  17. package/dist/src/generators/solid.js +3 -2
  18. package/dist/src/generators/svelte.js +7 -3
  19. package/dist/src/generators/swift-ui.js +2 -1
  20. package/dist/src/generators/vue.js +6 -2
  21. package/dist/src/helpers/get-state-object-string.d.ts +3 -2
  22. package/dist/src/helpers/get-state-object-string.js +15 -8
  23. package/dist/src/helpers/getters-to-functions.js +2 -1
  24. package/dist/src/helpers/handle-missing-state.js +1 -1
  25. package/dist/src/helpers/json.d.ts +3 -1
  26. package/dist/src/helpers/map-refs.js +16 -11
  27. package/dist/src/helpers/process-http-requests.js +1 -1
  28. package/dist/src/helpers/state.d.ts +4 -0
  29. package/dist/src/helpers/state.js +16 -0
  30. package/dist/src/helpers/styles/helpers.d.ts +2 -3
  31. package/dist/src/helpers/typescript.d.ts +3 -0
  32. package/dist/src/parsers/builder.d.ts +7 -6
  33. package/dist/src/parsers/builder.js +2 -1
  34. package/dist/src/parsers/jsx/ast.d.ts +3 -0
  35. package/dist/src/parsers/jsx/ast.js +74 -0
  36. package/dist/src/parsers/jsx/component-types.d.ts +6 -0
  37. package/dist/src/parsers/jsx/component-types.js +69 -0
  38. package/dist/src/parsers/jsx/context.d.ts +6 -0
  39. package/dist/src/parsers/jsx/context.js +68 -0
  40. package/dist/src/parsers/jsx/helpers.d.ts +3 -0
  41. package/dist/src/parsers/jsx/helpers.js +36 -0
  42. package/dist/src/parsers/jsx/index.d.ts +4 -0
  43. package/dist/src/parsers/jsx/index.js +25 -0
  44. package/dist/src/parsers/jsx/jsx.d.ts +10 -0
  45. package/dist/src/parsers/jsx/jsx.js +640 -0
  46. package/dist/src/parsers/jsx/metadata.d.ts +12 -0
  47. package/dist/src/parsers/jsx/metadata.js +70 -0
  48. package/dist/src/parsers/jsx/props.d.ts +2 -0
  49. package/dist/src/parsers/jsx/props.js +72 -0
  50. package/dist/src/parsers/jsx/state.d.ts +13 -0
  51. package/dist/src/parsers/jsx/state.js +163 -0
  52. package/dist/src/parsers/jsx/types.d.ts +11 -0
  53. package/dist/src/parsers/jsx/types.js +2 -0
  54. package/dist/src/targets.d.ts +2 -1
  55. package/dist/src/targets.js +2 -0
  56. package/dist/src/types/json.d.ts +5 -2
  57. package/dist/src/types/mitosis-component.d.ts +10 -2
  58. package/dist/test/qwik/Accordion/low.jsx +10 -4
  59. package/dist/test/qwik/For/low.jsx +1 -1
  60. package/dist/test/qwik/Image/med.js +1 -1
  61. package/dist/test/qwik/Image.slow/med.js +1 -1
  62. package/dist/test/qwik/bindings/low.cjs +1 -1
  63. package/dist/test/qwik/button/med.js +1 -1
  64. package/dist/test/qwik/component/bindings/low.jsx +1 -1
  65. package/dist/test/qwik/component/component/inputs/med.cjsx +2 -2
  66. package/dist/test/qwik/mount/low.cjs +1 -1
  67. package/dist/test/qwik/show-hide/med.jsx +3 -3
  68. package/dist/test/qwik/svg/low.js +1 -1
  69. package/dist/tsconfig.build.tsbuildinfo +1 -1
  70. package/dist/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +2 -2
@@ -10,7 +10,7 @@ declare type InternalOptions = {
10
10
  export declare const blockToBuilder: (json: MitosisNode, options?: ToBuilderOptions, _internalOptions?: InternalOptions) => BuilderElement;
11
11
  export declare const componentToBuilder: (options?: ToBuilderOptions) => ({ component }: TranspilerArgs) => {
12
12
  data: {
13
- httpRequests: import("../types/json").JSON;
13
+ httpRequests: import("../types/json")._JSON;
14
14
  jsCode: string;
15
15
  tsCode: string;
16
16
  blocks: BuilderElement[];
@@ -34,6 +34,7 @@ var builder_1 = require("../parsers/builder");
34
34
  var remove_surrounding_block_1 = require("../helpers/remove-surrounding-block");
35
35
  var traverse_1 = __importDefault(require("traverse"));
36
36
  var symbol_processor_1 = require("../symbols/symbol-processor");
37
+ var state_1 = require("../helpers/state");
37
38
  var omitMetaProperties = function (obj) {
38
39
  return (0, lodash_1.omitBy)(obj, function (_value, key) { return key.startsWith('$'); });
39
40
  };
@@ -233,7 +234,7 @@ var componentToBuilder = function (options) {
233
234
  return function (_a) {
234
235
  var _b, _c, _d, _e;
235
236
  var component = _a.component;
236
- var hasState = Boolean(Object.keys(component.state).length);
237
+ var hasState = (0, state_1.checkHasState)(component);
237
238
  var result = (0, fast_clone_1.fastClone)({
238
239
  data: {
239
240
  httpRequests: (_c = (_b = component === null || component === void 0 ? void 0 : component.meta) === null || _b === void 0 ? void 0 : _b.useMetadata) === null || _c === void 0 ? void 0 : _c.httpRequests,
@@ -7,7 +7,7 @@ var contextToReact = function (options) {
7
7
  if (options === void 0) { options = {}; }
8
8
  return function (_a) {
9
9
  var context = _a.context;
10
- var str = "\n import { createContext } from 'react';\n\n export default createContext(".concat((0, get_state_object_string_1.getMemberObjectString)(context.value), ")\n ");
10
+ var str = "\n import { createContext } from 'react';\n\n export default createContext(".concat((0, get_state_object_string_1.stringifyContextValue)(context.value), ")\n ");
11
11
  if (options.format !== false) {
12
12
  try {
13
13
  str = (0, standalone_1.format)(str, {
@@ -7,7 +7,7 @@ var contextToSolid = function (options) {
7
7
  if (options === void 0) { options = {}; }
8
8
  return function (_a) {
9
9
  var context = _a.context;
10
- var str = "\n import { createContext } from 'solid-js';\n\n export default createContext(".concat((0, get_state_object_string_1.getMemberObjectString)(context.value), ")\n ");
10
+ var str = "\n import { createContext } from 'solid-js';\n\n export default createContext(".concat((0, get_state_object_string_1.stringifyContextValue)(context.value), ")\n ");
11
11
  if (options.format !== false) {
12
12
  try {
13
13
  str = (0, standalone_1.format)(str, {
@@ -7,7 +7,7 @@ var contextToSvelte = function (options) {
7
7
  if (options === void 0) { options = {}; }
8
8
  return function (_a) {
9
9
  var context = _a.context;
10
- var str = "\n const key = Symbol(); \n\n export default {\n ".concat(context.name, ": ").concat((0, get_state_object_string_1.getMemberObjectString)(context.value), ", \n key \n }\n ");
10
+ var str = "\n const key = Symbol(); \n\n export default {\n ".concat(context.name, ": ").concat((0, get_state_object_string_1.stringifyContextValue)(context.value), ", \n key \n }\n ");
11
11
  if (options.prettier !== false) {
12
12
  try {
13
13
  str = (0, standalone_1.format)(str, {
@@ -34,34 +34,16 @@ var indent_1 = require("../../helpers/indent");
34
34
  var map_refs_1 = require("../../helpers/map-refs");
35
35
  var dash_case_1 = require("../../helpers/dash-case");
36
36
  var has_props_1 = require("../../helpers/has-props");
37
- var function_literal_prefix_1 = require("../../constants/function-literal-prefix");
38
- var method_literal_prefix_1 = require("../../constants/method-literal-prefix");
39
- var patterns_1 = require("../../helpers/patterns");
40
37
  var get_refs_1 = require("../../helpers/get-refs");
41
38
  // Having issues with this, so off for now
42
39
  var USE_MARKO_PRETTIER = false;
43
- function getStateTypeOfValue(value) {
44
- if (typeof value === 'string') {
45
- if (value.startsWith(function_literal_prefix_1.functionLiteralPrefix)) {
46
- return 'function';
47
- }
48
- else if (value.startsWith(method_literal_prefix_1.methodLiteralPrefix)) {
49
- var isGet = Boolean(value.replace(method_literal_prefix_1.methodLiteralPrefix, '').match(patterns_1.GETTER));
50
- if (isGet) {
51
- return 'getter';
52
- }
53
- return 'method';
54
- }
55
- }
56
- return 'property';
57
- }
58
40
  /**
59
41
  * Return the names of methods and functions on state
60
42
  */
61
43
  function getStateMethodNames(json) {
62
44
  return Object.keys(json.state).filter(function (key) {
63
- var value = json.state[key];
64
- var type = getStateTypeOfValue(value);
45
+ var _a;
46
+ var type = (_a = json.state[key]) === null || _a === void 0 ? void 0 : _a.type;
65
47
  return type === 'function' || type === 'method';
66
48
  });
67
49
  }
@@ -69,13 +51,13 @@ function getStateMethodNames(json) {
69
51
  * Return the names of getter and functions on state
70
52
  */
71
53
  function getStateGetterNames(json) {
72
- return Object.keys(json.state).filter(function (key) { return getStateTypeOfValue(json.state[key]) === 'getter'; });
54
+ return Object.keys(json.state).filter(function (key) { var _a; return ((_a = json.state[key]) === null || _a === void 0 ? void 0 : _a.type) === 'getter'; });
73
55
  }
74
56
  /**
75
57
  * Return the names of properties (basic literal values) on state
76
58
  */
77
59
  function getStatePropertyNames(json) {
78
- return Object.keys(json.state).filter(function (key) { return getStateTypeOfValue(json.state[key]) === 'property'; });
60
+ return Object.keys(json.state).filter(function (key) { var _a; return ((_a = json.state[key]) === null || _a === void 0 ? void 0 : _a.type) === 'property'; });
79
61
  }
80
62
  var blockToMarko = function (json, options) {
81
63
  var _a, _b, _c, _d, _e;
@@ -39,6 +39,7 @@ var map_refs_1 = require("../helpers/map-refs");
39
39
  var render_imports_1 = require("../helpers/render-imports");
40
40
  var jsx_1 = require("../parsers/jsx");
41
41
  var react_1 = require("./react");
42
+ var state_1 = require("../helpers/state");
42
43
  exports.DEFAULT_FORMAT = 'legacy';
43
44
  // Special isValidAttributeName for Mitosis so we can allow for $ in names
44
45
  var isValidAttributeName = function (str) {
@@ -152,7 +153,7 @@ var componentToMitosis = function (toMitosisOptions) {
152
153
  var components = Array.from((0, get_components_1.getComponents)(json));
153
154
  var mitosisComponents = components.filter(function (item) { return mitosisCoreComponents.includes(item); });
154
155
  var otherComponents = components.filter(function (item) { return !mitosisCoreComponents.includes(item); });
155
- var hasState = Boolean(Object.keys(component.state).length);
156
+ var hasState = (0, state_1.checkHasState)(component);
156
157
  var needsMitosisCoreImport = Boolean(hasState || refs.length || mitosisComponents.length);
157
158
  var stringifiedUseMetadata = json5_1.default.stringify(component.meta.useMetadata);
158
159
  // TODO: smart only pull in imports as needed
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
14
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
15
  if (ar || !(i in from)) {
@@ -10,19 +21,21 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
10
21
  };
11
22
  Object.defineProperty(exports, "__esModule", { value: true });
12
23
  exports.componentToQwik = void 0;
13
- var collect_css_1 = require("../../helpers/styles/collect-css");
14
- var convertMethodToFunction_1 = require("./convertMethodToFunction");
15
- var jsx_1 = require("./jsx");
16
- var src_generator_1 = require("./src-generator");
17
24
  var babel_transform_1 = require("../../helpers/babel-transform");
18
25
  var fast_clone_1 = require("../../helpers/fast-clone");
26
+ var collect_css_1 = require("../../helpers/styles/collect-css");
27
+ var state_1 = require("../../helpers/state");
19
28
  var add_prevent_default_1 = require("./add-prevent-default");
29
+ var convert_method_to_function_1 = require("./convert-method-to-function");
30
+ var jsx_1 = require("./jsx");
31
+ var src_generator_1 = require("./src-generator");
20
32
  Error.stackTraceLimit = 9999;
21
33
  // TODO(misko): styles are not processed.
22
34
  var DEBUG = false;
23
35
  var componentToQwik = function (userOptions) {
24
36
  if (userOptions === void 0) { userOptions = {}; }
25
37
  return function (_a) {
38
+ var _b, _c, _d;
26
39
  var _component = _a.component, path = _a.path;
27
40
  // Make a copy we can safely mutate, similar to babel's toolchain
28
41
  var component = (0, fast_clone_1.fastClone)(_component);
@@ -37,24 +50,36 @@ var componentToQwik = function (userOptions) {
37
50
  try {
38
51
  emitImports(file, component);
39
52
  emitTypes(file, component);
40
- var state_1 = emitStateMethodsAndRewriteBindings(file, component);
41
- var hasState_1 = Boolean(Object.keys(component.state).length);
53
+ var metadata_1 = component.meta.useMetadata || {};
54
+ var isLightComponent = ((_c = (_b = metadata_1 === null || metadata_1 === void 0 ? void 0 : metadata_1.qwik) === null || _b === void 0 ? void 0 : _b.component) === null || _c === void 0 ? void 0 : _c.isLight) || false;
55
+ var imports_1 = (_d = metadata_1 === null || metadata_1 === void 0 ? void 0 : metadata_1.qwik) === null || _d === void 0 ? void 0 : _d.imports;
56
+ imports_1 && Object.keys(imports_1).forEach(function (key) { return file.import(imports_1[key], key); });
57
+ var state_2 = emitStateMethodsAndRewriteBindings(file, component, metadata_1);
58
+ var hasState_1 = (0, state_1.checkHasState)(component);
42
59
  var css_1 = null;
43
- file.src.const(component.name, (0, src_generator_1.invoke)(file.import(file.qwikModule, 'component$'), [
44
- (0, src_generator_1.arrowFnBlock)(['props'], [
45
- function () {
46
- css_1 = emitUseStyles(file, component);
47
- emitUseContext(file, component);
48
- emitUseRef(file, component);
49
- hasState_1 && emitUseStore(file, state_1);
50
- emitUseContextProvider(file, component);
51
- emitUseMount(file, component);
52
- emitUseWatch(file, component);
53
- emitUseCleanup(file, component);
54
- emitJSX(file, component);
55
- },
56
- ], [component.propsTypeRef || 'any']),
57
- ]), true, true);
60
+ var topLevelElement_1 = isLightComponent ? null : getTopLevelElement(component);
61
+ var componentBody = (0, src_generator_1.arrowFnBlock)(['props'], [
62
+ function () {
63
+ var _a, _b;
64
+ if ((_b = (_a = metadata_1 === null || metadata_1 === void 0 ? void 0 : metadata_1.qwik) === null || _a === void 0 ? void 0 : _a.component) === null || _b === void 0 ? void 0 : _b.useHostElement)
65
+ emitUseHostElement(file);
66
+ css_1 = emitUseStyles(file, component);
67
+ emitUseContext(file, component);
68
+ emitUseRef(file, component);
69
+ hasState_1 && emitUseStore(file, state_2);
70
+ emitUseContextProvider(file, component);
71
+ emitUseMount(file, component);
72
+ emitUseWatch(file, component);
73
+ emitUseCleanup(file, component);
74
+ emitTagNameHack(file, component);
75
+ emitJSX(file, component, topLevelElement_1);
76
+ },
77
+ ], [component.propsTypeRef || 'any']);
78
+ file.src.const(component.name, isLightComponent
79
+ ? componentBody
80
+ : (0, src_generator_1.invoke)(file.import(file.qwikModule, 'component$'), __spreadArray([
81
+ componentBody
82
+ ], (topLevelElement_1 ? ["{tagName:\"".concat(topLevelElement_1, "\"}")] : []), true)), true, true);
58
83
  file.exportDefault(component.name);
59
84
  emitStyles(file, css_1);
60
85
  DEBUG && file.exportConst('COMPONENT', JSON.stringify(component, null, 2));
@@ -67,6 +92,13 @@ var componentToQwik = function (userOptions) {
67
92
  };
68
93
  };
69
94
  exports.componentToQwik = componentToQwik;
95
+ function emitTagNameHack(file, component) {
96
+ var _a;
97
+ var elementTag = (_a = component.meta.useMetadata) === null || _a === void 0 ? void 0 : _a.elementTag;
98
+ if (elementTag) {
99
+ file.src.emit(elementTag, '=', (0, convert_method_to_function_1.convertMethodToFunction)(elementTag, stateToMethodOrGetter(component.state), getLexicalScopeVars(component)), ';');
100
+ }
101
+ }
70
102
  function emitUseMount(file, component) {
71
103
  if (component.hooks.onMount) {
72
104
  // This is called useMount, but in practice it is used as
@@ -78,9 +110,10 @@ function emitUseMount(file, component) {
78
110
  function emitUseWatch(file, component) {
79
111
  if (component.hooks.onUpdate) {
80
112
  component.hooks.onUpdate.forEach(function (onUpdate) {
81
- file.src.emit(file.import(file.qwikModule, 'useWatch$').localName, '((track)=>{');
113
+ file.src.emit(file.import(file.qwikModule, 'useWatch$').localName, '(({track})=>{');
82
114
  emitTrackExpressions(file.src, onUpdate.deps);
83
- file.src.emit(convertTypeScriptToJS(onUpdate.code), '});');
115
+ file.src.emit(convertTypeScriptToJS(onUpdate.code));
116
+ file.src.emit('});');
84
117
  });
85
118
  }
86
119
  }
@@ -89,9 +122,11 @@ function emitTrackExpressions(src, deps) {
89
122
  var dependencies = deps.substring(1, deps.length - 1).split(',');
90
123
  dependencies.forEach(function (dep) {
91
124
  var lastDotIdx = dep.lastIndexOf('.');
92
- var objExp = dep.substring(0, lastDotIdx).replace(/\?$/, '');
93
- var objProp = dep.substring(lastDotIdx + 1);
94
- src.emit(objExp, '&&track(', objExp, ',"', objProp, '");');
125
+ if (lastDotIdx > 0) {
126
+ var objExp = dep.substring(0, lastDotIdx).replace(/\?$/, '');
127
+ var objProp = dep.substring(lastDotIdx + 1);
128
+ objExp && src.emit(objExp, '&&track(', objExp, ',"', objProp, '");');
129
+ }
95
130
  });
96
131
  }
97
132
  }
@@ -101,11 +136,17 @@ function emitUseCleanup(file, component) {
101
136
  file.src.emit(file.import(file.qwikModule, 'useCleanup$').localName, '(()=>{', code, '});');
102
137
  }
103
138
  }
104
- function emitJSX(file, component) {
139
+ function emitJSX(file, component, topLevelElement) {
105
140
  var directives = new Map();
106
141
  var handlers = new Map();
107
142
  var styles = new Map();
108
143
  var parentSymbolBindings = {};
144
+ var children = component.children;
145
+ if (topLevelElement && children.length == 1) {
146
+ var child = children[0];
147
+ children[0] = __assign(__assign({}, child), { name: 'Host' });
148
+ file.import(file.qwikModule, 'Host');
149
+ }
109
150
  file.src.emit('return ', (0, jsx_1.renderJSXNodes)(file, directives, handlers, component.children, styles, parentSymbolBindings));
110
151
  }
111
152
  function emitUseContextProvider(file, component) {
@@ -117,9 +158,7 @@ function emitUseContextProvider(file, component) {
117
158
  var propValue = context.value[prop];
118
159
  file.src.emit(prop, ':');
119
160
  if (isGetter(propValue)) {
120
- var methodMap = stateToMethodOrGetter(component.state);
121
- var code = (0, convertMethodToFunction_1.convertMethodToFunction)(extractGetterBody(propValue), methodMap, getLexicalScopeVars(component));
122
- file.src.emit('(()=>{', code, '})(),');
161
+ file.src.emit('(()=>{', extractGetterBody(propValue), '})(),');
123
162
  }
124
163
  else if (typeof propValue == 'function') {
125
164
  throw new Error('Qwik: Functions are not supported in context');
@@ -143,9 +182,9 @@ function emitUseRef(file, component) {
143
182
  });
144
183
  }
145
184
  function emitUseStyles(file, component) {
146
- var css = (0, collect_css_1.collectCss)(component);
185
+ var css = (0, collect_css_1.collectCss)(component, { prefix: component.name });
147
186
  if (css) {
148
- file.src.emit(file.import(file.qwikModule, 'useScopedStyles$').localName, '(STYLES);');
187
+ file.src.emit(file.import(file.qwikModule, 'useStylesScoped$').localName, '(STYLES);');
149
188
  }
150
189
  return css;
151
190
  }
@@ -154,19 +193,22 @@ function emitStyles(file, css) {
154
193
  file.exportConst('STYLES', '`' + css.replace(/`/g, '\\`') + '`');
155
194
  }
156
195
  }
196
+ /**
197
+ * @param file
198
+ * @param stateInit
199
+ */
157
200
  function emitUseStore(file, stateInit) {
158
201
  var state = stateInit[0];
159
- file.src.emit('const state=', file.import(file.qwikModule, 'useStore').localName, '(');
160
- if (stateInit.length == 1) {
202
+ var hasState = state && Object.keys(state).length > 0;
203
+ if (hasState) {
204
+ file.src.emit('const state=', file.import(file.qwikModule, 'useStore').localName, '(');
161
205
  file.src.emit(JSON.stringify(state));
206
+ file.src.emit(');');
162
207
  }
163
208
  else {
164
- file.src.emit('()=>{const state=', JSON.stringify(state), ';');
165
- file.src.emitList(stateInit.slice(1), ';');
166
- file.src.emit(';return state;');
167
- file.src.emit('}');
209
+ // TODO hack for now so that `state` variable is defined, even though it is never read.
210
+ file.src.emit('const state={};');
168
211
  }
169
- file.src.emit(');');
170
212
  }
171
213
  function emitTypes(file, component) {
172
214
  var _a, _b;
@@ -175,14 +217,35 @@ function emitTypes(file, component) {
175
217
  (_b = component.interfaces) === null || _b === void 0 ? void 0 : _b.forEach(function (i) { return file.src.emit(i); });
176
218
  }
177
219
  }
178
- function emitStateMethodsAndRewriteBindings(file, component) {
220
+ function emitStateMethodsAndRewriteBindings(file, component, metadata) {
179
221
  var _a;
180
222
  var lexicalArgs = getLexicalScopeVars(component);
181
223
  var state = emitStateMethods(file, component.state, lexicalArgs);
182
224
  var methodMap = stateToMethodOrGetter(component.state);
183
- (_a = component.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) { return rewriteBindings(node, methodMap, lexicalArgs); });
225
+ rewriteCodeExpr(component, methodMap, lexicalArgs, (_a = metadata.qwik) === null || _a === void 0 ? void 0 : _a.replace);
184
226
  return state;
185
227
  }
228
+ function rewriteCodeExpr(obj, methodMap, lexicalArgs, replace) {
229
+ if (obj && typeof obj == 'object') {
230
+ if (Array.isArray(obj)) {
231
+ obj.forEach(function (item) { return rewriteCodeExpr(item, methodMap, lexicalArgs, replace); });
232
+ }
233
+ else {
234
+ Object.keys(obj).forEach(function (key) {
235
+ var value = obj[key];
236
+ if (typeof value == 'string') {
237
+ if (value.startsWith(CODE_PREFIX) || key == 'code') {
238
+ var code_1 = (0, convert_method_to_function_1.convertMethodToFunction)(value, methodMap, lexicalArgs);
239
+ replace &&
240
+ Object.keys(replace).forEach(function (key) { return (code_1 = code_1.replace(key, replace[key])); });
241
+ obj[key] = code_1;
242
+ }
243
+ }
244
+ rewriteCodeExpr(value, methodMap, lexicalArgs, replace);
245
+ });
246
+ }
247
+ }
248
+ }
186
249
  function getLexicalScopeVars(component) {
187
250
  return __spreadArray(__spreadArray(['props', 'state'], Object.keys(component.refs), true), Object.keys(component.context.get), true);
188
251
  }
@@ -202,11 +265,12 @@ var FUNCTION = CODE_PREFIX + 'function:';
202
265
  var METHOD = CODE_PREFIX + 'method:';
203
266
  var GETTER = CODE_PREFIX + 'method:get ';
204
267
  function emitStateMethods(file, componentState, lexicalArgs) {
205
- var state = {};
206
- var stateInit = [state];
268
+ var stateValues = {};
269
+ var stateInit = [stateValues];
207
270
  var methodMap = stateToMethodOrGetter(componentState);
208
271
  Object.keys(componentState).forEach(function (key) {
209
- var code = componentState[key];
272
+ var _a;
273
+ var code = (_a = componentState[key]) === null || _a === void 0 ? void 0 : _a.code;
210
274
  if (isCode(code)) {
211
275
  var codeIisGetter = isGetter(code);
212
276
  var prefixIdx = code.indexOf(':') + 1;
@@ -217,7 +281,7 @@ function emitStateMethods(file, componentState, lexicalArgs) {
217
281
  prefixIdx += 'function '.length;
218
282
  }
219
283
  code = code.substring(prefixIdx);
220
- code = (0, convertMethodToFunction_1.convertMethodToFunction)(code, methodMap, lexicalArgs).replace('(', "(".concat(lexicalArgs.join(','), ","));
284
+ code = (0, convert_method_to_function_1.convertMethodToFunction)(code, methodMap, lexicalArgs).replace('(', "(".concat(lexicalArgs.join(','), ","));
221
285
  var functionName = code.split(/\(/)[0];
222
286
  if (codeIisGetter) {
223
287
  stateInit.push("state.".concat(key, "=").concat(functionName, "(").concat(lexicalArgs.join(','), ")"));
@@ -229,14 +293,12 @@ function emitStateMethods(file, componentState, lexicalArgs) {
229
293
  file.exportConst(functionName, 'function ' + code, true);
230
294
  }
231
295
  else {
232
- state[key] = code;
296
+ stateValues[key] = code;
233
297
  }
234
298
  });
235
299
  return stateInit;
236
300
  }
237
301
  function convertTypeScriptToJS(code) {
238
- // HACK, proper implementation should use Babel
239
- // return code.replace(/(\w+):\s+[\w\[\]"']+/gm, (_, ident) => ident);
240
302
  return (0, babel_transform_1.babelTransformExpression)(code, {});
241
303
  }
242
304
  function isGetter(code) {
@@ -256,26 +318,36 @@ function extractGetterBody(code) {
256
318
  function stateToMethodOrGetter(state) {
257
319
  var methodMap = {};
258
320
  Object.keys(state).forEach(function (key) {
259
- var code = state[key];
321
+ var _a;
322
+ var code = (_a = state[key]) === null || _a === void 0 ? void 0 : _a.code;
260
323
  if (typeof code == 'string' && code.startsWith(METHOD)) {
261
324
  methodMap[key] = code.startsWith(GETTER) ? 'getter' : 'method';
262
325
  }
263
326
  });
264
327
  return methodMap;
265
328
  }
266
- function rewriteBindings(node, methodMap, lexicalArgs) {
329
+ /**
330
+ * Return a top-level element for the component.
331
+ *
332
+ * WHAT: If the component has a single root element, then this returns the element name.
333
+ *
334
+ * WHY: This is useful to pull the root element into the component's host and thus saving unnecessary wrapping.
335
+ *
336
+ * @param component
337
+ */
338
+ function getTopLevelElement(component) {
267
339
  var _a;
268
- Object.keys(node.bindings).forEach(function (key) {
269
- var binding = node.bindings[key];
270
- if (binding === null || binding === void 0 ? void 0 : binding.code) {
271
- binding.code = (0, convertMethodToFunction_1.convertMethodToFunction)(binding.code, methodMap, lexicalArgs);
272
- }
273
- if (key.startsWith('on') && (binding === null || binding === void 0 ? void 0 : binding.code)) {
274
- var args = (binding === null || binding === void 0 ? void 0 : binding.arguments) || [];
275
- binding.code = "(".concat(args.join(','), ") => ").concat(binding.code);
276
- delete node.bindings[key];
277
- node.bindings[key + '$'] = binding;
340
+ if (((_a = component.children) === null || _a === void 0 ? void 0 : _a.length) === 1) {
341
+ var child = component.children[0];
342
+ if (child['@type'] === '@builder.io/mitosis/node' && startsLowerCase(child.name)) {
343
+ return child.name;
278
344
  }
279
- });
280
- (_a = node.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) { return rewriteBindings(child, methodMap, lexicalArgs); });
345
+ }
346
+ return null;
347
+ }
348
+ function startsLowerCase(name) {
349
+ return name.length > 0 && name[0].toLowerCase() === name[0];
350
+ }
351
+ function emitUseHostElement(file) {
352
+ file.src.emit('const hostElement=', file.import(file.qwikModule, 'useHostElement').localName, '();');
281
353
  }
@@ -0,0 +1 @@
1
+ export declare function convertMethodToFunction(code: string, properties: Record<string, 'method' | 'getter'>, lexicalArgs: string[]): string;
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertMethodToFunction = void 0;
4
+ function convertMethodToFunction(code, properties, lexicalArgs) {
5
+ var out = [];
6
+ var idx = 0;
7
+ var lastIdx = idx;
8
+ var end = code.length;
9
+ var mode = "code" /* Mode.code */;
10
+ var braceDepth = 0;
11
+ var stringEndBraceDepth = -1;
12
+ var stringEndBraceDepthQueue = [];
13
+ var lastCh = null;
14
+ while (idx < end) {
15
+ var ch = code.charCodeAt(idx++);
16
+ // console.log(mode, code[idx - 1]);
17
+ switch (mode) {
18
+ case "code" /* Mode.code */:
19
+ if (ch === QUOTE_DOUBLE) {
20
+ mode = "stringDouble" /* Mode.stringDouble */;
21
+ }
22
+ else if (ch === QUOTE_SINGLE) {
23
+ mode = "stringSingle" /* Mode.stringSingle */;
24
+ }
25
+ else if (ch === QUOTE_BACK_TICK) {
26
+ mode = "stringTemplate" /* Mode.stringTemplate */;
27
+ }
28
+ else if (ch === OPEN_BRACE) {
29
+ braceDepth++;
30
+ }
31
+ else if (lastCh == FORWARD_SLASH && ch == FORWARD_SLASH) {
32
+ mode = "commentSingleline" /* Mode.commentSingleline */;
33
+ }
34
+ else if (lastCh == FORWARD_SLASH && ch == STAR) {
35
+ mode = "commentMultiline" /* Mode.commentMultiline */;
36
+ }
37
+ else if (ch === CLOSE_BRACE) {
38
+ braceDepth--;
39
+ if (braceDepth === stringEndBraceDepth) {
40
+ stringEndBraceDepth = stringEndBraceDepthQueue.pop();
41
+ mode = "stringTemplate" /* Mode.stringTemplate */;
42
+ }
43
+ }
44
+ else if (isWord(ch, code, idx, 'this') || isWord(ch, code, idx, 'state')) {
45
+ idx--;
46
+ flush();
47
+ consumeIdent();
48
+ if (code.charCodeAt(idx) == DOT) {
49
+ idx++;
50
+ var propEndIdx = findIdentEnd();
51
+ var identifier = code.substring(idx, propEndIdx);
52
+ var propType = properties[identifier];
53
+ if (propType) {
54
+ var isGetter = code.charCodeAt(propEndIdx) !== OPEN_PAREN;
55
+ lastIdx = idx = propEndIdx + (isGetter ? 0 : 1);
56
+ if (isGetter) {
57
+ if (propType === 'method') {
58
+ out.push(identifier, ".bind(null,".concat(lexicalArgs.join(','), ")"));
59
+ }
60
+ else {
61
+ out.push(identifier, "(".concat(lexicalArgs.join(','), ")"));
62
+ }
63
+ }
64
+ else {
65
+ out.push(identifier, "(".concat(lexicalArgs.join(','), ","));
66
+ }
67
+ }
68
+ else {
69
+ flush();
70
+ }
71
+ }
72
+ }
73
+ break;
74
+ case "commentSingleline" /* Mode.commentSingleline */:
75
+ if (ch == EOL)
76
+ mode = "code" /* Mode.code */;
77
+ break;
78
+ case "commentMultiline" /* Mode.commentMultiline */:
79
+ if (lastCh == STAR && ch == FORWARD_SLASH)
80
+ mode = "code" /* Mode.code */;
81
+ break;
82
+ case "stringSingle" /* Mode.stringSingle */:
83
+ if (lastCh !== BACKSLASH && ch == QUOTE_SINGLE)
84
+ mode = "code" /* Mode.code */;
85
+ break;
86
+ case "stringDouble" /* Mode.stringDouble */:
87
+ if (lastCh !== BACKSLASH && ch == QUOTE_DOUBLE)
88
+ mode = "code" /* Mode.code */;
89
+ break;
90
+ case "stringTemplate" /* Mode.stringTemplate */:
91
+ if (lastCh !== BACKSLASH && ch == QUOTE_BACK_TICK) {
92
+ mode = "code" /* Mode.code */;
93
+ }
94
+ else if (lastCh === DOLLAR && ch == OPEN_BRACE) {
95
+ mode = "code" /* Mode.code */;
96
+ stringEndBraceDepthQueue.push(stringEndBraceDepth);
97
+ stringEndBraceDepth = braceDepth;
98
+ braceDepth++;
99
+ }
100
+ break;
101
+ }
102
+ lastCh = ch;
103
+ }
104
+ flush();
105
+ return out.join('');
106
+ function flush() {
107
+ out.push(code.substring(lastIdx, idx));
108
+ lastIdx = idx;
109
+ }
110
+ function findIdentEnd() {
111
+ var scanIdx = idx;
112
+ while (isIdentCh(code.charCodeAt(scanIdx)) && scanIdx < end) {
113
+ scanIdx++;
114
+ }
115
+ return scanIdx;
116
+ }
117
+ function consumeIdent() {
118
+ while (isIdentCh(code.charCodeAt(idx))) {
119
+ idx++;
120
+ }
121
+ }
122
+ }
123
+ exports.convertMethodToFunction = convertMethodToFunction;
124
+ function isIdentCh(ch) {
125
+ return ((CHAR_0 <= ch && ch <= CHAR_9) ||
126
+ (CHAR_a <= ch && ch <= CHAR_z) ||
127
+ (CHAR_A <= ch && ch <= CHAR_Z) ||
128
+ ch === UNDERSCORE ||
129
+ ch === DOLLAR);
130
+ }
131
+ function isWord(ch, code, idx, word) {
132
+ if (ch !== word.charCodeAt(0))
133
+ return false;
134
+ for (var i = 1; i < word.length; i++) {
135
+ if (code.charCodeAt(idx + i - 1) !== word.charCodeAt(i)) {
136
+ return false;
137
+ }
138
+ }
139
+ if (isIdentCh(code.charCodeAt(idx + word.length - 1))) {
140
+ return false;
141
+ }
142
+ return true;
143
+ }
144
+ var QUOTE_DOUBLE = '"'.charCodeAt(0);
145
+ var QUOTE_SINGLE = "'".charCodeAt(0);
146
+ var QUOTE_BACK_TICK = '`'.charCodeAt(0);
147
+ var BACKSLASH = "\\".charCodeAt(0);
148
+ var FORWARD_SLASH = "/".charCodeAt(0);
149
+ var EOL = "\n".charCodeAt(0);
150
+ var STAR = "*".charCodeAt(0);
151
+ var CHAR_0 = "0".charCodeAt(0);
152
+ var CHAR_9 = "9".charCodeAt(0);
153
+ var CHAR_a = "a".charCodeAt(0);
154
+ var CHAR_z = "z".charCodeAt(0);
155
+ var CHAR_A = "A".charCodeAt(0);
156
+ var CHAR_Z = "Z".charCodeAt(0);
157
+ var UNDERSCORE = "_".charCodeAt(0);
158
+ var DOLLAR = "$".charCodeAt(0);
159
+ var DOT = ".".charCodeAt(0);
160
+ var OPEN_PAREN = '('.charCodeAt(0);
161
+ var OPEN_BRACE = '{'.charCodeAt(0);
162
+ var CLOSE_BRACE = '}'.charCodeAt(0);