threads-plugin icon indicating copy to clipboard operation
threads-plugin copied to clipboard

question, i cant make it work, i follow steps on threads.js.org

Open echb opened this issue 2 years ago • 1 comments

hi this is a question, i follow steps on threads.js.org, I've been looking for an answer but i couldn't make it work, someone knows whats happening?

packgage.json

{
  "name": "workers_threadjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack server --config webpack.dev.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^5.3.2",
    "threads-plugin": "^1.4.0",
    "webpack": "^5.48.0",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^3.11.2"
  },
  "dependencies": {
    "threads": "^1.6.5",
    "tiny-worker": "^2.3.0"
  }
}

webpack config.js

const HtmlWebpackPlugin = require("html-webpack-plugin");
const ThreadsPlugin = require('threads-plugin');
var path = require('path');

const javascriptRules = {
  test: /\.js$/,
  exclude: /(node_modules)/
}

module.exports = {
  mode: 'development',
  devServer: {
    contentBase: path.join(__dirname, 'dist/public'),
    open: false
  },
  output: {
    filename: 'index.js',
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [javascriptRules]
  },
  plugins: [
    new ThreadsPlugin(),

    new HtmlWebpackPlugin({
      filename: "index.html",
      template: 'src/views/index.html'
    })
  ]
}

main

// master.js
import { spawn, Thread, Worker } from "threads"

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

  console.log(`2 + 3 = ${sum}`)

  await Thread.terminate(add)
}

main().catch(console.error)
console.log('dasd');

on worker

// workers/add.js
import { expose } from "threads/worker"

expose(function add(a, b) {
  return a + b
})

error 1-> Loading Worker from “http://localhost:8080/workers/add” was blocked because of a disallowed MIME type (“text/html”).

warning1-> Object { message: "No instantiations of threads.js workers found.\nPlease check that:\n 1. You have configured Babel / TypeScript to not transpile ES modules\n 2. You import Worker from threads where you use it\n\nFor more details see: https://github.com/andywer/threads-plugin\n" }

error 2-> Error: Timeout: Did not receive an init message from worker after 10000ms. Make sure the worker calls expose(). timeoutHandle webpack://workers_threadjs/./node_modules/threads/dist/master/spawn.js?:35

echb avatar Aug 31 '21 18:08 echb