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

Commit

Permalink
feat: add the workerType option (replaces #178) (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath authored Jun 30, 2020
1 parent 0efd0e4 commit f03498d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ module.exports = {
};
```

### workerType

Type: `string`
Default: `Worker`

Set the worker type. Defaults to `Worker`. Supports `ServiceWorker`, `SharedWorker`.

## Examples

### Basic
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
},
"publicPath": {
"type": "string"
},
"workerType": {
"type": "string"
}
},
"additionalProperties": false
Expand Down
22 changes: 17 additions & 5 deletions src/workers/InlineWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@

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

module.exports = function inlineWorker(content, url) {
function CreateWorker(url, workerType) {
switch (workerType) {
case 'SharedWorker':
return new SharedWorker(url);
case 'ServiceWorker':
return new ServiceWorker(url);
default:
return new Worker(url);
}
}

module.exports = function inlineWorker(content, url, workerType) {
try {
try {
var blob;
Expand All @@ -28,17 +39,18 @@ module.exports = function inlineWorker(content, url) {
blob = new Blob([content]);
}

return new Worker(URL.createObjectURL(blob));
return CreateWorker(URL.createObjectURL(blob), workerType);
} catch (e) {
return new Worker(
'data:application/javascript,' + encodeURIComponent(content)
return CreateWorker(
'data:application/javascript,' + encodeURIComponent(content),
workerType
);
}
} catch (e) {
if (!url) {
throw Error('Inline worker is not supported');
}

return new Worker(url);
return CreateWorker(url, workerType);
}
};
16 changes: 14 additions & 2 deletions src/workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ const getWorker = (file, content, options) => {

return `require(${InlineWorkerPath})(${JSON.stringify(
content
)}, ${fallbackWorkerPath})`;
)}, ${fallbackWorkerPath}, ${options.workerType})`;
}

return `new Worker(${publicWorkerPath})`;
let worker = 'Worker';
switch (options.workerType) {
case 'SharedWorker':
worker = 'SharedWorker';
break;
case 'ServiceWorker':
worker = 'ServiceWorker';
break;
default:
worker = 'Worker';
}

return `new ${worker}(${publicWorkerPath})`;
};

export default getWorker;

0 comments on commit f03498d

Please sign in to comment.