andywer / threads-plugin

📦 Makes your threads.js code build with webpack out of the box.
Apache License 2.0
28 stars 6 forks source link

webpack loads wrong worker when using two worker #6

Closed MuffinK closed 5 years ago

MuffinK commented 5 years ago

When using two worker in one js file, it seems webpack will resolve them to the same worker js file ?

A little demo shown as below:

index.js

import { spawn, Thread, Worker } from "threads"

async function main() {
  const add = await spawn(new Worker("./workers/add"))
  const sub = await spawn(new Worker("./workers/sub"))
  const sum = await add(2, 3)
  const min = await sub(2, 3)

  console.log(`2 + 3 = ${sum}`)
  // 2 + 3 = -1     why ???
  console.log(`2 - 3 = ${min}`)
  // 2 - 3 = -1

  await Thread.terminate(add)
}

main().catch(console.error)

webpack.config.js

const ThreadsPlugin = require("threads-plugin");
module.exports = {
  mode: "production",
  plugins: [new ThreadsPlugin()]
};

add.js

import { expose } from "threads/worker"
expose(function add(a, b) {
  return a + b
})

sub.js

import { expose } from "threads/worker"
expose(function add(a, b) {
  return a - b
})

PS. webpack shows this warn message when bundleing:

Warning (worker-plugin): output.globalObject is set to "window". It must be set to "self" to support HMR in Workers.

Does this warning matters ? I have read this issue, and changed webpack.config.js to

const ThreadsPlugin = require("threads-plugin");
module.exports = {
  mode: "production",
  plugins: [new ThreadsPlugin({globalObject: 'self'})]
};

The warn message is gone, but the bundle result is still wrong.

Hoping for your response Thank you

andywer commented 5 years ago

Hey @MuffinK!

Yeah, that warning message is unrelated to the issue you describe here. Sounds pretty severe. I will have a look.

andywer commented 5 years ago

I moved the issue into the threads-plugin repo: andywer/threads-plugin#6

andywer commented 5 years ago

Good news! The original repo that this one is based on already has a fix for that: GoogleChromeLabs/worker-plugin#35

Will port the fix now...

andywer commented 5 years ago

Fix has been published as v1.1.0

MuffinK commented 5 years ago

I have tried the update, the bug is fixed, but another warn message is shown.

    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

    For more details see: https://github.com/andywer/threads-plugin

Thanks again for the bug fix and your awesome library😄

andywer commented 5 years ago

Hmm, that message is only shown if the plugin ran, but didn't find any new Worker() expression.

Is the code you are working on open-source? Would be interested to check why you see that message...

Or do you run webpack multiple times on different entrypoints maybe?

MuffinK commented 5 years ago

Hmm, that message is only shown if the plugin ran, but didn't find any new Worker() expression.

Is the code you are working on open-source? Would be interested to check why you see that message...

Or do you run webpack multiple times on different entrypoints maybe?

I'm sorry, It was my fault. Everything work fine now ,Thank you.