Code Monkey home page Code Monkey logo

workerify's Introduction

workerify

Transform web workers into browserified inline Blobs with browserify.

example

Your entry point main.js:

var mod = require('module')
var worker = new Worker('worker.js')

Your worker entry point worker.js:

self.onmessage = function(e) {
  var ab = new Uint8Array(10)
  for (var n = 0; n < ab.length; n++) ab[n] = 1
  self.postMessage(ab.buffer, [ab.buffer])
}

Browserify with this workerify transform:

browserify -t workerify main.js > bundle.js

and your bundle.js will look like:

var mod = require('module')
var worker = new Worker(window.URL.createObjectURL(new Blob(['BROWSERIFIED CONTENTS OF worker.js'])));

Modular Workers

The main reason for this is modular workers.

Let's say you create a module that would like to use web workers. Users would need to configure the URL to the worker. When your module becomes a dependency of a dependency and so on, the setup becomes really cumbersome. Especially when your worker needs to be browserified.

With this transform you simply npm install workerify --save and configure your module's package.json to apply the transform:

{
  "name": "mymodule",
  "browserify": {
    "transform": "workerify"
  }
}

Now when end users browserify your module, anywhere in the dependency tree, it will browserify and inline the worker. No URLs, no extra build steps and no additional end user requirements.

Notes

This is a work in progress. Currently it will only transform new Worker('string'). But it should also detect if a worker is variable identifier so it can be used with libs like workerstream.

install

With npm do:

npm install workerify

release history

  • 0.1.0 - initial release

license

Copyright (c) 2013 Kyle Robinson Young
Licensed under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.