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

Commit f03498d

Browse files
authoredJun 30, 2020
feat: add the workerType option (replaces #178) (#247)
1 parent 0efd0e4 commit f03498d

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ module.exports = {
176176
};
177177
```
178178

179+
### workerType
180+
181+
Type: `string`
182+
Default: `Worker`
183+
184+
Set the worker type. Defaults to `Worker`. Supports `ServiceWorker`, `SharedWorker`.
185+
179186
## Examples
180187

181188
### Basic

‎src/options.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
},
1313
"publicPath": {
1414
"type": "string"
15+
},
16+
"workerType": {
17+
"type": "string"
1518
}
1619
},
1720
"additionalProperties": false

‎src/workers/InlineWorker.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55

66
var URL = window.URL || window.webkitURL;
77

8-
module.exports = function inlineWorker(content, url) {
8+
function CreateWorker(url, workerType) {
9+
switch (workerType) {
10+
case 'SharedWorker':
11+
return new SharedWorker(url);
12+
case 'ServiceWorker':
13+
return new ServiceWorker(url);
14+
default:
15+
return new Worker(url);
16+
}
17+
}
18+
19+
module.exports = function inlineWorker(content, url, workerType) {
920
try {
1021
try {
1122
var blob;
@@ -28,17 +39,18 @@ module.exports = function inlineWorker(content, url) {
2839
blob = new Blob([content]);
2940
}
3041

31-
return new Worker(URL.createObjectURL(blob));
42+
return CreateWorker(URL.createObjectURL(blob), workerType);
3243
} catch (e) {
33-
return new Worker(
34-
'data:application/javascript,' + encodeURIComponent(content)
44+
return CreateWorker(
45+
'data:application/javascript,' + encodeURIComponent(content),
46+
workerType
3547
);
3648
}
3749
} catch (e) {
3850
if (!url) {
3951
throw Error('Inline worker is not supported');
4052
}
4153

42-
return new Worker(url);
54+
return CreateWorker(url, workerType);
4355
}
4456
};

‎src/workers/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,22 @@ const getWorker = (file, content, options) => {
1717

1818
return `require(${InlineWorkerPath})(${JSON.stringify(
1919
content
20-
)}, ${fallbackWorkerPath})`;
20+
)}, ${fallbackWorkerPath}, ${options.workerType})`;
2121
}
2222

23-
return `new Worker(${publicWorkerPath})`;
23+
let worker = 'Worker';
24+
switch (options.workerType) {
25+
case 'SharedWorker':
26+
worker = 'SharedWorker';
27+
break;
28+
case 'ServiceWorker':
29+
worker = 'ServiceWorker';
30+
break;
31+
default:
32+
worker = 'Worker';
33+
}
34+
35+
return `new ${worker}(${publicWorkerPath})`;
2436
};
2537

2638
export default getWorker;

0 commit comments

Comments
 (0)
This repository has been archived.