SvelteStack / svelte-query

Performant and powerful remote data synchronization for Svelte
https://sveltequery.vercel.app
MIT License
813 stars 31 forks source link

Vite project not starting in development mode without broadcast-channel installed #63

Open philip-weber opened 2 years ago

philip-weber commented 2 years ago

After updating to release 1.5.0 I needed to install broadcast-channel manually to get my project running in development mode. The production bundle, on the other hand, can be created without the dependency without any problems and works perfectly. I'm not using the broadcastQueryClient in my project. I use vite@2.6.14. Of course I can install broadcast-channel but I thought it would not be needed as of release 1.5.0. Or did I understand the release notes wrong? Do I have to make certain settings in vite for it to work? I have already excluded @sveltestack/svelte-query in the vite.config.js.

[vite] Internal server error: Failed to resolve import "broadcast-channel" from "node_modules/@sveltestack/svelte-query/svelte/queryCore/broadcastQueryClient-experimental/index.js". Does the file exist?
Plugin: vite:import-analysis
File: /Users/XXXXX/node_modules/@sveltestack/svelte-query/svelte/queryCore/broadcastQueryClient-experimental/index.js
1  |  import '../core';
2  |  export async function broadcastQueryClient({ queryClient, broadcastChannel = 'svelte-query', }) {
3  |      const { BroadcastChannel } = await import('broadcast-channel');
   |                                                ^
4  |      let transaction = false;
5  |      const tx = (cb) => {
SomaticIT commented 2 years ago

Hello,

Indeed, we tried to make broadcast-channel an optional dependency since it's only used in broadcastQueryClient to reduce the package size.

It created a side effect with vite which tries to load it even if you do not use broadcastQueryClient. Maybe we could open an issue on the vite side to improve this behavior.

As a workaround, you can exclude broadcast-channel from the dependency pre-bundling process.

Edit 1: I created a minimal reproduction projects, it seems that added svelte-query or broadcast-channel is not enough to workaround this issue.

Edit 2: I filled an issue on the vite project (vitejs/vite#6007) to see if it's possible to better handle optional dynamically imported dependencies.

winston0410 commented 2 years ago

@SomaticIT @SomaticIT Are you able to use svelte-query with SvelteKit right now? I have tried to build the app with svelte-query installed, but still it has failed due to the broadcast-channel package

SomaticIT commented 2 years ago

I use it on a SvelteKit project in SPA mode. I have to install broadcast-channel to allow vite to analyze imports.

swyxio commented 2 years ago

@SomaticIT i actually installed broadcast-channel but am still getting this error. no idea how to resolve.

image

update: solved by deleting .svelte-kit and installing svelte query again