Open Lms24 opened 9 months ago
nope...
OK So I think I figured out the cause:
We bundle two versions of Spotlight code in different places and dispatch to the "wrong" event target
"open"
event.init
function of our Astro toolbar plugin file, we call Spotlight.open(). This call dispatches an "open"
event to spotlight's eventTarget. However, the event target that this is dispatched on, is the one from the toolbar plugin bundle. While it should actually trigger the other target. I think we have two solutions:
I think, given the limitations of 1, I'm gonna go with 2.
Oh btw I have no idea about why this didn't cause problems within the monorepo. My best guess is that Vite and pnpm resolve and bundle dependencies differently than when a package is added as a dependency. This gives me trust issues in our monorepo 😅
So, this has all to do with how and where we import spotlight from.
import * as Spotlight from @spotlightjs/core
because importing from @spotlightjs/astro
(i.e.its own package) throws errorsimport * as Spotlight from @spotlightjs/astro
because importing from @spotlightjs/core
throws an error (at least in pnpm which requres all deps to be directly specified in package.json).Not sure if we can get around this easily...
I don't think double-bundling is something that should happen. I could see this as perhaps being a race-condition where both modules are loaded in parallel resulting in them being double-loaded.
What I'd try first is listing these packages in Vite's optimizedDeps
config here: https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-include
Doing this will cause Vite to pre-bundle them, in which case it should see that there is common dependencies between them and prevent the duplication. If that doesn't work, let me know and I'll think of something else.
How you would do that in an Astro integration is with updateConfig
@matthewp thanks for looking into this!
I gave it a quick shot and optimizedDeps
seems to fix it on first glance. However, in pnpm-based projects, vite will complain about not being able to resolve @spotlightjs/core
. I think that's because the package is a dependency of @spotlightjs/astro
and therefore not explicitly declared in users' package.json
s.
If I only include "@spotlightjs/astro"
in the optimizedDeps
array, the original problem still persists (which makes sense afaict)
Any further ideas?
Fwiw, I pushed a workaround in #121 which also resolves the problem but it doesn't fix the double bundling.
Gonna remove from the 1.0 milestone because it's unlikely that we fix this properly before 1.0. Also edited the title to be more precise.
@Lms24 how can I tell if this is still an issue or not after the general Vite plugin approach?
how can I tell if this is still an issue or not after the general Vite plugin approach?
I guess we'd need to remove putting the spotlight event target (see eventTarget.ts
) onto the global object and only rely on the event target in the module scope. Then try to open/close spotlight in the Astro dev toolbar.
However, given the age of this issue, if things work fine for now (with the global even target), I suggest we close this and reopen/open if we get reports. wdyt?
let's see if we can keep this closed