{"id":13393814,"url":"https://github.com/GoogleChromeLabs/import-from-worker","last_synced_at":"2025-03-13T19:31:50.158Z","repository":{"id":44204103,"uuid":"247119933","full_name":"GoogleChromeLabs/import-from-worker","owner":"GoogleChromeLabs","description":"It’s like import(), but runs the module in a worker.","archived":false,"fork":false,"pushed_at":"2024-09-27T05:48:20.000Z","size":340,"stargazers_count":251,"open_issues_count":6,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-11T01:37:53.371Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleChromeLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-13T16:53:24.000Z","updated_at":"2025-03-04T12:45:00.000Z","dependencies_parsed_at":"2024-10-25T13:55:02.245Z","dependency_job_id":null,"html_url":"https://github.com/GoogleChromeLabs/import-from-worker","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fimport-from-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fimport-from-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fimport-from-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleChromeLabs%2Fimport-from-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleChromeLabs","download_url":"https://codeload.github.com/GoogleChromeLabs/import-from-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243469217,"owners_count":20295712,"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":[],"created_at":"2024-07-30T17:01:00.677Z","updated_at":"2025-03-13T19:31:48.641Z","avatar_url":"https://github.com/GoogleChromeLabs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# importFromWorker()\n\nIt’s like `import()`, but runs the module in a worker.\n\nThis library is a love-child of [@_developit] and [@dassurma].\n\nTotal size: 1.7K brotli’d (including [Comlink]).\n\n**Requires [modules in workers], which are currently only implemented in Chrome!**\n\n## Usage\n\n```js\n// service.js\nexport function add(a, b) {\n  return a + b;\n}\n\nexport function multiply(a, b) {\n  return a * b;\n}\n```\n\n```js\n// main.js\nimport importFromWorker from \"import-from-worker\";\n\nconst { add, multiply } = await importFromWorker(\"./service.js\");\nconsole.log(await add(40, 2));\n// ...\n```\n\nYou can also use `importFromWorker()` from a CORS-enabled CDN like [JSDelivr] or [unpkg]:\n\n```js\nimport importFromWorker from \"https://cdn.jsdelivr.net/npm/import-from-worker@latest/dist/import-from-worker.js\";\n```\n\nYou can [remix this glitch](glitch), if you like.\n\n## Q\u0026A\n\n### How does it work?\n\nThe library injects itself as the worker file and then uses dynamic `import()` to load the module. The resulting module is then exposed to the worker’s parent using [Comlink]. All guidance about callbacks and transferables from Comlink applies to this library as well. For adjustments, the library re-exports Comlink:\n\n```js\nimport importFromWorker, { Comlink } from \"import-from-worker\";\n```\n\n### Can I get a handle to the underlying worker?\n\nWhy yes you can! The module exports a `workerSymbol` that can be used thusly:\n\n```js\nimport importFromWorker, { workerSymbol } from \"import-from-worker\";\n\nconst module = await importFromWorker(\"./service.js\");\nconst worker = module[workerSymbol];\n// You could also use destructuring:\n// const {[workerSymbol]: worker, add, subtract} = await importFromWorker(\"./service.js\");\n\n// Use the worker instance directly\nworker.terminate();\n```\n\n### What about browsers without module support in workers?\n\nI tried to make the library work in those environments. It’s possible, but it gets messy. I welcome PRs (and preferably a preceding issue to discuss the design) on this problem!\n\n### How do I feature-detect module support in workers?\n\nThe snippet that’s [somewhat endorsed by the WHATWG][esm worker detection] is the following:\n\n```js\nlet supportsModuleWorker = false;\nconst workerURL = URL.createObjectURL(new Blob([\"\"]));\nconst options = {\n  get type() {\n    supportsModuleWorker = true;\n  }\n};\nnew Worker(workerURL, options).terminate();\nURL.revokeObjectURL(workerURL);\n```\n\n---\n\nLicense Apache-2.0\n\n[comlink]: https://github.com/GoogleChromeLabs/comlink\n[@_developit]: https://twitter.com/_developit\n[@dassurma]: https://twitter.com/dassurma\n[modules in workers]: https://wpt.fyi/results/workers/modules/dedicated-worker-import.any.html?label=master\u0026product=chrome%5Bstable%5D\u0026product=firefox%5Bstable%5D\u0026product=safari%5Bstable%5D\u0026product=chrome%5Bexperimental%5D\u0026product=firefox%5Bexperimental%5D\u0026product=safari%5Bexperimental%5D\u0026aligned\n[esm worker detection]: https://github.com/whatwg/html/issues/5325\n[jsdelivr]: https://www.jsdelivr.com/\n[unpkg]: https://unpkg.com/\n[glitch]: https://glitch.com/edit/#!/import-from-worker-example\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleChromeLabs%2Fimport-from-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGoogleChromeLabs%2Fimport-from-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleChromeLabs%2Fimport-from-worker/lists"}