andywer / andywer/threads.js

No instantiations of threads.js workers found in TypeScript/Webpack setup

Open
#466 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3.5k
Forks
173
PR merge metrics
No merged PRs in 30d

Description

Hello. I am working on an Electron app that uses TypeScript with Webpack and I recently installed `threads.js`. I followed the instructions on configuring TypeScript and Webpack and I keep getting this warning from webpack:

```
WARNING in No instantiations of threads.js workers found.
Please check that:
1. You have configured Babel / TypeScript to not transpile ES modules
2. You import `Worker` from `threads` where you use it
```

If I attempt to run the resulting bundle via `Node.js`, I get an exception saying that the thread script doesn't exist (which it doesn't since it isn't being bundled). I primarily use Node.js `v19.5.0` but I also tested this with `v18.0.0` and `v16.0.0` and all gave me the same result.

I created a minimal example that reproduces the issue in full. Here are all my files.

`src/index.ts`
```ts
import { spawn, Thread, Worker } from "threads";
import { Stuff } from "./stuff";

(async () => {
const stuffThread = await spawn(new Worker("./stuff"));

console.log(await stuffThread.go());

await Thread.terminate(stuffThread);
})();
```

`src/stuff.ts`
```ts
import { expose } from "threads/worker";

const stuff = {
go: async () => {
return 3;
},
};

export type Stuff = typeof stuff;

expose(stuff);
```

`webpack.config.js`
```js
const path = require("path");
const ThreadsPlugin = require('threads-plugin');

module.exports = {
target: "electron22.2-main",
entry: {
main: "./src/index.ts"
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.ts$/,
loader: "ts-loader",
exclude: /node_modules/,
options: {
configFile: "tsconfig.json"
}
}
]
},
resolve: {
extensions: [
".tsx",
".ts",
".js"
]
},
plugins: [
new ThreadsPlugin()
]
};
```

`tsconfig.json`
```json
{
"compilerOptions": {
"outDir": "dist",
"module": "ESNext",
"lib": [
"ESNext",
],
"moduleResolution": "node"
}
}
```

`package.json`
```json
{
"scripts": {
"build": "webpack"
},
"dependencies": {
"@babel/types": "^7.21.2",
"threads": "^1.7.0",
"threads-plugin": "^1.4.0",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"webpack": "^5.76.0"
},
"devDependencies": {
"webpack-cli": "^5.0.1"
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.