ThreadsJS integration for Webpack. If you're using Rollup, you may want to check out rollup-plugin-threads (Basically does the same thing as this plugin but for Rollup).
- Written in modern TypeScript
- Uses Rollup for TS compilation
- Thoroughly commented
//Imports
import {BlobWorker, spawn, Thread} from 'threads';
import WorkerText from './worker'; //May have to @ts-ignore if using TypeScript
//Create a **BLOB WORKER**
const worker = await spawn(BlobWorker.fromText(WorkerText));
console.log(worker.echo('Hello World!')); //Worker received: Hello World!
//Destroy the worker
Thread.terminate(worker);
//Imports
import {expose} from 'threads';
//Worker functions
const worker = {
echo(input)
{
return `Worker received: ${input}`;
}
};
//Expose worker
expose(worker);
//Export
module.exports = {
//Point at your normal file (The plugin will take care of loading the worker)
entry: 'src/index.js',
module: {
rules: [
{
test: /worker\.js$/,
loader: 'threads-webpack-plugin',
options: {
//Webpack child bundler options
}
}
]
}
};