
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
webworkify-webpack
Advanced tools
launch a web worker that can require() in the browser with webpack
inspired by webworkify
First, a main.js
file will launch the worker.js
and print its output:
import work from 'webworkify-webpack';
let w = work(require('./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) { // use named function instead of anonymous to prevent possible issues (check the second caveat)
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 ]
...
import work from 'webworkify-webpack'
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.
With npm do:
npm install webworkify-webpack --save
MIT
FAQs
launch a web worker at runtime that can require() in the browser with webpack
The npm package webworkify-webpack receives a total of 46,356 weekly downloads. As such, webworkify-webpack popularity was classified as popular.
We found that webworkify-webpack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.