Nativescript-cli: [AOT] multiple workers won't compile using aot

Created on 11 Feb 2020  路  3Comments  路  Source: NativeScript/nativescript-cli

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

bug

Most helpful comment

Fixed in nativescript-dev-webpack rc version, should be released in 1.5.1

All 3 comments

Hey @trodellez ,
Thanks for the detailed report and the sample project, this helps a lot in the investigation.
It looks like the problem is caused by the simultaneous webpack child compilations of the two TypeScript workers - the shared Angular Compiler plugin instance (passed in the webpack config to the plugin) is executed simultaneously for both of them and its internal state set by the first compilation breaks the second one.
We've implemented a fix in the worker-loader to wait each compilation to finishes its execution before starting the next one, which should resolve the issue.

Fixed in nativescript-dev-webpack rc version, should be released in 1.5.1

Thanks @rosen-vladimirov, I have also tested with 1.5.1 and it gets compiled and works properly, looking forward to have this fix in the stable release.

Was this page helpful?
0 / 5 - 0 ratings