All Projects → borisirota → Webworkify Webpack

borisirota / Webworkify Webpack

launch a web worker at runtime that can require() in the browser with webpack

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webworkify Webpack

Worker Plugin
👩‍🏭 Adds native Web Worker bundling support to Webpack.
Stars: ✭ 1,840 (+1652.38%)
Mutual labels:  webpack, workers, web-worker
Preact Worker Demo
Demo of preact rendering an entire app in a Web Worker.
Stars: ✭ 204 (+94.29%)
Mutual labels:  webpack, workers, web-worker
Web Worker Proxy
A better way of working with web workers
Stars: ✭ 218 (+107.62%)
Mutual labels:  web-worker, browser
Obsolete Webpack Plugin
🌈 A Webpack plugin generates a browser-side standalone script that detects browser compatibility based on `Browserslist` and prompts website users to upgrade it.
Stars: ✭ 148 (+40.95%)
Mutual labels:  webpack, browser
Workerize Loader
🏗️ Automatically move a module into a Web Worker (Webpack loader)
Stars: ✭ 2,135 (+1933.33%)
Mutual labels:  webpack, web-worker
React Svg Inline
DEPRECATED, I recommend you the tool SVGR
Stars: ✭ 230 (+119.05%)
Mutual labels:  webpack, inline
Vue Svg Inline Loader
Webpack loader used for inline replacement of SVG images with actual content of SVG files in Vue projects.
Stars: ✭ 105 (+0%)
Mutual labels:  webpack, inline
React Native Threads
Create new JS processes for CPU intensive work
Stars: ✭ 527 (+401.9%)
Mutual labels:  workers, web-worker
Via.js
Write JS code that runs in a different context. E.g. use the DOM in a Web Worker.
Stars: ✭ 412 (+292.38%)
Mutual labels:  workers, web-worker
purescript-workers
An API wrapper around Web Workers (Dedicated, Shared and Service)
Stars: ✭ 24 (-77.14%)
Mutual labels:  workers, web-worker
React Article Bucket
总结,积累,分享,传播JavaScript各模块核心知识点文章全集,欢迎star,issue(勿fork,内容可能随时修改)。webpack核心内容部分请查看专栏: https://github.com/liangklfangl/webpack-core-usage
Stars: ✭ 750 (+614.29%)
Mutual labels:  webpack, browser
Defiant.js
http://defiantjs.com
Stars: ✭ 907 (+763.81%)
Mutual labels:  web-worker, browser
Conf
Landing page for event React Conf Brazil
Stars: ✭ 104 (-0.95%)
Mutual labels:  webpack
Web Client
Generic Linked Data browser and UX component framework. Apache license.
Stars: ✭ 105 (+0%)
Mutual labels:  browser
Forge React App
Start building React apps using TypeScript or ECMAScript
Stars: ✭ 104 (-0.95%)
Mutual labels:  webpack
Angular Es6
Angular ES6 utility library. Write directives, controllers and services as ES6 classes.
Stars: ✭ 103 (-1.9%)
Mutual labels:  webpack
Simple React Full Stack
Boilerplate to build a full stack web application using React, Node.js, Express and Webpack.
Stars: ✭ 1,506 (+1334.29%)
Mutual labels:  webpack
Redtamarin
AS3 running on the command line / server side
Stars: ✭ 105 (+0%)
Mutual labels:  runtime
React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-1.9%)
Mutual labels:  webpack
Webpack Format Messages
Beautiful formatting for Webpack messages; ported from Create React App!
Stars: ✭ 103 (-1.9%)
Mutual labels:  webpack

webworkify-webpack

Generates a web worker at runtime from webpack's bundled modules with only the used dependencies. Possible because of webpack's module structure. Just require.resolve(PATH_TO_MODULE) the module you want to be the worker's entry point.

inspired by webworkify

install

npm install webworkify-webpack --save

v2 vs v1

For v1 go to: 1.1.8

Version 2 uses webpack's api as much as possible vs the hacky implementation of version 1 (I wasn't aware of webpack's api while writing it) which did the job but with some inconsistency between different browsers and some caveats.

