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

Commit 2abd129

Browse files
authored
fix: make inline workers work from inside workers (#307)
1 parent edca167 commit 2abd129

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"webpack": "^4.0.0 || ^5.0.0"
4242
},
4343
"dependencies": {
44-
"schema-utils": "^3.0.0",
45-
"loader-utils": "^2.0.0"
44+
"loader-utils": "^2.0.0",
45+
"schema-utils": "^3.0.0"
4646
},
4747
"devDependencies": {
4848
"@babel/cli": "^7.12.8",

src/runtime/inline.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
/* eslint-disable no-undef, no-use-before-define, new-cap */
33

44
module.exports = (content, workerConstructor, workerOptions, url) => {
5+
const globalScope = self || window;
56
try {
67
try {
78
let blob;
89

910
try {
1011
// New API
11-
blob = new window.Blob([content]);
12+
blob = new globalScope.Blob([content]);
1213
} catch (e) {
1314
// BlobBuilder = Deprecated, but widely implemented
1415
const BlobBuilder =
15-
window.BlobBuilder ||
16-
window.WebKitBlobBuilder ||
17-
window.MozBlobBuilder ||
18-
window.MSBlobBuilder;
16+
globalScope.BlobBuilder ||
17+
globalScope.WebKitBlobBuilder ||
18+
globalScope.MozBlobBuilder ||
19+
globalScope.MSBlobBuilder;
1920

2021
blob = new BlobBuilder();
2122

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

27-
const URL = window.URL || window.webkitURL;
28+
const URL = globalScope.URL || globalScope.webkitURL;
2829
const objectURL = URL.createObjectURL(blob);
29-
const worker = new window[workerConstructor](objectURL, workerOptions);
30+
const worker = new globalScope[workerConstructor](
31+
objectURL,
32+
workerOptions
33+
);
3034

3135
URL.revokeObjectURL(objectURL);
3236

3337
return worker;
3438
} catch (e) {
35-
return new window[workerConstructor](
39+
return new globalScope[workerConstructor](
3640
`data:application/javascript,${encodeURIComponent(content)}`,
3741
workerOptions
3842
);
@@ -42,6 +46,6 @@ module.exports = (content, workerConstructor, workerOptions, url) => {
4246
throw Error("Inline worker is not supported");
4347
}
4448

45-
return new window[workerConstructor](url, workerOptions);
49+
return new globalScope[workerConstructor](url, workerOptions);
4650
}
4751
};

0 commit comments

Comments
 (0)