/* 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 { sveltePreprocess } from "svelte-preprocess"; import * as recast from "recast"; import { rollup } from "rollup"; import svelte from "rollup-plugin-svelte"; import alias from "@rollup/plugin-alias"; import commonjs from "@rollup/plugin-commonjs"; import { nodeResolve } from "@rollup/plugin-node-resolve"; import virtual from "@rollup/plugin-virtual"; import swc from "@rollup/plugin-swc"; import path from "node:path"; class Builder { path; props; locals; compiled; ssr; workingDir; preprocess; pathAliases; root; constructor(path2, root, props, locals, client, server, ssr, workingDir, preprocess, pathAliases) { this.path = path2; this.root = root; this.props = readable(Object.assign(props, locals, props)); this.locals = locals; this.compiled = { client, server }; this.ssr = ssr; this.workingDir = workingDir; this.preprocess = preprocess; this.pathAliases = pathAliases; } async bundle(generate) { const bundle = (await (await rollup({ input: "entry", output: { format: "esm", sourcemap: true }, watch: { skipWrite: true }, plugins: [ // @ts-expect-error see https://github.com/rollup/plugins/issues/1662 virtual({ entry: `import App from '${this.path}'; export default App` }), // @ts-expect-error see https://github.com/rollup/plugins/issues/1662 svelte({ compilerOptions: { generate, css: "injected", hydratable: true }, emitCss: false, preprocess: sveltePreprocess(this.preprocess), onwarn: (warning, handler) => { if (warning.code === "missing-declaration" && warning.message.includes("'props'")) return; throw new Error(warning.message); } }), // @ts-expect-error see https://github.com/rollup/plugins/issues/1662 alias({ entries: Object.entries(this.pathAliases || {}).map((k, v) => { return new Object({ find: k, replacement: v }); }) }), // @ts-expect-error see https://github.com/rollup/plugins/issues/1662 commonjs(), nodeResolve({ browser: true, exportConditions: ["svelte"], extensions: [".svelte"] }), // @ts-expect-error see https://github.com/rollup/plugins/issues/1662 swc({ swc: { jsc: { target: "es6", minify: { format: { comments: "all" } } }, minify: true, sourceMaps: true, inlineSourcesContent: true } }) ] })).generate({ format: "esm", sourcemap: true })).output; bundle[0].map.sources = bundle[0].map.sources.map((el) => { return path.relative(this.path, this.root) + "/" + path.relative(this.root, el); }); return `//# sourceMappingURL=${bundle[0].map.toUrl()} //# sourceURL=${path.relative(this.path, this.root) + "/" + path.relative(this.root, this.path)} ${bundle[0].code}`.trim(); } async client() { return this.compiled?.client || this.standardizeClient(await this.bundle("dom")); } standardizeClient(code) { const ast = recast.parse(code); let name; recast.visit(ast, { visitExportNamedDeclaration: (path2) => { const stagingName = path2.node?.specifiers?.[0].local?.name; name = typeof stagingName !== "string" ? "" : stagingName; path2.prune(); return false; } }); recast.visit(ast, { visitIdentifier: (path2) => { if (path2.node.name === name) { path2.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 };