Closed brunnerh closed 1 year ago
this isn't really a bug in vite-plugin-svelte. Processing dependencies with vite isn't exactly a feature but merely a side-effect of optimizeDeps.exclude - which we had to set by default for svelte dependencies for a long time because it didn't work.
But recent versions of vite-plugin-svelte enable it by default, so libraries that depend on vite features will not work unless you exclude them from optimizeDeps in your vite config.
Such libraries are a special case, the consumer of these libraries always needs to consume them with vite, otherwise they will break. (eg webpack won't know what import.meta.glob is or even worse, have a different implementation)
So these libraries are better suited for monorepo setups and not actually published to a public registry. If you insist on publishing them, they need to use a peer dependency on vite and accompany documentation on how to set up the required exclusion.
Leaving this open for discussion a bit, but we currently have no plans to support such libraries - and i personally believe you should not build public libraries that only work when consumed with vite.
i personally believe you should not build public libraries that only work when consumed with vite
So far these libraries are only published internally. Do you happen to know of a better approach of how asset dependencies could/should be handled in Svelte component libraries? I suspect there is no single solution that would work for all build systems.
If the libraries are published internally and are meant to be used with Vite only. You can only add them to optimizeDeps.exclude
and ssr.noExternal
to tell Vite that these libraries should be interpreted as "source code", meaning Vite features will be processed within them.
This is a general issue with libraries that uses Vite-specific features so it's not something vite-plugin-svelte can fix. Ideally Vite's prebundling guide should better cover this, but I haven't got around updating the guide yet. Closing this in favour of https://github.com/vitejs/vite/issues/8735
(PS thanks for the very conclusive reproduction. Not every day we get a well constructed bug report 🙂 )
Describe the bug
There are various ways in which assets can be imported and referenced and there appear to be various issues when using a package that has assets with the Vite dev server. I also found one scenario in which the build output is also broken.
Use cases for this are components that require images as assets or translation data in separate JSON files by language (so only the file for the current language can be dynamically loaded.)
dev
build
import.meta.glob(...)
)url(...)
references<img src>
references (static import)Component code:
Workaround for issues that only appear in the dev server:
Reproduction URL
https://github.com/brunnerh/svelte-package-assets-imports-issue
Reproduction
npm install
test-package
intonode_modules
npm run dev
to view the dev server results.npm run build
andnpm run preview
to view build results.Logs
System Info