{"id":13417195,"url":"https://github.com/mathe42/vite-plugin-comlink","last_synced_at":"2025-05-15T14:08:49.735Z","repository":{"id":38339364,"uuid":"399387060","full_name":"mathe42/vite-plugin-comlink","owner":"mathe42","description":"Use WebWorkers in Vite with comlink!","archived":false,"fork":false,"pushed_at":"2025-05-14T22:35:55.000Z","size":455,"stargazers_count":201,"open_issues_count":12,"forks_count":18,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-05-14T23:28:10.058Z","etag":null,"topics":["comlink","vite","vite-plugin","vitejs","webworker"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathe42.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-08-24T08:15:06.000Z","updated_at":"2025-05-02T07:36:19.000Z","dependencies_parsed_at":"2023-02-18T20:16:03.988Z","dependency_job_id":"de4e2c2a-5176-4241-bcc7-3e21489709e7","html_url":"https://github.com/mathe42/vite-plugin-comlink","commit_stats":{"total_commits":159,"total_committers":11,"mean_commits":"14.454545454545455","dds":0.5157232704402516,"last_synced_commit":"e5efeeb53ac15e25224ccca8b32f6e05b7d9e568"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathe42%2Fvite-plugin-comlink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathe42%2Fvite-plugin-comlink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathe42%2Fvite-plugin-comlink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathe42%2Fvite-plugin-comlink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathe42","download_url":"https://codeload.github.com/mathe42/vite-plugin-comlink/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254249177,"owners_count":22039027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["comlink","vite","vite-plugin","vitejs","webworker"],"created_at":"2024-07-30T22:00:33.730Z","updated_at":"2025-05-15T14:08:44.724Z","avatar_url":"https://github.com/mathe42.png","language":"TypeScript","readme":"# vite-plugin-comlink\n\n![NPM Version](https://img.shields.io/npm/l/vite-plugin-comlink)\n![NPM Version](https://img.shields.io/npm/v/vite-plugin-comlink)\n![NPM Version](https://img.shields.io/bundlephobia/minzip/vite-plugin-comlink)\n![NPM Version](https://img.shields.io/npm/dm/vite-plugin-comlink)\n![NPM Version](https://img.shields.io/npm/d18m/vite-plugin-comlink)\n\n\n\u003e This plugins requires vite \u003e=2.8 for WebWorkers and vite \u003e= 2.9.6 for shared worker to work properly.\n\nUse WebWorkers with comlink.\n\nThis plugin removes the need to call `expose`, `wrap` from comlink and also you don't need to create the worker on your own.\n\n## Upgrade Vite 4 to Vite 5\n\nMake sure that you change the worker plugin config to a function for example like this\n\n```ts\n// vite.config.js\nexport default {\n  worker: {\n    plugins: () =\u003e [comlink()],\n  },\n};\n```\n\nsee https://github.com/vitejs/vite/pull/14685 for details.\n\n## Install\n\n```sh\nnpm i --save-dev vite-plugin-comlink # yarn add -D vite-plugin-comlink\nnpm i --save comlink # yarn add comlink\n```\n\n### Comlink install\n\nAs you don't want to wait for a new release for this plugin when a new version of comlink is released, this plugin has comlink as a peer dependency so you can install the version of comlink that you need.\n\n### Add it to vite.config.js\n\n```ts\n// vite.config.js\nimport { comlink } from \"vite-plugin-comlink\";\n\nexport default {\n  plugins: [comlink()],\n  worker: {\n    plugins: () =\u003e [comlink()],\n  },\n};\n```\n\n### Plugin order\n\nPut this plugin as one of the first plugins. Only other plugins that create `ComlinkWorker` or `ComlinkSharedWorker` or transform files based on the existence of `ComlinkWorker` or `ComlinkSharedWorker` should come before this plugin!\n\n## Usage\n\n```ts\n// worker.js\nexport const add = (a: number, b: number) =\u003e a + b;\n\n// main.ts\n\n// Create Worker\nconst instance = new ComlinkWorker(new URL(\"./worker.js\", import.meta.url), {\n  /* normal Worker options*/\n});\nconst result = await instance.add(2, 3);\n\nresult === 5;\n\n// Create SharedWorker\nconst instance = new ComlinkSharedWorker(\n  new URL(\"./worker.js\", import.meta.url),\n  {\n    /* normal Worker options*/\n  }\n);\nconst result = await instance.add(2, 3);\n\nresult === 5;\n```\n\n### With TypeScript\n\nAdd\n\n```ts\n/// \u003creference types=\"vite-plugin-comlink/client\" /\u003e\n```\n\nto your vite-env.d.ts file to make sure typescript will use `vite-plugin-comlink/client`.\n\n```ts\n// worker.ts\nexport const add = (a: number, b: number) =\u003e a + b;\n\n// main.ts\n\n// Create Worker\nconst instance = new ComlinkWorker\u003ctypeof import(\"./worker\")\u003e(\n  new URL(\"./worker\", import.meta.url),\n  {\n    /* normal Worker options*/\n  }\n);\nconst result = await instance.add(2, 3);\n\nresult === 5;\n\n// Create SharedWorker\nconst instance = new ComlinkSharedWorker\u003ctypeof import(\"./worker\")\u003e(\n  new URL(\"./worker\", import.meta.url),\n  {\n    /* normal Worker options*/\n  }\n);\nconst result = await instance.add(2, 3);\n\nresult === 5;\n```\n\n### Get Worker Instance\n\nYou can get to the worker instance like this:\n\n```ts\nimport { endpointSymbol } from \"vite-plugin-comlink/symbol\";\n\nconst api = new ComlinkWorker\u003ctypeof import(\"./worker\")\u003e(\n  new URL(\"./worker\", import.meta.url),\n  {\n    /* normal Worker options*/\n  }\n);\nconst worker = api[endpointSymbol];\n```\n\n## Module Worker\n\nNot all Browsers support module Workers (see https://caniuse.com/mdn-api_worker_worker_ecmascript_modules).\n\nThis results in some drawbacks for fastest and best support:\n\nFor fast development we use module Workers as bundling the complete worker on the fly is not performant.\n\nIn default settings we bundle the whole worker at build to a single file. Therefore all browsers that support Workers, work in production.\n\nThis is the same behavior as vite and it is NOT CHANGEABLE!\n\nIf you want a worker to be a module worker in production, add `type: 'module'` to the worker constructor options.\n\n### What this means:\n\n1. In development you need a browser that supports module Worker (see https://caniuse.com/mdn-api_worker_worker_ecmascript_modules)\n2. In production all browsers are supported\n\n## Breaking changes\n\n### v2 to v3\n\n- remove of customConfigs breaking FF support in development for some projects and removing the abbility for inline worker. This is a limitation of vite so if vite adds support of it this plugin will follow\n- remove of typefile. For typescript support please write your own type file or switch to the new syntax.\n- remove of ServiceWorker support. This was considered unstable an it was hacky so it got removed. If vite adds support for building ServiceWorker this will be added!\n- you have to add comlink to `worker.plugins` array.\n\n### v3 to v4\n\n- the import syntax will be removed you have to switch to the new syntax!\n- Removal of Warnings of leagacy (v2) options\n- ESM support\n- Better Source Maps\n\n### v4 to v5\n\n- some undocumented internal options got removed.\n- full rewrite of the core transformer to fix a lot of bugs\n- should be breaking free but as this is a big rewrite there might be breaking changes\n\n## Resources\n\nhttps://github.com/GoogleChromeLabs/comlink  \nhttps://github.com/surma/rollup-plugin-comlink\n","funding_links":[],"categories":["Plugins","TypeScript"],"sub_categories":["Framework-agnostic Plugins"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathe42%2Fvite-plugin-comlink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathe42%2Fvite-plugin-comlink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathe42%2Fvite-plugin-comlink/lists"}