Skip to content

Commit

Permalink
fix: ponyfill SharedWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS committed Nov 17, 2023
1 parent ac789b2 commit ff780c5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"types": "dist/index.d.ts",
"exports": {
"./worker": {
"types": "./dist/common/webpack/worker/web-worker.d.ts",
"default": "./dist/common/webpack/worker/web-worker.js"
"types": "./dist/common/webpack/worker/web-worker.d.mts",
"default": "./dist/common/webpack/worker/web-worker.mjs"
},
".": {
"types": "./dist/index.d.ts",
Expand All @@ -22,7 +22,7 @@
"./dist/index.d.ts"
],
"worker": [
"./dist/common/webpack/worker/web-worker.d.ts"
"./dist/common/webpack/worker/web-worker.d.mts"
]
}
},
Expand Down Expand Up @@ -68,6 +68,7 @@
"@babel/preset-react": "^7.22.0",
"@babel/preset-typescript": "^7.22.0",
"@babel/runtime": "^7.22.0",
"@okikio/sharedworker": "^1.0.4",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.11",
"@sentry/webpack-plugin": "^2.7.1",
"@statoscope/webpack-plugin": "^5.27.0",
Expand Down
45 changes: 45 additions & 0 deletions src/common/webpack/worker/web-worker.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// eslint-disable-next-line camelcase
declare let __webpack_public_path__: string;

// @ts-expect-error ts does not find types in @okikio/sharedworker/@types/index.d.ts
import {SharedWorkerPolyfill} from '@okikio/sharedworker';

class WebWorker extends Worker {
constructor(url: string | URL, options?: WorkerOptions) {
const objectURL = generateWorkerLoader(url);
super(objectURL, options);
URL.revokeObjectURL(objectURL);
}
}

class SharedWebWorker extends SharedWorkerPolyfill {
constructor(url: string | URL, options?: string | WorkerOptions) {
const objectURL = generateWorkerLoader(url);
super(objectURL, options);
URL.revokeObjectURL(objectURL);
}
}

export {WebWorker as Worker, SharedWebWorker as SharedWorker};

function generateWorkerLoader(url: string | URL) {
// eslint-disable-next-line camelcase
const publicPath = __webpack_public_path__;
const workerPublicPath = publicPath.match(/^https?:\/\//)
? publicPath
: new URL(publicPath, window.location.href).toString();
const objectURL = URL.createObjectURL(
new Blob(
[
[
`self.__PUBLIC_PATH__ = ${JSON.stringify(workerPublicPath)}`,
`importScripts(${JSON.stringify(url.toString())});`,
].join('\n'),
],
{
type: 'application/javascript',
},
),
);
return objectURL;
}
5 changes: 4 additions & 1 deletion src/common/webpack/worker/web-worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// eslint-disable-next-line camelcase
declare let __webpack_public_path__: string;

// @ts-expect-error ts does not find types in @okikio/sharedworker/@types/index.d.ts
import {SharedWorkerPolyfill} from '@okikio/sharedworker';

class WebWorker extends Worker {
constructor(url: string | URL, options?: WorkerOptions) {
const objectURL = generateWorkerLoader(url);
Expand All @@ -9,7 +12,7 @@ class WebWorker extends Worker {
}
}

class SharedWebWorker extends SharedWorker {
class SharedWebWorker extends SharedWorkerPolyfill {
constructor(url: string | URL, options?: string | WorkerOptions) {
const objectURL = generateWorkerLoader(url);
super(objectURL, options);
Expand Down

0 comments on commit ff780c5

Please sign in to comment.