In v2:

  • no limitation on webpack's devtool - eval was forbidden in v1.
  • no issues with anonymous functions exported as module.exports - there were issues with anonymous functions in v1.
  • require.resolve instead of regular require\import - The only limitation is using require.resolve which means that currently the code using webworkify-webpack is coupled to the build tool (webpack - but who uses webworkify-webpack already uses webpack) and its not possible to use es2015 modules => checkout out the future work section.

webworkify-webpack vs webpack's worker-loader and target: 'webworker'

webworkify-webpack allows to use one bundle for running same code both on browser and web worker environments. webpack's current alternatives for web workers are creating bundle which can be run in a web worker environment only and can results in 2 separate files like in the worker-loader case (one file for browser and one for web worker => code duplication).

The motivation for webworkify-webpack was creating a library which expose to the user the same functionality both in sync and async forms. I wanted to keep one bundle in order to reduce complexity of using external library to the minimum and make bundle size as minimal as possible when using external library which supports both sync and async functionality (without code duplication).

Since webpack's solutions for web workers are being constructed at compile time, the added value is that its possible to use dev tools like hmr (at least when using target: 'webworker') which isn't possible with webworkify-webpack.
In addition, regular js syntax is being used without the need to use require.resolve as in the webworkify-webpack case => checkout out the future work section.

methods

import work from 'webworkify-webpack'

let w = work(require.resolve(modulePath) [, options])

Return a new web worker from the module at modulePath.

The file at modulePath should export its worker code in module.exports as a function that will be run with no arguments.

Note that all the code outside of the module.exports function will be run in the main thread too so don't put any computationally intensive code in that part. It is necessary for the main code to require() the worker code to fetch the module reference and load modulePath's dependency graph into the bundle output.

options

  • all - bundle all the dependencies in the web worker and not only the used ones. can be useful in edge cases that I'm not aware of when the used dependencies aren't being resolved as expected due to the runtime regex checking mechanism or just to avoid additional work at runtime to traverse the dependencies tree.
  • bare - the return value will be the blob constructed with the worker's code and not the web worker itself.

example

First, a main.js file will launch the worker.js and print its output:

import work from 'webworkify-webpack';

let w = work(require.resolve('./worker.js'));
w.addEventListener('message', event => {
    console.log(event.data);
});

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

then worker.js can require() modules of its own. The worker function lives inside of the module.exports:

import gamma from 'gamma'

module.exports = function worker (self) {
    self.addEventListener('message', (event) => {
        const startNum = parseInt(event.data); // ev.data=4 from main.js
        setInterval(() => {
            const r = startNum / Math.random() - 1;
            self.postMessage([ startNum, r, gamma(r) ]);
        }, 500);
    });
};

Now after webpackifying this example, the console will contain output from the worker:

[ 4, 0.09162078520553618, 10.421030346237066 ]
[ 4, 2.026562457360466, 1.011522336481017 ]
[ 4, 3.1853125018703716, 2.3887589540750214 ]
[ 4, 5.6989969260510005, 72.40768854476167 ]
[ 4, 8.679491643020487, 20427.19357947782 ]
[ 4, 0.8528139834191428, 1.1098187157762498 ]
[ 4, 8.068322137547542, 5785.928308309402 ]
...

future work

The goal is to make webworkify-webpack fully based on webpack's api. I'm not sure how to accomplish it since I never wrote a webpack loader\plugin (is it possible other way?) so I'm asking for help :)

Points of view:

  1. webpackBootstrapFunc - should be taken from webpack's source.
  2. ability to use regular module import\require (not require.resolve) but still passing the module id to 'webworkify-webpack'.
  3. ability to know all specific's module dependencies in compile time so there is no need to traverse the dependencies tree in runtime with regular expressions (when uglifying the code the web worker's bundle can include more dependencies than only the used ones because regular expressions nature).
  4. if there is going to be build in compile time, what about hmr as dev tool ?
  5. is the ability 'webworkify-webpack' provides should be part of webpack core as another form of web workers support or should it remain as external module ?

license

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].