andywer / andywer/threads.js

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

Đang mở
#466 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
3.5k
Fork
173
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.