简体   繁体   English

如何在webpack中使用javascript webworker?

[英]How to use javascript webworker in webpack?

i'm trying to use web workers in my web app but i'm having a hard time. 我正在尝试在Web应用程序中使用Web Worker,但是我遇到了困难。 Adding a new entry to the webpack.config.js does not work. 将新条目添加到webpack.config.js无效。

so, I'm trying to use the npm package called worker-loader but there is no proper example on how to use it. 因此,我正在尝试使用名为worker-loader的npm软件包,但没有有关如何使用它的适当示例。 All of my attempts to use it has failed. 我所有使用它的尝试都失败了。 Can you guys please show me a simple example on how to use it. 你们能给我看一个简单的例子来说明如何使用它。

import Worker from "worker-loader!./Worker.js";
const myworker = new Worker();
myworker.postMessage(songs);
myworker.onmessage = function(Data) {
//do something
}

my webpack.config.js file is like this with 我的webpack.config.js文件是这样的

 entry: __dirname + "/js/index.js",
 output: {
 path: __dirname + "/dist",
 filename: "bundle.js"
 },  
{
  module: {
    rules: [
     {
    test: /\.worker\.js$/,
    use: { loader: 'worker-loader' }
  }
]
}
}

My server tells me the following: 我的服务器告诉我以下内容:

"GET /279b1e1fcb403131377a.worker.js HTTP/1.1" 404

Since my 279b1e1fcb403131377a.worker.js is inside /dist its giving me 404 error. 由于我的279b1e1fcb403131377a.worker.js位于/dist内部,因此出现404错误。 How can i make the request to go inside /dist . 我如何发出进入/dist的请求。

There are plenty of examples given on the official Github page: Github官方页面上提供了很多示例:

https://github.com/webpack-contrib/worker-loader https://github.com/webpack-contrib/worker-loader

That should easily get you going. 那应该很容易让你前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM