-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Support for plugins #7100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Please elaborate more on what you want to achieve with these global hooks/plugins. You can use the existing lifecycle functions outside of Svelte components just fine, you just need to make sure they are called synchronously during component initialization.
|
@dummdidumm A simple example plugin could look like // Plugin
import {beforeCreate, afterCreate, afterDestroy} from "svelte"
var toCleanup = new Map // instance => handlers[]
var currentInstance
beforeCreate(instance => currentInstance = instance) // the component initialization runs between beforeCreate and afterCreate
afterCreate(instance => currentInstance = null)
afterDestroy(instance => {
var handlers = toCleanup.get(instance)
if (handlers) {
unwatch(...handlers)
toCleanup.delete(instance)
}
})
export function watch(handler) {
watchEffect(handler);
toCleanup.get(currentInstance)?.push(handler) ?? toCleanup.set(currentInstance, [handler])
} Your suggestion can also work in this case but you can't combine the handlers so it can create a lot of closures. Of course these hooks can be used for other meta things that do not involve or are limited to the component calling a function during initialization. A plugin could support asynchronous registration, like how |
Man, your issue is so incredibly vague. I tried to make sense of it and I really couldn't. I'm going to give you the benefit of the doubt and assume you're not a troll because your example code felt somewhat legitimate. I come from working on a gigantic Angular frontend that is basically like a native app level SPA with tons of state and pages and forms and widgets galore. Angular locks you in to their stupid CLI project setup and Angular modules are basically just a waste of time on top of regular TypeScript imports. I'm not even sure I can get access to the WebPack config the Angular CLI uses behind the scenes so I can incrementally port components to Svelte. Now, the beauty of Svelte is that every // NOTE: this is pseudo-code, look at the Svelte docs to see what the actual method names are
export default class UserContactCard {
// Svelte-generated code for DOM manipulation..
function hookupToDOM(parent, sibling) {
// ...
}
// Svelte can even add getters/setters for props (inputs and outputs) that will
// contain generated code to schedule DOM updates when you set the values
} The elegance of this approach is that you can import that Svelte component as a regular JS class literally anywhere you want. You can mix and match vanilla JS (with your own custom DOM manipulation code) and Svelte components however you like. This means adding Svelte to a React or Vue project is as easy as adding the Svelte plugin for WebPack or Rollup (whichever you are using), write some Svelte component(s), and then import them into your TSX/JSX workflow. Use teardown hooks in your React component to then tell the Svelte child component to tear itself down. If you have a vanilla JS/TS module for UI like modals or popup notifications ("toasts"), you can add a function to that file that takes a Svelte component for a custom toast and then calls its Regarding the thing you said about interop with As far as "plugins" go, Svelte seems like it hit a sweet spot for simplicitly and elegance. Like I said, you can also mix and match it with vanilla JS when you need to write custom code for performance (like rendering big data tables intelligently). It does not try to add too many fancy abstractions over the native DOM elements and it does not try to hide stuff from you and take over your whole life with an enterprise workflow (lookin' at you, Angular). Probably best not to try and make Svelte yet another "do it ALL" highly opinionated framework, and keep it at a lower-level so you can mix and match it just like you build up trees of functions in any programming language. |
Also, what do you mean by "serious frameworks like Angular and React"? Angular is heavily opinionated, but I would argue it gets a number of things wrong, and that quickly becomes a problem when you're locked into the workflow with a gigantic application and you want to shop for alternatives. Svelte may not have an equivalent for Angular Material, but I literally skipped all the other "1st party" parts of Angular like the HttpClient (I just use a thin layer on top of |
Describe the problem
I was considering testing svelte on a project. But it has no support for plugins (other than compilation preprocessors) which I could use to, for example, patch in runtime dependency handling.
Describe the proposed solution
A plugin would require an API for registering hooks globally like synchronous
beforeCreate
,afterCreate
,afterDestroy
for doing initializations and cleanup. With that one could integrate runtime state management libraries like mobx and have functions likewatch(handler)
that unregister the handlers when the component is destroyed.Without such global hooks one could maybe resort to using an existing instance to add
on_destroy
handlers likewatch(closure, currentInstance)
but it seems the official svelte build refuses to provide an api for that (#5517).In addition a way to integrate observable arrays with template features like
#each
could be helpful.And did I mention JSX For when templates don't cut it?
Btw, I saw it mentioned somewhere that svelte is just a compiler that can be combined with more serious frameworks like react, angular, ... but I'd like to see how that could be accomplished considering all serious frameworks themselves have components and compilation steps.
Alternatives considered
Importance
i cannot use svelte without it
The text was updated successfully, but these errors were encountered: