Angular with Web Workers: Step by step
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.
- Run
ng eject
so Angular CLI generates the webpack.config.js file. - 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
- Create a new file src/workerLoader.ts
- 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. - Import
platformWorkerAppDynamic
- Import the
AppModule
(remove the import from main.ts) and bootstrap it using thisplatformWorkerAppDynamic
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:
- Add a
webworker
entry point for ourworkerLoader.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 usedwebworker.bundle.js
- Go to HtmlWebpackPlugin and exclude the
webworker
entry point, so the generated Web Worker file is not included in the index.html file. - Go to CommonChunksPlugin, and for the
inline
common chunk, set the entry chunks explicitely to preventwebworker
to be included. - Go to AotPlugin and set explicitely the
entryModule
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 ;)