Skip to content

Instantly share code, notes, and snippets.

@fabiospampinato
Created June 1, 2025 20:05
Show Gist options
  • Save fabiospampinato/9670b7fe91f9f77ac2610f8dc6e80b00 to your computer and use it in GitHub Desktop.
Save fabiospampinato/9670b7fe91f9f77ac2610f8dc6e80b00 to your computer and use it in GitHub Desktop.
Transparent worker pool demo, with WorkTank and Esbuild.
/* IMPORT */
import esbuild from 'esbuild';
import worktank from 'worktank-esbuild-plugin';
/* MAIN */
esbuild.build ({
bundle: true,
minify: true,
format: 'esm',
platform: 'node',
target: 'es2018',
entryPoints: ['example.ts'],
outdir: 'dist',
plugins: [
worktank ({
filter: /\.worker\.(js|ts)$/
})
]
});
// Instructions:
// Install dependencies: npm install
// Compile the example with esbuild: node build.js
// Run the compiled example: node dist/example.js
/* IMPORT */
import {id, sum} from './example.worker';
import process from 'node:process';
/* MAIN */
const test = async (): Promise<void> => {
console.assert ( await sum ( 10, 5 ) === 15 );
const ids = await Promise.all ([ id (), id (), id () ]);
console.log ( 'IDs:', ids );
process.exit ();
};
/* RUN */
test ();
/* IMPORT */
import {threadId} from 'worker_threads';
/* MAIN */
const id = async (): Promise<number> => threadId;
const sum = async ( a: number, b: number ) => a + b;
/* EXPORT */
export {id, sum};
{
"private": true,
"name": "test",
"type": "module",
"scripts": {
"compile": "node build.js"
},
"dependencies": {
"worktank": "^3.0.2"
},
"devDependencies": {
"@types/node": "^22.15.29",
"esbuild": "^0.25.5",
"worktank-esbuild-plugin": "^2.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment