Code Monkey home page Code Monkey logo

rollup-plugin-webworkify's Introduction

rollup-plugin-webworkify

Bundles a single JS file alongside your main source code as the source for a Web Worker. Provides a fallback for running the code in the main thread if the browser does not support creating Workers from blobs.

inspired by webworkify

Install

yarn add rollup-plugin-webworkify -D

Require the plugin and add it to your configuration:

import webworkify from 'rollup-plugin-webworkify';

export default {
  entry: 'src/main.js',
  plugins: [
    webworkify({
      // specifically patten files
      pattern: '**/*.worker.js'  // Default: undefined (follow micromath globs)
    })
  ],
  format: 'umd'
};

Example

For each worker that you want to create, import the file with a worker# prefix:

// import work from 'worker#../lib/worker.js'
import work from '../lib/foo.worker.js'

var w = new work
w.addEventListener('message', function (e) {
  console.log(e.data)
})

w.postMessage(4) // send the worker a message

then foo.worker.js, The worker function lives inside of the module.exports:

var gamma = require('gamma')

module.exports = function (self) {
  self.addEventListener('message',function (e) {
    var startNum = parseInt(e.data); // e.data=4 from main.js

    setInterval(function () {
      var r = startNum / Math.random() - 1
      self.postMessage([ startNum, r, gamma(r) ])
    }, 500)
  })
}

rollup-plugin-webworkify's People

Contributors

allex avatar

Stargazers

丹魄Tempranillo avatar Rod Lewis avatar  avatar Baltazar Gomez avatar Jonathan Lurie avatar Rowan Winsemius avatar

Watchers

 avatar James Cloos avatar Alessandro Grosselle avatar  avatar

Forkers

augusttty

rollup-plugin-webworkify's Issues

Supporting a glob pattern in options

Hi there,

Thanks for a neat little plugin.

I was wondering if it would be possible to add options to the plugin config that allowed you to pass in a glob pattern to test if something was a worker, rather than relying on prepending worker#.., this would provide comparable configuration as the webpack worker-loader

For example in my src code I'd write

import Worker from './decoder.worker';

And then in my rollup config I'd write

plugins: [
   webworkify({
     pattern: '*.worker.js'
   }) 
]

This would allow my source code to be less polluted by plugin-specific ways of doing things. What do you think?

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.