@builder.io/mitosis 0.1.5 → 0.1.7

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.
@@ -185,7 +185,7 @@ var componentToSolid = function (passedOptions) {
185
185
  hasShowComponent ? 'Show' : undefined,
186
186
  hasForComponent ? 'For' : undefined,
187
187
  json.hooks.onMount.length ? 'onMount' : undefined
188
- ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect'] : []), true), ((_d = state === null || state === void 0 ? void 0 : state.import.solidjs) !== null && _d !== void 0 ? _d : []), true).filter(nullable_1.checkIsDefined));
188
+ ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect', 'createMemo'] : []), true), ((_d = state === null || state === void 0 ? void 0 : state.import.solidjs) !== null && _d !== void 0 ? _d : []), true).filter(nullable_1.checkIsDefined));
189
189
  var storeImports = (_e = state === null || state === void 0 ? void 0 : state.import.store) !== null && _e !== void 0 ? _e : [];
190
190
  var propType = json.propsTypeRef || 'any';
191
191
  var propsArgs = "props".concat(options.typescript ? ":".concat(propType) : '');
@@ -198,14 +198,27 @@ var componentToSolid = function (passedOptions) {
198
198
  }), json.name, propsArgs, (_f = state === null || state === void 0 ? void 0 : state.str) !== null && _f !== void 0 ? _f : '', getRefsString(json, options), getContextString(json, options), json.hooks.onMount.map(function (hook) { return "onMount(() => { ".concat(hook.code, " })"); }).join('\n'), json.hooks.onUpdate
199
199
  ? json.hooks.onUpdate
200
200
  .map(function (hook, index) {
201
- if (hook.deps) {
202
- var hookName = "onUpdateFn_".concat(index);
203
- return "\n function ".concat(hookName, "() { ").concat(hook.code, " };\n createEffect(on(() => ").concat(hook.deps, ", ").concat(hookName, "));\n ");
204
- }
205
- else {
206
- // TO-DO: support `onUpdate` without `deps`
201
+ // TO-DO: support `onUpdate` without `deps`
202
+ if (!hook.deps)
207
203
  return '';
208
- }
204
+ var hookName = "onUpdateFn_".concat(index);
205
+ var depsArray = hook.deps
206
+ .slice(1, hook.deps.length - 1)
207
+ .split(',')
208
+ .map(function (x) { return x.trim(); });
209
+ var getReactiveDepName = function (dep) {
210
+ var newLocal = dep.replace(/(\.|\?|\(|\)|\[|\])/g, '_');
211
+ return "".concat(hookName, "_").concat(newLocal);
212
+ };
213
+ var needsMemo = function (dep) { return true; };
214
+ var reactiveDepsWorkaround = depsArray
215
+ .filter(needsMemo)
216
+ .map(function (dep) { return "const ".concat(getReactiveDepName(dep), " = createMemo(() => ").concat(dep, ");"); })
217
+ .join('\n');
218
+ var depsArrayStr = depsArray
219
+ .map(function (x) { return (needsMemo(x) ? "".concat(getReactiveDepName(x), "()") : x); })
220
+ .join(', ');
221
+ return "\n ".concat(reactiveDepsWorkaround, "\n function ").concat(hookName, "() { ").concat(hook.code, " };\n createEffect(on(() => [").concat(depsArrayStr, "], ").concat(hookName, "));\n ");
209
222
  })
210
223
  .join('\n')
211
224
  : '', addWrapper ? '<>' : '', json.children
