I am trying to do web-worker setup in my existing Angular 7.0.1 project (medium scale project). I did the setup after going through the following links:
- Web workers setup for Angular 4 app
- Webpack config change for Angular 6+ app - library and related article
Here are my changes in each file:
./src/main.ts
import 'zone.js';
import { enableProdMode } from '@angular/core';
import { bootstrapWorkerUi } from '@angular/platform-webworker';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
console.log("main.ts file loaded!");
bootstrapWorkerUi('webworker.js');
./src/workerLoader.ts
import 'polyfills.ts';
import '@angular/core';
import '@angular/common';
console.log("workerLoader.ts file loaded!");
import { platformWorkerAppDynamic } from '@angular/platform-webworker-dynamic';
import { AppModule } from './app/app.module';
platformWorkerAppDynamic().bootstrapModule(AppModule);
./angular.json
"projects": {
"[project name]": {
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./extra-webpack.config.js"
}
...
...
}
}
}
}
}
./extra-webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
"entry": {
"webworker": [
"./src/workerLoader.ts"
]
},
"plugins": [
new HtmlWebpackPlugin({
"excludeChunks": [
"webworker"
],
})
],
"output": {
"globalObject": 'this'
}
}
./tsconfig.json
{
...
...
"angularCompilerOptions": {
"entryModule": "./app/app.module#AppModule"
}
}
Here is the demo. You might want to check my research notes.
I might be going in completely wrong direction because I just need to include and run a angular service worker library in Angular project (which should be super easy).
My main intention of trying to include webworker is to let the web app run on multi-thread so that the animations, checkbox, custom dropdown and scroll effects on the screen performs smoothly.