[英]How To Create Worker in React
I'm trying to create a worker in an app created from create-react-app using react 16.8.6 and yarn 1.16.0.我正在尝试使用 react 16.8.6 和 yarn 1.16.0 在从 create-react-app 创建的应用程序中创建一个工人。 If I use
const backgroundWorker = new Worker('../assets/js/myWorker.js');
如果我使用
const backgroundWorker = new Worker('../assets/js/myWorker.js');
I get the console error: Uncaught SyntaxError: Unexpected token <我收到控制台错误:Uncaught SyntaxError: Unexpected token <
But I know that is the correct path.但我知道那是正确的道路。 This works fine in Angular.
这在 Angular 中运行良好。 Is there a good tutorial on how to create a worker in React?
是否有关于如何在 React 中创建工作线程的好教程?
The directory "assets" is the public directory for @angular/cli projects, not create-react-app projects. 目录“assets”是@ angular / cli项目的公共目录,而不是create-react-app项目。 In create-react-app the equivalent of @angular/cli "assets" is "public" which is described in the create-react-app documentation under Using the Public Folder .
在create-react-app中,@ angular / cli“assets”的等价物是“public”,在使用公用文件夹下的create-react-app文档中对此进行了描述。 Move your "js" directory and the "myWorker.js" file to the public directory and update the creation of the worker to point to that path instead:
将“js”目录和“myWorker.js”文件移动到公共目录,并更新worker的创建以指向该路径:
const backgroundWorker = new Worker('/js/myWorker.js');
You can also use process.env.PUBLIC_URL
instead: 您也可以使用
process.env.PUBLIC_URL
:
const backgroundWorker = new Worker(`${process.env.PUBLIC_URL}/js/myWorker.js`);
Hopefully that helps! 希望这有帮助!
Follow these steps in order to add your worker files to your create-react-app
project.按照以下步骤将您的工作文件添加到您的
create-react-app
项目。
1. Install these three packages: 1.安装这三个包:
$ yarn add worker-plugin --dev
$ yarn add comlink
$ yarn add react-app-rewired --dev
2. Override webpack
config: 2. 覆盖
webpack
配置:
In your project's root
directory create a config-overrides.js
file with the following content:在项目的
root
目录中创建一个config-overrides.js
文件,内容如下:
const WorkerPlugin = require("worker-plugin");
module.exports = function override(config, env) {
//do stuff with the webpack config...
config.plugins = [new WorkerPlugin({ globalObject: "this"}), ...config.plugins]
return config;
}
3. Replace your npm
scripts inside package.json
by: 3. 将
package.json
的npm
脚本替换为:
"start": "react-app-rewired start",
"build": "react-app-rewired build",
4. Create two files in order to test your configuration: 4. 创建两个文件以测试您的配置:
worker.js
import * as Comlink from "comlink";
class WorkerWorld {
sayHello() {
console.log("Hello! I am doing a heavy task.")
let numbers = Array(500000).fill(5).map(num => num * 5);
return numbers;
}
}
Comlink.expose(WorkerWorld)
use-worker.js
import * as Comlink from "comlink";
const initWorker = async () => {
const workerFile = new Worker("./worker", { name: "my-worker", type: "module" });
const WorkerClass = Comlink.wrap(workerFile)
const instance = await new WorkerClass();
const result = await instance.sayHello();
console.log("Result of my worker's computation: ", result);
}
initWorker()
5. See the output: 5. 查看输出:
$ yarn start
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.