From 38b5b28ff5e1eba49105f35bbf3e1317a59779aa Mon Sep 17 00:00:00 2001 From: Juli Date: Sun, 19 Aug 2018 21:47:58 +0200 Subject: [PATCH] Add support for SharedWorkers (#1907) Closes #1905 --- src/Parser.js | 2 +- src/assets/JSAsset.js | 2 +- src/visitors/dependencies.js | 2 +- test/integration/workers/index-alternative.js | 1 + test/integration/workers/index.js | 1 + test/integration/workers/shared-worker.js | 5 +++++ test/integration/workers/worker-client.js | 3 +++ test/javascript.js | 16 ++++++++++++++++ 8 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/integration/workers/shared-worker.js diff --git a/src/Parser.js b/src/Parser.js index 013aecf600b..926e91bfb97 100644 --- a/src/Parser.js +++ b/src/Parser.js @@ -83,4 +83,4 @@ class Parser { } } -module.exports = Parser; \ No newline at end of file +module.exports = Parser; diff --git a/src/assets/JSAsset.js b/src/assets/JSAsset.js index 3e089da0924..9014c359622 100644 --- a/src/assets/JSAsset.js +++ b/src/assets/JSAsset.js @@ -22,7 +22,7 @@ const ENV_RE = /\b(?:process\.env)\b/; const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer|define)\b/; const FS_RE = /\breadFileSync\b/; const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/; -const WORKER_RE = /\bnew\s*Worker\s*\(/; +const WORKER_RE = /\bnew\s*(?:Shared)?Worker\s*\(/; const SOURCEMAP_RE = /\/\/\s*[@#]\s*sourceMappingURL\s*=\s*([^\s]+)/; const DATA_URL_RE = /^data:[^;]+(?:;charset=[^;]+)?;base64,(.*)/; diff --git a/src/visitors/dependencies.js b/src/visitors/dependencies.js index 806209cc4be..635d9ae962b 100644 --- a/src/visitors/dependencies.js +++ b/src/visitors/dependencies.js @@ -81,7 +81,7 @@ module.exports = { const isWebWorker = callee.type === 'Identifier' && - callee.name === 'Worker' && + (callee.name === 'Worker' || callee.name === 'SharedWorker') && args.length === 1 && types.isStringLiteral(args[0]); diff --git a/test/integration/workers/index-alternative.js b/test/integration/workers/index-alternative.js index dfa6d9f99c4..77f2c588c8d 100644 --- a/test/integration/workers/index-alternative.js +++ b/test/integration/workers/index-alternative.js @@ -1,4 +1,5 @@ exports.startWorker = require('./worker-client').startWorker; +exports.startSharedWorker = require('./worker-client').startSharedWorker; exports.commonFunction = require('./common').commonFunction; exports.feature = require('./feature'); diff --git a/test/integration/workers/index.js b/test/integration/workers/index.js index 3e3e8fabe9c..25f1d963059 100644 --- a/test/integration/workers/index.js +++ b/test/integration/workers/index.js @@ -1,4 +1,5 @@ exports.commonFunction = require('./common').commonFunction; exports.startWorker = require('./worker-client').startWorker; +exports.startSharedWorker = require('./worker-client').startSharedWorker; exports.feature = require('./feature'); diff --git a/test/integration/workers/shared-worker.js b/test/integration/workers/shared-worker.js new file mode 100644 index 00000000000..b20ae8ba191 --- /dev/null +++ b/test/integration/workers/shared-worker.js @@ -0,0 +1,5 @@ +onconnect = (e) => { + let port = e.ports[0]; + port.addEventListener('message', ()=> {}); + port.start(); +} \ No newline at end of file diff --git a/test/integration/workers/worker-client.js b/test/integration/workers/worker-client.js index 7505793ebce..cf25588bb78 100644 --- a/test/integration/workers/worker-client.js +++ b/test/integration/workers/worker-client.js @@ -7,5 +7,8 @@ exports.startWorker = function() { worker.postMessage(commonText); }; +exports.startSharedWorker = function() { + const worker = new SharedWorker('shared-worker.js'); +}; diff --git a/test/javascript.js b/test/javascript.js index e2a28891da5..b5c6e611928 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -220,6 +220,14 @@ describe('javascript', function() { } ] }, + { + assets: ['shared-worker.js'], + childBundles: [ + { + type: 'map' + } + ] + }, { assets: ['worker.js', 'common.js'], childBundles: [ @@ -257,6 +265,14 @@ describe('javascript', function() { } ] }, + { + assets: ['shared-worker.js'], + childBundles: [ + { + type: 'map' + } + ] + }, { assets: ['worker.js', 'common.js'], childBundles: [