Skip to content

Commit

Permalink
web worker support (#441)
Browse files Browse the repository at this point in the history
* web worker support

* fix lint error
  • Loading branch information
jdanyow authored and devongovett committed Dec 30, 2017
1 parent 4c67f03 commit c8a1156
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/assets/JSAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const IMPORT_RE = /\b(?:import\b|export\b|require\s*\()/;
const GLOBAL_RE = /\b(?:process|__dirname|__filename|global|Buffer)\b/;
const FS_RE = /\breadFileSync\b/;
const SW_RE = /\bnavigator\s*\.\s*serviceWorker\s*\.\s*register\s*\(/;
const WORKER_RE = /\bnew\s*Worker\s*\(/;

class JSAsset extends Asset {
constructor(name, pkg, options) {
Expand All @@ -32,7 +33,8 @@ class JSAsset extends Asset {
!/.js$/.test(this.name) ||
IMPORT_RE.test(this.contents) ||
GLOBAL_RE.test(this.contents) ||
SW_RE.test(this.contents)
SW_RE.test(this.contents) ||
WORKER_RE.test(this.contents)
);
}

Expand Down
31 changes: 25 additions & 6 deletions src/visitors/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,22 @@ module.exports = {
matchesPattern(callee, serviceWorkerPattern);

if (isRegisterServiceWorker) {
let assetPath = asset.addURLDependency(args[0].value);
if (!isURL(assetPath)) {
assetPath = urlJoin(asset.options.publicURL, assetPath);
}
args[0].value = assetPath;
asset.isAstDirty = true;
addURLDependency(asset, args[0]);
return;
}
},

NewExpression(node, asset) {
const {callee, arguments: args} = node;

const isWebWorker =
callee.type === 'Identifier' &&
callee.name === 'Worker' &&
args.length === 1 &&
types.isStringLiteral(args[0]);

if (isWebWorker) {
addURLDependency(asset, args[0]);
return;
}
}
Expand All @@ -79,3 +89,12 @@ function addDependency(asset, node, opts = {}) {
opts.loc = node.loc && node.loc.start;
asset.addDependency(node.value, opts);
}

function addURLDependency(asset, node) {
let assetPath = asset.addURLDependency(node.value);
if (!isURL(assetPath)) {
assetPath = urlJoin(asset.options.publicURL, assetPath);
}
node.value = assetPath;
asset.isAstDirty = true;
}
3 changes: 3 additions & 0 deletions test/integration/workers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
navigator.serviceWorker.register('service-worker.js', { scope: './' });

new Worker('worker.js');
1 change: 1 addition & 0 deletions test/integration/workers/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
self.addEventListener('message', () => {});
1 change: 1 addition & 0 deletions test/integration/workers/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
self.addEventListener('message', () => {});
8 changes: 6 additions & 2 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ describe('javascript', function() {
assert.equal(await output(), 3);
});

it('should support bundling service workers', async function() {
let b = await bundle(__dirname + '/integration/service-worker/index.js');
it('should support bundling workers', async function() {
let b = await bundle(__dirname + '/integration/workers/index.js');

assertBundleTree(b, {
name: 'index.js',
assets: ['index.js'],
childBundles: [
{
assets: ['service-worker.js'],
childBundles: []
},
{
assets: ['worker.js'],
childBundles: []
Expand Down

0 comments on commit c8a1156

Please sign in to comment.