From c0c99614ee78d2a7bc4990ecfd20764ef8b78be5 Mon Sep 17 00:00:00 2001 From: jiechud Date: Wed, 2 Jan 2019 18:32:08 +0800 Subject: [PATCH 1/2] refactor: webpack loader --- src/options.json | 3 +++ src/workers/InlineWorker.js | 16 ++++++++++++---- src/workers/index.js | 5 +++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/options.json b/src/options.json index 27e5ea4..1f635d4 100644 --- a/src/options.json +++ b/src/options.json @@ -12,6 +12,9 @@ }, "publicPath": { "type": "string" + }, + "isSharedWorker": { + "type": "boolean" } }, "additionalProperties": false diff --git a/src/workers/InlineWorker.js b/src/workers/InlineWorker.js index f84a58f..ef42f5a 100644 --- a/src/workers/InlineWorker.js +++ b/src/workers/InlineWorker.js @@ -2,7 +2,15 @@ var URL = window.URL || window.webkitURL; -module.exports = function (content, url) { +function CreateWorker(url, isSharedWorker) { + if (isSharedWorker) { + return new isSharedWorker(url); + } else { + return Worker(url); + } +} + +module.exports = function (content, url, options) { try { try { var blob; @@ -24,15 +32,15 @@ module.exports = function (content, url) { blob = new Blob([content]); } - return new Worker(URL.createObjectURL(blob)); + return new createWorker(URL.createObjectURL(blob), options.isSharedWorker); } catch (e) { - return new Worker('data:application/javascript,' + encodeURIComponent(content)); + return new CreateWorker('data:application/javascript,' + encodeURIComponent(content), options.isSharedWorker); } } catch (e) { if (!url) { throw Error('Inline worker is not supported'); } - return new Worker(url); + return new CreateWorker(url, options.isSharedWorker); } }; diff --git a/src/workers/index.js b/src/workers/index.js index 1af9817..bd292be 100644 --- a/src/workers/index.js +++ b/src/workers/index.js @@ -18,10 +18,11 @@ const getWorker = (file, content, options) => { return `require(${InlineWorkerPath})(${JSON.stringify( content - )}, ${fallbackWorkerPath})`; + )}, ${fallbackWorkerPath}, ${options})`; } - return `new Worker(${publicWorkerPath})`; + const Worker = options.isSharedWorker ? 'SharedWorker' : 'Worker'; + return `new ${Worker}(${publicWorkerPath})`; }; export default getWorker; From 890eff9a993c3fcb092074b77e3ee7a0ed52e822 Mon Sep 17 00:00:00 2001 From: jiechud Date: Fri, 1 Feb 2019 10:25:32 +0800 Subject: [PATCH 2/2] fix: worker instance --- src/workers/InlineWorker.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/workers/InlineWorker.js b/src/workers/InlineWorker.js index ef42f5a..9e084c6 100644 --- a/src/workers/InlineWorker.js +++ b/src/workers/InlineWorker.js @@ -32,15 +32,15 @@ module.exports = function (content, url, options) { blob = new Blob([content]); } - return new createWorker(URL.createObjectURL(blob), options.isSharedWorker); + return CreateWorker(URL.createObjectURL(blob), options.isSharedWorker); } catch (e) { - return new CreateWorker('data:application/javascript,' + encodeURIComponent(content), options.isSharedWorker); + return CreateWorker('data:application/javascript,' + encodeURIComponent(content), options.isSharedWorker); } } catch (e) { if (!url) { throw Error('Inline worker is not supported'); } - return new CreateWorker(url, options.isSharedWorker); + return CreateWorker(url, options.isSharedWorker); } };