Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Run web workers from a single browserified bundle.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
LICENSE.md
README.md
bundler.js
index.js
package.json

README.md

worker-pack

A quick and dirty way of running Web Workers within a single browserified bundle.

Intended as a solution that works now, avoiding any significant tradeoffs (e.g. duplicate scripts). Here's hoping it gets rendered obsolete by workerify if/when Browserify's transform API gets a little bit more flexible.

Installation

npm install worker-pack

Usage

Firstly, though worker-pack is intended for Browserify v2 it isn't packaged as a transform stream. Instead, you just have to pipe Browserify's output through the worker-pack CLI:

# Instead of this:
browserify index.js > bundle.js
# Do this:
browserify index.js | node_modules/.bin/worker-pack > bundle.js

Now you should be able to require('worker-pack') from index.js:

var pack = require('worker-pack')

if (pack.isWorker()) {
  self.onmessage = function() { self.postMessage('beep boop') }
} else {
  var worker = pack.createWorker()
  worker.onmessage = function(e) { console.log(e.data) }
  worker.postMessage()
}

If you want to use the workerstream module, you can just get a Worker URL and pass that to the stream:

var workerstream = require('workerstream')
  , pack = require('worker-pack')

if (!pack.isWorker()) {
  var worker = workerstream(pack.createWorkerURL())
}
Something went wrong with that request. Please try again.