Skip to content

Using Web Workers in CRA? #4505

Closed
Closed

Description

@subhranshudas

I am able to use worker-loader in a simple webpack bundled app to import web worker into my APP. But how to do the same thing in CRA, i also want the capability of importing other modules to my web worker to do those tasks inside web worker.

Thanks.

Activity

  1. iansu commented on May 23, 2018

    @iansu
    Contributor

    We are hoping to add support for Web Workers and worker-loader in react-scripts 2.0: #3934

  2. subhranshudas commented on May 23, 2018

    @subhranshudas
    Author

    @iansu thanks for the reply, any way to do it if i am using an ejected setup? use the same changes of #3934 to config\webpack.config.*.js ??

  3. iansu commented on May 23, 2018

    @iansu
    Contributor

    We don't support ejected setups but that will most likely work. If not then check out the worker-loader docs.

  4. subhranshudas commented on May 23, 2018

    @subhranshudas
    Author

    i tried with whatever is provided in the worker-loader config
    package.json

     "worker-loader": "^1.1.1"
    

    webpack config

       {
            test: /\.worker\.js$/,
            use: { loader: 'worker-loader' },
          },
    

    App.js

    import Worker from './worker';
    
    const worker = new Worker();
    
    worker.postMessage({msg: "hello"});
    
    

    worker.js

    self.addEventListener('message', event => {
      console.log('Web Worker called by PARENT THREAD...', event.data);
    });
    

    ERROR
    TypeError: _Worker2.default is not a constructor

  5. iansu commented on May 23, 2018

    @iansu
    Contributor

    As I mentioned before, we can't provide support for ejected setups. There are too many unknowns and we have limited time to help. I'd suggest reading over the worker-loader docs and maybe trying to get this working in a fresh project first.

    I'm going to close this now as work on this feature is already underway.

  6. subhranshudas commented on May 23, 2018

    @subhranshudas
    Author

    Ok. thanks for the information.. the above error was from a fresh project...

  7. subhranshudas commented on May 23, 2018

    @subhranshudas
    Author

    @iansu one quick thing i noticed
    in your PR #3934

     require.resolve('thread-loader'),
    

    so when i eject CRA, in package.json i don't see this module. Is it needed to have your other changes work?

  8. iansu commented on May 23, 2018

    @iansu
    Contributor

    Yes, that module is required. You'll have to install it yourself since you've ejected.

  9. subhranshudas commented on May 24, 2018

    @subhranshudas
    Author

    @iansu and as of now in non-ejected CRA there is no way we can use web workers apart from react-app-rewired as explained in https://medium.com/@danilog1905/how-to-use-web-workers-with-react-create-app-and-not-ejecting-in-the-attempt-3718d2a1166b

    As per #1277 (comment) i don't see how we can ES6 import several modules which i want to process in the worker itself.

  10. iansu commented on May 24, 2018

    @iansu
    Contributor

    Yes, you can't currently use Web Workers in Create React App until #3934 is merged and released.

    I'm not really sure what you mean about importing modules into the worker. The worker files are run through babel-loader just like all the other JS so you should be able to do all the same things, including importing modules.

  11. subhranshudas commented on May 30, 2018

    @subhranshudas
    Author

    @iansu thanks for the reply, when is

    We are hoping to add support for Web Workers and worker-loader in react-scripts 2.0: #3934

    coming out. any expected timelines.

  12. locked and limited conversation to collaborators on Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions