sveldoc
Version:
Readme-driven Development for building Svelte components
44 lines (43 loc) • 2.05 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseComponent = void 0;
const compiler_1 = require("svelte/compiler");
// TODO: allow examples to use TypeScript
const parseComponent = ({ source: raw_source, filename, }) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
let source = raw_source;
const { html, css, instance, module } = (0, compiler_1.parse)(source, { filename });
const fragment = (start, end) => source.slice(start, end);
const split = (fragment) => fragment
.split(";")
.map((line) => line.trim())
.filter(Boolean)
.map((line) => line + ";");
const parsed = {
html: fragment(html.start, html.end),
css: (_a = css === null || css === void 0 ? void 0 : css.content.styles) !== null && _a !== void 0 ? _a : "",
script: [],
module: [],
};
if (module) {
parsed.html = parsed.html.replace(fragment(module.start, module.end), "");
parsed.module = split(fragment(module.content.start, module.content.end));
}
if (instance) {
parsed.html = parsed.html.replace(source.slice(instance.start, instance.end), "");
parsed.script = split(fragment(instance.content.start, instance.content.end));
}
return {
parsed,
};
});
exports.parseComponent = parseComponent;
;