@@ -20,7 +20,7 @@ var processSignalStateValue = function (_a) {
20
20
  if (typeof value === 'string') {
21
21
  switch (type) {
22
22
  case 'getter':
23
- return (0, function_1.pipe)(value, patterns_1.replaceGetterWithFunction, mapValue);
23
+ return (0, function_1.pipe)(value, mapValue, patterns_1.extractGetterCodeBlock, function (x) { return "const ".concat(key, " = createMemo(() => {").concat(x, "})"); });
24
24
  case 'function':
25
25
  return mapValue(value);
26
26
  case 'method':
@@ -58,7 +58,7 @@ var getState = function (_a) {
58
58
  var stateStr = "\n ".concat(mutableStateStr, "\n ").concat(signalStateStr, "\n ").concat(storeStateStr, "\n ");
59
59
  var importObj = {
60
60
  store: __spreadArray(__spreadArray([], (hasMutableState ? ['createMutable'] : []), true), (hasStoreState ? ['createStore', 'reconcile'] : []), true),
61
- solidjs: __spreadArray(__spreadArray([], (hasSignalState ? ['createSignal'] : []), true), (hasStoreState ? ['createEffect', 'on'] : []), true),
61
+ solidjs: __spreadArray(__spreadArray([], (hasSignalState ? ['createSignal', 'createMemo'] : []), true), (hasStoreState ? ['createEffect', 'on'] : []), true),
62
62
  };
63
63
  return {
64
64
  str: stateStr,
@@ -335,7 +335,7 @@ var componentToSvelte = function (userProvidedOptions) {
335
335
  * rerun the effect every time the parent store is changed in any way.
336
336
  */
337
337
  return "\n function ".concat(fnName, "(..._args").concat(options.typescript ? ': any[]' : '', ") {\n ").concat(code, "\n }\n ").concat(reactiveDepsWorkaround, "\n $: ").concat(fnName, "(...[").concat(depsArrayStr, "]);\n ");
338
- }).join(';')) || '',
338
+ }).join('\n')) || '',
339
339
  // make sure this is after all other state/code is initialized
340
340
  setContextCode({ json: json, options: options }), !((_k = json.hooks.onUnMount) === null || _k === void 0 ? void 0 : _k.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
341
341
  .map(function (item) {
@@ -4,4 +4,5 @@ export declare const checkIsGetter: (code: string) => RegExpMatchArray | null;
4
4
  export declare const stripGetter: (str: string) => string;
5
5
  export declare const replaceGetterWithFunction: (str: string) => string;
6
6
  export declare const replaceFunctionWithGetter: (str: string) => string;
7
+ export declare const extractGetterCodeBlock: (getter: string) => string;
7
8
  export declare const prefixWithFunction: (str: string) => string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prefixWithFunction = exports.replaceFunctionWithGetter = exports.replaceGetterWithFunction = exports.stripGetter = exports.checkIsGetter = exports.SETTER = exports.GETTER = void 0;
3
+ exports.prefixWithFunction = exports.extractGetterCodeBlock = exports.replaceFunctionWithGetter = exports.replaceGetterWithFunction = exports.stripGetter = exports.checkIsGetter = exports.SETTER = exports.GETTER = void 0;
4
4
  exports.GETTER = /^\s*get /;
5
5
  exports.SETTER = /^\s*set /;
6
6
  var checkIsGetter = function (code) { return code.match(exports.GETTER); };
@@ -11,5 +11,9 @@ var replaceGetterWithFunction = function (str) { return str.replace(/^(get )?/,
11
11
  exports.replaceGetterWithFunction = replaceGetterWithFunction;
12
12
  var replaceFunctionWithGetter = function (str) { return str.replace(/^(function )?/, 'get '); };
13
13
  exports.replaceFunctionWithGetter = replaceFunctionWithGetter;
14
+ var extractGetterCodeBlock = function (getter) {
15
+ return getter.replace(/[\S\s]*\(\) \{([\S\s]*)\}[\S\s]*/, '$1').trim();
16
+ };
17
+ exports.extractGetterCodeBlock = extractGetterCodeBlock;
14
18
  var prefixWithFunction = function (str) { return "function ".concat(str); };
15
19
  exports.prefixWithFunction = prefixWithFunction;
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.1.5",
25
+ "version": "0.1.7",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {