Search code examples
nativescriptnativescript-plugin

Problem with using worker in nativescript plugin


I created my first plugin: https://www.npmjs.com/package/nativescript-ftp-client And it is working when in development mode (using seed project), But when I package it app says it cannot find a ftp-worker-android.js file which is included Problem is that I am using new Worker('./ftp-worker-android.js'); and it gives an error. If I try to include it with import at the beginning of a file it get's included so I know that it is in the package but worker can't seem to load, it gives following error: JS: [Error: com.tns.NativeScriptException: Failed to find module: "./ftp-worker-android.js", relative to: app//


Solution

  • You have to conditionally use nativescript-worker-loader plugin to initialise worker on webpack builds.

        var worker;
        if (global.TNS_WEBPACK) {
            var GrayscaleWorker = require('nativescript-worker-loader!./ftp-worker-android.js');
            worker = new GrayscaleWorker();
        } else {
            worker = new Worker('./ftp-worker-android.js');
        }