UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

45 lines (44 loc) 1.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderWatchHooks = exports.hasWatchHooks = exports.renderUpdateHooks = exports.hasRootUpdateHook = void 0; const lodash_1 = require("lodash"); const extractCode = (hook) => hook.code; function renderRootUpdateHook(hooks, output) { if (hooks.length === 0) { return output; } const str = `onUpdate() { ${hooks.map(extractCode).join('\n')} }`; return output.replace(/,?(\s*})$/, `,\n${str}$1`); } function getRootUpdateHooks(json) { var _a; return ((_a = json.hooks.onUpdate) !== null && _a !== void 0 ? _a : []).filter((hook) => hook.deps == ''); } function hasRootUpdateHook(json) { return getRootUpdateHooks(json).length > 0; } exports.hasRootUpdateHook = hasRootUpdateHook; exports.renderUpdateHooks = (0, lodash_1.curry)((json, output) => { return renderRootUpdateHook(getRootUpdateHooks(json), output); }); function getWatchHooks(json) { var _a; return ((_a = json.hooks.onUpdate) !== null && _a !== void 0 ? _a : []).filter((hook) => { var _a; return (_a = hook.deps) === null || _a === void 0 ? void 0 : _a.match(/state|this/); }); } const hasWatchHooks = (json) => { return getWatchHooks(json).length > 0; }; exports.hasWatchHooks = hasWatchHooks; function renderWatchHook(hook) { var _a, _b; const deps = (_b = ((_a = hook.deps) !== null && _a !== void 0 ? _a : '')) === null || _b === void 0 ? void 0 : _b.slice(1).slice(0, -1).split(', ').filter((dep) => dep.match(/state|this/)); return deps .map((dep) => `this.$watch('${dep.replace(/(state|this)\./, '')}', (value, oldValue) => { ${hook.code} });`) .join('\n'); } const renderWatchHooks = (json) => { return (0, exports.hasWatchHooks)(json) ? getWatchHooks(json).map(renderWatchHook).join('\n') : ''; }; exports.renderWatchHooks = renderWatchHooks;