Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

fix: make inline workers work from inside workers #307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -41,8 +41,8 @@
"webpack": "^4.0.0 || ^5.0.0"
},
"dependencies": {
"schema-utils": "^3.0.0",
"loader-utils": "^2.0.0"
"loader-utils": "^2.0.0",
"schema-utils": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.12.8",
Expand Down
22 changes: 13 additions & 9 deletions src/runtime/inline.js
Expand Up @@ -2,20 +2,21 @@
/* eslint-disable no-undef, no-use-before-define, new-cap */

module.exports = (content, workerConstructor, workerOptions, url) => {
const globalScope = self || window;
try {
try {
let blob;

try {
// New API
blob = new window.Blob([content]);
blob = new globalScope.Blob([content]);
} catch (e) {
// BlobBuilder = Deprecated, but widely implemented
const BlobBuilder =
window.BlobBuilder ||
window.WebKitBlobBuilder ||
window.MozBlobBuilder ||
window.MSBlobBuilder;
globalScope.BlobBuilder ||
globalScope.WebKitBlobBuilder ||
globalScope.MozBlobBuilder ||
globalScope.MSBlobBuilder;

blob = new BlobBuilder();

Expand All @@ -24,15 +25,18 @@ module.exports = (content, workerConstructor, workerOptions, url) => {
blob = blob.getBlob();
}

const URL = window.URL || window.webkitURL;
const URL = globalScope.URL || globalScope.webkitURL;
const objectURL = URL.createObjectURL(blob);
const worker = new window[workerConstructor](objectURL, workerOptions);
const worker = new globalScope[workerConstructor](
objectURL,
workerOptions
);

URL.revokeObjectURL(objectURL);

return worker;
} catch (e) {
return new window[workerConstructor](
return new globalScope[workerConstructor](
`data:application/javascript,${encodeURIComponent(content)}`,
workerOptions
);
Expand All @@ -42,6 +46,6 @@ module.exports = (content, workerConstructor, workerOptions, url) => {
throw Error("Inline worker is not supported");
}

return new window[workerConstructor](url, workerOptions);
return new globalScope[workerConstructor](url, workerOptions);
}
};