简体   繁体   English

如何在 React 中创建 Worker

[英]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.jsonnpm脚本替换为:

"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.

相关问题 如何使用worker-loader配置创建react应用 - how to config create react app with worker-loader 如何配置 Create React Apps 默认 Service Worker 以缓存不在 react src 上下文(公共文件夹)中的资产? - How to configre Create React Apps default Service Worker to cache assets that are not in react src Context (public Folder)? Create-react-app Service Worker 不起作用 - Create-react-app service worker not functioning Service Worker无法使用create-react-app - Service Worker not working with create-react-app 没有使用 create-react-app 获得服务人员 - Not getting service worker with create-react-app 添加 Service Worker 以创建 React App 项目 - Add Service Worker to Create React App Project 如何在 create react 应用程序中从 service worker 范围中排除 url,而不必弹出? - How do I exclude url's from service worker scope in create react app, without having to eject? 如何在 create-react-app 项目中使用 Service Worker 制作自定义离线页面? - How to make a custom offline page using service worker in a create-react-app project? 如何在create-react-app中以开发人员模式启用Service Worker? - How can i enable service worker in dev mode in create-react-app? 如何使用create-react-app从服务工作者中排除一些网址? - How can i exclude some urls from service worker with create-react-app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM