diff --git a/README.md b/README.md index 72c4e73..26a616d 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,13 @@ module.exports = { }; ``` +### sharedWorker + +Type: `boolean` +Default: `false` + +Uses SharedWorker rather than Worker. + ## Examples ### Basic diff --git a/src/options.json b/src/options.json index 27e5ea4..ffd5188 100644 --- a/src/options.json +++ b/src/options.json @@ -12,6 +12,9 @@ }, "publicPath": { "type": "string" + }, + "sharedWorker": { + "type": "boolean" } }, "additionalProperties": false diff --git a/src/workers/InlineWorker.js b/src/workers/InlineWorker.js index ada87c3..b609801 100644 --- a/src/workers/InlineWorker.js +++ b/src/workers/InlineWorker.js @@ -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; @@ -28,10 +35,10 @@ 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) { @@ -39,6 +46,6 @@ module.exports = function inlineWorker(content, url) { throw Error('Inline worker is not supported'); } - return new Worker(url); + return CreateWorker(url, options.sharedWorker); } }; diff --git a/src/workers/index.js b/src/workers/index.js index af881ee..cab74da 100644 --- a/src/workers/index.js +++ b/src/workers/index.js @@ -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;