{"id":22490649,"url":"https://github.com/dynamind/minimal-vue-worker","last_synced_at":"2025-08-02T22:33:19.284Z","repository":{"id":40943033,"uuid":"146470075","full_name":"dynamind/minimal-vue-worker","owner":"dynamind","description":"A minimal example of a Web Worker in VueJS (Vue CLI 3)","archived":false,"fork":false,"pushed_at":"2022-12-08T23:33:18.000Z","size":1126,"stargazers_count":38,"open_issues_count":16,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-03-22T08:10:24.180Z","etag":null,"topics":["vue","webpack","worker"],"latest_commit_sha":null,"homepage":null,"language":"Vue","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dynamind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-28T15:41:40.000Z","updated_at":"2024-03-02T10:19:25.000Z","dependencies_parsed_at":"2023-01-25T08:00:24.033Z","dependency_job_id":null,"html_url":"https://github.com/dynamind/minimal-vue-worker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dynamind%2Fminimal-vue-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dynamind%2Fminimal-vue-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dynamind%2Fminimal-vue-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dynamind%2Fminimal-vue-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dynamind","download_url":"https://codeload.github.com/dynamind/minimal-vue-worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228501396,"owners_count":17930239,"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":["vue","webpack","worker"],"created_at":"2024-12-06T17:22:29.308Z","updated_at":"2024-12-06T17:25:31.812Z","avatar_url":"https://github.com/dynamind.png","language":"Vue","funding_links":[],"categories":["Vue"],"sub_categories":[],"readme":"# Minimal example of using a Web Worker with Vue\n\n## Requirements\n\n- Vue CLI 3 (see [https://cli.vuejs.org]() for installation instructions)\n- `worker-loader` to let Webpack load the worker for you\n- `promise-worker` to simplify communication with the worker\n\n## Running the example\n\nThis example uses the standard Vue CLI 3 commands:\n\n```bash\nyarn serve\n```\n\n#### Stand-alone build\n\n```bash\nyarn build\n```\n\nYou could use Python's SimpleHTTPServer to check out the build locally:\n\n```bash\ncd dist\npython -m SimpleHTTPServer\n```\n\n## Build it yourself\n\n### Create the app (using Vue CLI 3.0)\n\n```bash\nvue create minimal-vue-worker\n# select your preferred preset, such as 'default'\ncd minimal-vue-worker\n```\n\n### Add `worker-loader` and `promise-worker`\n\n```bash\nyarn add --dev worker-loader promise-worker\n```\n\n### Configuring the build with `vue.config.js`\n\n### Issue with globalObject\nSetting `globalObject` to `this` to fix a reference error: \"Uncaught ReferenceError: window is not defined\".\n\n\u003e **Note:** This is a workaround taken from webpack/webpack#6642 (comment) and should be replaced by `target: 'universal'` when webpack/webpack#6525 has been implemented.\n\n```js\nmodule.exports = {\n  chainWebpack: config =\u003e {\n    config.output\n      .globalObject('this')\n    /* ... */\n  }\n}\n```\n\n#### Hot Module Replacement in Safari\nFor HMR there's currently an issue in Safari which can be fixed with a small config change:\n\n```js\nmodule.exports = {\n  chainWebpack: config =\u003e {\n    /* ... */\n    if (process.env.NODE_ENV === 'development') {\n      config.output\n        .publicPath('/')\n        .filename('[name].[hash].js')\n        .end()\n    }\n    /* ... */\n}\n```\n\n#### Webpack Loader configuration\n\nConfiguring the `worker-loader` here using a rule does not work reliably.\n\n- Hot Module Replacement does not seem to work at all\n- Even refresh (F5) doesn't reload the worker javascript, unless the development server is restarted\n- Sometimes the contents of `index.html` is served in stead of the worker javascript!\n\nUsing the inline loader syntax works fine:\n\n```js\nimport Worker from 'worker-loader!./worker'\n```\n\nBut if it did work, I would need to add the following configuration here, which triggers the loader for any file ending with `worker.js`. You can't use this in combination with the inline syntax.\n\n```js\nmodule.exports = {\n  chainWebpack: config =\u003e {\n    /* ... */\n    config.module\n      .rule('worker')\n        .test(/worker\\.js$/)\n        .use('worker-loader')\n          .loader('worker-loader')\n      .end()\n    /* ... */\n  }\n}\n```\n\n### Create the primary entrypoints of your worker\n\n```bash\nmkdir src/my-worker\ntouch src/my-worker/index.js\ntouch src/my-worker/worker.js\n```\n\nThis is slightly opinionated, but the idea is this: the `index.js` defines the public API of your worker, and `worker.js` defines the private API. The `index.js` offers a thin layer of abstraction to simplify communication with the worker.\n\n\u003e **Note:** Take care that the `worker.js` does not import anything major from the main application, especially when you're refactoring an existing code base. You don't want to duplicate your entire app inside your worker...\n\n#### index.js\n\nThis is the script the rest of your application sees. It sets up the `postMessage` and `onMessage` code, and exports a simple API.\n\n```js\nimport PromiseWorker from 'promise-worker'\nimport Worker from 'worker-loader!./worker'\n\nconst promiseWorker = new PromiseWorker(new Worker())\n\nconst send = message =\u003e promiseWorker.postMessage({\n  type: 'message',\n  message\n})\n\nexport default {\n  send\n}\n```\n\n#### worker.js\n\nThis is where messages come in and are passed on to other modules if you like.\n\nPromise-Worker makes all this really simple and intuitive, and if you like you can still access the raw Web Worker API, i.e. `postMessage` and `onMessage`.\n\n```js\nimport registerPromiseWorker from 'promise-worker/register'\n\nregisterPromiseWorker((message) =\u003e {\n  if (message.type === 'message') {\n    return `Worker replies: ${JSON.stringify(message)}`\n  }\n})\n```\n\n#### Using the worker\n\nIn any part of your application where you want to use the worker, do something like this:\n\n```js\nimport myWorker from '@/my-worker'\n/* ... */\nmyWorker.send({ anything: 'you need' })\n  .then(reply =\u003e {\n    // Handle the reply\n  })\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdynamind%2Fminimal-vue-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdynamind%2Fminimal-vue-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdynamind%2Fminimal-vue-worker/lists"}