andywer / andywer/threads.js

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

Abierto
#466 1 comentario 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
TypeScript
Estrellas
3.5k
Forks
173
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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"
}
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.