@builder.io/mitosis
Version:
Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io
60 lines (59 loc) • 2.24 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSvelte = void 0;
const lodash_1 = require("lodash");
const svelte_preprocess_1 = __importDefault(require("svelte-preprocess"));
const compiler_1 = require("svelte/compiler");
const css_1 = require("./css");
const post_process_1 = require("./helpers/post-process");
const html_1 = require("./html");
const instance_1 = require("./instance");
const module_1 = require("./module");
const typescript_1 = require("./typescript");
function mapAstToMitosisJson(ast, name, string_ = '', usesTypescript = false) {
const json = {
'@type': '@builder.io/mitosis/component',
inputs: [],
state: {},
props: {},
refs: {},
hooks: {
onMount: [],
onEvent: [],
},
imports: [],
children: [],
context: { get: {}, set: {} },
subComponents: [],
meta: {},
name,
style: undefined,
};
(0, module_1.parseModule)(ast, json);
(0, instance_1.parseInstance)(ast, json);
(0, html_1.parseHtml)(ast, json);
(0, css_1.parseCss)(ast, json);
(0, post_process_1.postProcess)(json);
if (usesTypescript) {
(0, typescript_1.collectTypes)(string_, json);
}
return (0, lodash_1.omit)(json, ['props']);
}
const parseSvelte = async function (string_, path = 'MyComponent.svelte') {
var _a, _b;
const usesTypescript = (0, typescript_1.isTypeScriptComponent)(string_);
const processedString = await (0, compiler_1.preprocess)(string_, [
(0, svelte_preprocess_1.default)({
typescript: usesTypescript ? { tsconfigFile: false } : false,
}),
], {
filename: path.split('/').pop(),
});
const ast = (0, compiler_1.parse)(processedString.code);
const componentName = (_b = (_a = path.split('/').pop()) === null || _a === void 0 ? void 0 : _a.split('.')[0]) !== null && _b !== void 0 ? _b : 'MyComponent';
return mapAstToMitosisJson(ast, componentName, string_, usesTypescript);
};
exports.parseSvelte = parseSvelte;
;