angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

How to work with web workers (use a web worker IN cli project)

Open jbgarr opened this issue 9 years ago • 72 comments

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [X] feature request

Versions.

@angular/cli: 1.0.0 node: 6.9.1 os: darwin x64 (macOS Sierra 10.12.3)

Is there a way to import and use a web worker in a component using angular-cli? I'm trying to incorporate a web worker that I wrote into my project, but I'm not having any luck. The web worker worked fine in my project before attempting to convert it to use the CLI (previous version was based on System.js). I'm trying to import my web worker code like this:

import * as MyWorker from 'assets/workers/dropdown.worker.js';

and then I'm trying to create my actual worker instance like this:

let blob = new Blob(['(this.onmessage=', MyWorker.toString(), ')'], { type: "text/javascript" });
let dropdownWorker = new Worker((<any>window).URL.createObjectURL(blob));

The above does not work though, and if I log MyWorker right after I try to import it I see it's just an empty object instead of a function (which is what it is in my other app). Any help on this would be much appreciated!

UPDATE

I just want to clarify what I'm trying to do: I want to use an existing web worker IN my Angular app, not run my whole app IN a web worker. I know there is separate issue already that relates to the trying to run an entire app in a web worker. These are similar, but very different issues.

jbgarr avatar Apr 07 '17 23:04 jbgarr

Exactly. When Angular will replace webpack for another tool, the CLI will still support it by builders like @angular-devkit/build-webpack.

dirkluijk avatar Oct 18 '18 23:10 dirkluijk

@michaeljota @dirkluijk That's a fair point. Maybe I misinterpreted the comment and jumped the gun.

However, I like the solution that uses worker-loader because of output hashing. The worker.js file is also hashed and I don't need to worry about manual cache-busting. With ng-packagr, I can no longer use worker-loader to load my worker scripts.

Are you aware of any automated way that I could include a output hash in the worker.js file while importing it using the new Worker('hash.worker.js') approach? Any pointers?

Thanks.

kiranjholla avatar Oct 19 '18 02:10 kiranjholla

@kiranjholla

I already asked this question few comments above... https://github.com/angular/angular-cli/issues/5885#issuecomment-428346967

Also tried on StackOverflow and angular/cli but without luck.

That said, considering that from what I am understanding you have 2 builds, there would be no way to reference the hashed file in the previous file. https://github.com/angular/angular-cli/issues/12582

In case you find a solution don't hesitate to give feedback here

mboughaba avatar Oct 19 '18 08:10 mboughaba

@kiranjholla @mboughaba Have you tried https://github.com/angular/angular-cli/issues/5885#issuecomment-295153316?

shlangus avatar Oct 19 '18 21:10 shlangus

@shlangus Thanks very much for the suggestion! I managed to get it working using your method This is how I require my file

const StorageWorker: any = require('file-loader?name=storage.[hash:20].[ext]!../../../../dist/storage.js');
// [...]
// ctor
   this.worker = new Worker(StorageWorker);

On the config side, I have the following

const config = {
  entry: {
    storage: './src/worker/storage.ts'
  },
  mode: 'production',
  watch: false,
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'awesome-typescript-loader',
        query: {
          configFileName: './src/tsconfig.worker.json'
        }
      }
    ]
  },
  output: {
    path: path.resolve(__dirname, 'dist-worker'),
    filename: '[name].[contenthash].js'
  }
};

There is a lot of magic, but hey it works!!

I spoke too fast 😞 ... This was loading the storage.js which was left over in the dist folder... As soon as I run the prod build which uses CleanWebpackPlugin.

mboughaba avatar Oct 20 '18 08:10 mboughaba

You might be interested in using inline web workers to avoid the overhead of having to create a separate Webpack configuration with Typescript etc.: https://www.html5rocks.com/en/tutorials/workers/basics/#toc-inlineworkers

julkue avatar Nov 09 '18 17:11 julkue

It could be great to create a clean repository with a full working example of an Angular 7 app with Lazy-Loading, running entirely in a web-worker.

mhosman avatar Nov 15 '18 15:11 mhosman

@mhosman This issue is about using web workers IN an Angular app. Not running Angular itself in a worker.

For the latter, see https://legacy-to-the-edge.com/2018/05/01/platform-webworker-in-angular/ (I just googled for it)

dirkluijk avatar Nov 15 '18 15:11 dirkluijk

Thanks @dirkluijk, my problem is that when I'm loading modules, loaders and the UI itself is stucked for a couple of seconds. I need a way to run the logic in the background in order to get the animations only working in the main thread.

mhosman avatar Nov 15 '18 15:11 mhosman

There's ongoing work to add https://github.com/googlechromelabs/worker-plugin in https://github.com/angular/angular-cli/pull/12575.

filipesilva avatar Jan 28 '19 15:01 filipesilva

One of the problems is when you load modules and material2 animations get stuck.

mhosman avatar Jan 28 '19 16:01 mhosman

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.