/* IF YOU ARE READING THIS, YOU ARE VIEWING THE BUILT JS FROM THE SOURCE TYPESCRIPT AT ../ts/ . DO NOT MODIFY. */ import { readable } from "svelte/store"; import { importFromStringSync } from "module-from-string"; import * as esbuild from "esbuild"; import sveltePlugin from "esbuild-svelte"; import { sveltePreprocess } from "svelte-preprocess"; import * as recast from "recast"; class Builder { path; props; locals; compiled; ssr; workingDir; preprocess; constructor(path, props, locals, client, server, ssr, workingDir, preprocess) { this.path = path; this.props = readable(props); this.locals = locals; this.compiled = { client, server }; this.ssr = ssr; this.workingDir = workingDir; this.preprocess = preprocess; } async bundle(generate, sveltePath = "svelte") { const bundle = await esbuild.build({ entryPoints: [this.path], mainFields: ["svelte", "browser", "module", "main"], conditions: ["svelte", "browser"], absWorkingDir: this.workingDir, write: false, outfile: "component.js", bundle: true, format: "esm", metafile: true, plugins: [ // @ts-expect-error sveltePlugin({ compilerOptions: { generate, css: "injected", hydratable: true, sveltePath }, preprocess: sveltePreprocess(this.preprocess), filterWarnings: (warning) => { if (warning.code === "missing-declaration" && warning.message.includes("'props'")) { return false; } return true; } }) ] }); const throwables = [].concat(bundle.errors, bundle.warnings); if (throwables.length > 0) { throw throwables[0]; } return bundle.outputFiles[0].text; } async client() { return this.compiled?.client || this.standardizeClient(await this.bundle("dom", "https://esm.sh/svelte")); } standardizeClient(code) { const ast = recast.parse(code); let name; recast.visit(ast, { visitExportNamedDeclaration: (path) => { const stagingName = path.node?.specifiers?.[0].local?.name; name = typeof stagingName !== "string" ? "" : stagingName; path.prune(); return false; } }); recast.visit(ast, { visitIdentifier: (path) => { if (path.node.name === name) { path.node.name = "App"; } return false; } }); return recast.print(ast).code; } async server() { const output = this.compiled?.server || await this.bundle("ssr"); const Component = importFromStringSync(output, { globals: { props: this.props } }).default; const { html, css, head } = await Component.render(this.locals); return { output, html, head, css: css.code }; } async build() { try { const serv = this.ssr ? await this.server() : null; const cli = await this.client(); const comp = { client: cli, server: serv?.output }; return { client: cli, server: serv, compiled: comp }; } catch (e) { return { error: e }; } } } var builder_default = Builder; export { builder_default as default };