vite-plugin-symfony
Version:
A Vite plugin to integrate easily Vite in your Symfony application
78 lines (76 loc) • 2.37 kB
JavaScript
// src/stimulus/helpers/svelte/util.ts
var svelteImportedModules = {};
function registerSvelteControllerComponents(modules, controllersDir = "./svelte/controllers") {
svelteImportedModules = { ...svelteImportedModules, ...modules };
window.resolveSvelteComponent = (name) => {
const svelteModule = svelteImportedModules[`${controllersDir}/${name}.svelte`];
if (typeof svelteModule === "undefined") {
const possibleValues = Object.keys(svelteImportedModules).map(
(key) => key.replace(`${controllersDir}/`, "").replace(".svelte", "")
);
throw new Error(`Svelte controller "${name}" does not exist. Possible values: ${possibleValues.join(", ")}`);
}
return svelteModule;
};
}
// src/stimulus/helpers/svelte/render_controller.ts
import { Controller } from "@hotwired/stimulus";
import { mount, unmount } from "svelte";
var render_controller_default = class extends Controller {
app;
props;
intro;
static values = {
component: String,
props: Object,
intro: Boolean
};
connect() {
this.element.innerHTML = "";
this.props = this.propsValue ?? void 0;
this.intro = this.introValue ?? void 0;
this.dispatchEvent("connect");
const importedSvelteModule = window.resolveSvelteComponent(this.componentValue);
const onload = (svelteModule) => {
const Component = svelteModule.default;
this._destroyIfExists();
this.app = mount(Component, {
target: this.element,
props: this.props,
intro: this.intro
});
this.element.root = this.app;
this.dispatchEvent("mount", {
component: Component
});
};
if (typeof importedSvelteModule === "function") {
importedSvelteModule().then(onload);
} else {
onload(importedSvelteModule);
}
}
disconnect() {
this._destroyIfExists();
this.dispatchEvent("unmount");
}
_destroyIfExists() {
if (this.element.root !== void 0) {
unmount(this.element.root);
delete this.element.root;
}
}
dispatchEvent(name, payload = {}) {
const detail = {
componentName: this.componentValue,
props: this.props,
intro: this.intro,
...payload
};
this.dispatch(name, { detail, prefix: "svelte" });
}
};
export {
render_controller_default as SvelteController,
registerSvelteControllerComponents
};