Home > OS >  How to import Svelte component into vanilla JS project, without adding Svelte as a dependency to tha
How to import Svelte component into vanilla JS project, without adding Svelte as a dependency to tha

Time:12-14

As the title implies: I am looking for a way to import a Svelte component into a project that "does not know what Svelte it" (i.e. does not use Svelte as a dependency).

Imagine making a npm package that contains a Svelte component and then installing that package in one or several projects. The projects that install that package do not need to add Svelte as a dependency. The package is self-contained that way.

In this specific case I cannot, however, use npm to expose my Svelte component to the projects that need it. I was thinking more along the lines of simply sending the code to those that need it, so they can import it as a module (maybe naive...), but I would not know how to make that meet the dependency requirement described above.

I am looking for a similarly self-contained way of making my Svelte component available to projects.

Any ideas? Ty.

CodePudding user response:

Since Svelte builds to a web component I don't think it's possible. Many npm packages are targeted at the Frameworks they're made for.... You could make your package using Vanilla JS or TypeScript and it'll be installable everywhere. A new JavaScript framework called Astro is looking to merge all the frameworks, you could check it out.

  • Related