Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
echb opened this issue Aug 31, 2021 · 1 comment
Open

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

echb opened this issue Aug 31, 2021 · 1 comment

Comments

@echb
Copy link

echb commented Aug 31, 2021

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

@guozhenma
Copy link

I have encountered the same problem. Did you find a solution please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants