Description
While working with workers in nativescript we stuck with an error when there is more than one worker defined and being used. There is no issue while compiling with JIT but by adding the AOT flag the project won't compile and following error raises
Child worker:
Asset Size Chunks Chunk Names
0adb156fd1eb2394a395.worker.js 4.21 MiB main [emitted] main
Entrypoint main = 0adb156fd1eb2394a395.worker.js
[../node_modules/ts-loader/index.js!./workers/second.worker.ts] ../node_modules/ts-loader!./workers/second.worker.ts 430 bytes {main} [built]
[./package.json] 139 bytes {main} [optional] [built]
[./shared.ts] 130 bytes {main} [built]
+ 252 hidden modules
Child worker:
Asset Size Chunks Chunk Names
5caf84999f4cdd72d8dd.worker.js 4.21 MiB main [emitted] main
Entrypoint main = 5caf84999f4cdd72d8dd.worker.js
[../node_modules/ts-loader/index.js!./workers/typescript.worker.ts] ../node_modules/ts-loader!./workers/typescript.worker.ts 416 bytes {main} [built]
[./package.json] 139 bytes {main} [optional] [built]
[./shared.ts] 130 bytes {main} [built]
+ 252 hidden modules
ERROR in Internal Error: already initialized!
Webpack compilation complete.
Executing webpack failed with exit code 2.
so this issue happens if there is more than one javascript or typescript worker defined.
Sample project
I have modified the official webworker sample ( https://github.com/NativeScript/worker-loader/tree/master/demo-angular ) and just added a second typescript worker. the sample source code can be found at https://github.com/trodellez/twoworker
so basically I have added a second ts worker
import "tns-core-modules/globals";
import { sharedFunction } from "../shared";
const context: Worker = self as any;
context.onmessage = msg => {
sharedFunction("worker");
setTimeout(() => {
console.log("Inside Second TS worker...");
console.log(msg);
(<any>global).postMessage("Second TS Worker");
}, 500)
};
and added a method to worker.service.ts to initialize it
initSecondTsWorker() {
if (this.secondTsWorker) {
return this.secondTsWorker;
}
// add if building with webpack
this.secondTsWorker = new SecondTsWorker();
return this.secondTsWorker;
}
then the project won't compile anymore with aot flag
tns build android --env.aot
any help would be really appreciated