Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Cloud-CNC/threads-webpack-plugin

Repository files navigation

ThreadsJS Webpack integration

npm tests last commit

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).

Features

  • Written in modern TypeScript
  • Uses Rollup for TS compilation
  • Thoroughly commented

Usage

Entry

//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);

Worker

//Imports
import {expose} from 'threads';

//Worker functions
const worker = {
  echo(input)
  {
    return `Worker received: ${input}`;
  }
};

//Expose worker
expose(worker);

Webpack Config

//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
        }
      }
    ]
  }
};