fix(worker): make Comunica QueryEngine work in Vite browser worker#9
Merged
Conversation
Vite's Rollup worker bundler wraps CJS modules in require_libXXX stubs
that break Comunica v5's circular actor/bus/mediator dep graph, causing
every queryGraph call to fail at runtime despite unit tests passing.
Two root causes identified via live browser reproduction:
1. node:diagnostics_channel (lru-cache) — Vite's browser-external
Proxy stub returns undefined for all property accesses, so
(0, L.channel)("lru-cache:metrics") throws at init time.
2. `global` (promise-polyfill) — not defined in browser workers;
cascades into broken __commonJS wrappers for downstream actors.
Fix:
- Add vite-plugin-worker-comunica.ts: intercepts the Comunica import
in worker.plugins and substitutes an esbuild-compiled flat ESM bundle
(same strategy as InProcessWorker tests that already pass).
- optimizeDeps.esbuildOptions: stub node:diagnostics_channel + define
global→globalThis so the dep-optimizer pre-bundle is also clean.
- e2e/sparql-worker.spec.ts: browser regression test covering INSERT,
SELECT, CONSTRUCT, DELETE, and malformed-SPARQL error path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
queryGraphMCP tool always failed in the browser with errors like:"(0, L.channel) is not a function"—lru-cacheimportsnode:diagnostics_channel; Vite's browser-external Proxy stub returnsundefinedfor all property accesses"Class extends value undefined is not a constructor or null"—promise-polyfilldoesroot = global;globalis undefined in browser workers; the broken__commonJSwrapper caches empty{}exports; downstream class inheritance failsRoot cause: two separate failure paths, both in how Vite pre-bundles Comunica's dependency chain for the browser worker context.
Fix
vite-plugin-worker-comunica.ts(new):diagnosticsChannelStub— esbuild plugin that replacesnode:diagnostics_channelwith a no-op stub providing realchannel()/tracingChannel()functionsworkerComunicaPlugin()— Vite plugin for production Rollup worker builds: intercepts@comunica/query-sparql-rdfjs, pre-bundles it with esbuild (flat ESM, correct init order), serves as a virtual module; same strategy asInProcessWorkertestsvite.config.ts:worker.plugins: () => [workerComunicaPlugin()]— activates the esbuild pre-bundle for Rollup worker builds (production)optimizeDeps.esbuildOptionswithdefine: { global: "globalThis" }andplugins: [diagnosticsChannelStub]— fixes dev mode pre-bundle (Vite serves workers as native ESM in dev;worker.pluginsRollup hooks don't run)e2e/sparql-worker.spec.ts(new):window.__mcpTools.queryGraphdirectly in the browserVerification
vite dev) and production (vite build) modes verifiedBranch
Derived from
main(not fromfeat/relay-dispatch-hardening).