Skip to content

Commit

Permalink
Add SharedWorker functionality
Browse files Browse the repository at this point in the history
This is a slightly adjusted version of webpack-contrib#178

Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Jun 27, 2020
1 parent 0efd0e4 commit 4500188
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 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 = {
};
```

### sharedWorker

Type: `boolean`
Default: `false`

Uses SharedWorker rather than Worker.

## 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"
},
"sharedWorker": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
17 changes: 12 additions & 5 deletions src/workers/InlineWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

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

module.exports = function inlineWorker(content, url) {
function CreateWorker(url, shared) {
if (shared) {
return new SharedWorker(url);
}
return new Worker(url);
}

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

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

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

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

return `new Worker(${publicWorkerPath})`;
const worker = options.sharedWorker ? `SharedWorker` : `Worker`
return `new ${worker}(${publicWorkerPath})`;
};

export default getWorker;

0 comments on commit 4500188

Please sign in to comment.