{"id":20661,"library":"vite-plugin-comlink","title":"vite-plugin-comlink","description":"Vite plugin that integrates Comlink to simplify Web Worker and SharedWorker usage. Current stable version is 5.3.0, released under MIT license. It removes the need for manual `expose` and `wrap` calls, automatically transforming workers referenced via `ComlinkWorker` and `ComlinkSharedWorker` constructors. Requires Vite >=2.9.6 and comlink as a peer dependency. Key differentiator: zero-config setup for Comlink with Vite, supports both module and classic workers, and provides TypeScript type inference for worker modules.","status":"active","version":"5.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/mathe42/vite-plugin-comlink","tags":["javascript","typescript"],"install":[{"cmd":"npm install vite-plugin-comlink","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-comlink","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-comlink","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; provides the Comlink RPC library for worker communication.","package":"comlink","optional":false},{"reason":"Peer dependency; required as the build tool. Must be >=2.9.6 for SharedWorker support.","package":"vite","optional":false}],"imports":[{"note":"Named export 'comlink' is the plugin function; default import is not available.","wrong":"import comlink from 'vite-plugin-comlink'","symbol":"comlink","correct":"import { comlink } from 'vite-plugin-comlink'"},{"note":"ComlinkWorker is a global constructor provided by the plugin; do not import it. It must be used with a URL object, not a string.","wrong":"const worker = new ComlinkWorker('./worker.js', {});","symbol":"ComlinkWorker","correct":"const worker = new ComlinkWorker(new URL('./worker.js', import.meta.url), {});"},{"note":"Also global; case-sensitive. For shared workers, Vite >=2.9.6 is required.","wrong":"const sharedWorker = new comlinkSharedWorker(new URL('./worker.js', import.meta.url), {});","symbol":"ComlinkSharedWorker","correct":"const sharedWorker = new ComlinkSharedWorker(new URL('./worker.js', import.meta.url), {});"}],"quickstart":{"code":"// vite.config.js\nimport { comlink } from 'vite-plugin-comlink';\n\nexport default {\n  plugins: [comlink()],\n  worker: {\n    plugins: () => [comlink()],\n  },\n};\n\n// worker.js\nexport const add = (a: number, b: number) => a + b;\n\n// main.ts\nconst instance = new ComlinkWorker<typeof import('./worker')>(\n  new URL('./worker.js', import.meta.url),\n  { /* Worker options */ }\n);\nconst result = await instance.add(2, 3);\nconsole.log(result); // 5","lang":"typescript","description":"Configures Vite with the plugin, creates a simple worker, and calls its function via Comlink."},"warnings":[{"fix":"Check for usage of internal plugin options; they may be removed. Update to latest v5.","message":"v5.0.0 introduced a full rewrite; some undocumented internal options were removed.","severity":"breaking","affected_versions":">=5.0.0 <5.0.1"},{"fix":"Add `comlink()` to both arrays in vite.config.js.","message":"The plugin must be included in both `plugins` and `worker.plugins` for proper worker transformation.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Change `worker: { plugins: [comlink()] }` to `worker: { plugins: () => [comlink()] }`.","message":"After upgrading to Vite 5, `worker.plugins` must be a function returning an array, not an array directly.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Add `/// <reference types=\"vite-plugin-comlink/client\" />` to your env.d.ts file.","message":"ComlinkWorker and ComlinkSharedWorker are not imported; they are global types/constructors provided by the plugin. TypeScript may complain if the client types are not referenced.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Avoid setting `type: 'module'` unless you understand the implications.","message":"Using `type: 'module'` in worker options may cause issues in production; plugin bundles workers as classic scripts by default.","severity":"deprecated","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add `/// <reference types=\"vite-plugin-comlink/client\" />` to your vite-env.d.ts file.","cause":"TypeScript doesn't recognize the global type; client types not loaded.","error":"Cannot find name 'ComlinkWorker'."},{"fix":"Use `new URL('./worker.js', import.meta.url)` instead of just `'./worker.js'`.","cause":"Using a string instead of a URL object for the first argument of ComlinkWorker.","error":"The argument 'path' must be a string or URL instance."},{"fix":"Ensure the worker file uses named exports and does not import from the main thread.","cause":"The worker module has circular dependencies or was not exported correctly.","error":"Cannot access 'add' before initialization."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}