Angular with Web Workers: Step by step

Enrique oriol
Angular In Depth
Published in
2 min readApr 6, 2017

This article describes, step by step, how to modify an existing Angular CLI generated app so it can take advantage of the Angular WebWorkers and run all the business logic inside an isolated thread.

Preconditions

I’m assuming that you have an Angular project (version 2 or 4) generated with Angular CLI 1.0 or higher. If that’s not the case and you want to practice the content of this article, check this sample repo.

Extract webpack file

Since Angular CLI v1.0, there’s the “eject” feature, that allows you to extract the webpack config file and manipulate it as you wish.

  1. Run ng eject so Angular CLI generates the webpack.config.js file.
  2. Run npm install so the new dependencies generated by CLI are satisfied

Install webworker bootstrap dependencies

Run npm install --save @angular/platform-webworker @angular/platform-webworker-dynamic

Changes in UI thread bootstrap

Changes in app.module.ts

Replace BrowserModule by WorkerAppModule in the app.module.ts file. You’ll also need to update the import statement in order to use @angular/platform-webworker library.

Changes in src/main.ts

Replace bootstrap process with: bootstrapWorkerUI (update also the import).

You’ll need to pass a URL with the file where the web worker is defined. Use a file called webworker.bundle.js, don’t worry, we will create this file soon.

Create workerLoader.ts file

  1. Create a new file src/workerLoader.ts
  2. As your Web Worker will be a single file containing all the required stuff, you need to include polyfills.ts, @angular/core, and @angular/common packages. On next steps, you will update Webpack in order to transpile and build a bundle with the result.
  3. Import platformWorkerAppDynamic
  4. Import the AppModule (remove the import from main.ts) and bootstrap it using this platformWorkerAppDynamic platform.

Update webpack to build your webworker

The webpack auto generated config file is quite long, but you’ll just need to center your attention in the following things:

  1. Add a webworkerentry point for our workerLoader.ts file. If you look at the output, you’ll see that it attaches a bundle.js prefix to all chunks. That’s why during bootstrap step we have used webworker.bundle.js
  2. Go to HtmlWebpackPlugin and exclude the webworker entry point, so the generated Web Worker file is not included in the index.html file.
  3. Go to CommonChunksPlugin, and for the inline common chunk, set the entry chunks explicitely to prevent webworker to be included.
  4. Go to AotPlugin and set explicitely the entryModule
Parts of the webpack.config.js file you have to check

You’re ready

If you have followed correctly the previous steps, now you only need to compile the code and try the results.

Run npm start

All the logic of your Angular app should be running inside a WebWorker, causing the UI to be more fluent.

Furter notes

npm start runs the webpack-dev server, and it has some kind of problem with webworkers throwing an error message on console log. Anyway, the webworker seems to run fine.

If you compile the app using webpack command and serve it from any http server like simplehttpserver, the error goes away ;)

That’s all folks!

If you have enjoyed this post, please share it ;)

--

--

Enrique oriol
Angular In Depth

Frontend Team Lead at Dynatrace. Angular & Ionic evangelist. Father, lover, blogger, Udemy trainer. — blog.enriqueoriol.com