NativeScript / worker-loader

36 stars 20 forks source link

Problem building with worker loader #2

Closed romulowspp closed 7 years ago

romulowspp commented 7 years ago

I try to use this plugin and with webpack i get this error:

94% asset optimization/home/ablankzin/mobile/node_modules/worker-loader/NativeScriptWorkerPlugin.js:12
                .map(chunk => resolve(output, chunk));
                ^

TypeError: Cannot read property 'map' of undefined

My package.json:

  "webpack": "~3.2.0",
    "webpack-bundle-analyzer": "^2.8.2",
    "webpack-sources": "~1.0.1",
    "nativescript-dev-webpack": "^0.7.3",
    "copy-webpack-plugin": "~4.0.1",
    "worker-loader": "github:nativescript/worker-loader"

Can you help me? :dancing_men:

sis0k0 commented 7 years ago

@Ablankzin,

Can you share some details about your project? How many worker scripts do you have? Are you providing them to the worker loader (var MyWorker = require("worker-loader!./file.js"))? Thanks!

Please, note that the plugin is still under development.

romulowspp commented 7 years ago

@sis0k0,

hello. Let me show a piece of my code:

export class ConversationComponent implements OnDestroy, OnInit {
           worker: Worker = new Worker("worker-loader!./../../services/worker/conversation.worker");

i'm using nativescript with angular. When i run without webpack it's works.

Looking into code, i see that:

compilation.workerChunks

Is undefined. I don't know why.

;)

sis0k0 commented 7 years ago

It must be that the loader cannot find the conversation.worker. Can you transpile that file manually to js and then change the require to new Worker("worker-loader!./../../services/worker/conversation.worker.js");?

romulowspp commented 7 years ago

I transpile and use .js and same error happen.

romulowspp commented 7 years ago

Update.

Now with your update i receive this error in webpack process.

ERROR in ../node_modules/nativescript-dev-webpack/tns-aot-loader.js!../node_modules/@ngtools/webpack/src!./modules/main/services/worker/conversation.worker.ts
    Module build failed: Error: @ngtools/webpack is being used as a loader but no `tsConfigPath` option nor AotPlugin was detected. You must provide at least one of these.
        at Object.ngcLoader (/mobile/node_modules/@ngtools/webpack/src/loader.js:434:19)

I change the code to:


import * as MyWorker from "worker-loader!./../../services/worker/conversation.worker";

worker: Worker = new MyWorker();

:)

sis0k0 commented 7 years ago

Hey @Ablankzin,

I will write later today with the solution and will add a demo application showing the approach. Thanks for your patience! :)

sis0k0 commented 7 years ago

@Ablankzin,

The problem is caused by the @ngtools/webpack loader which is used to transpile the TS files to JS. We need to provide a typescript configuration to the @ngtools/webpack loader, because the one it uses by default - the one from the AotPlugin - is no longer available. When the worker script is ran through the loader, the AoT plugin instance is no longer part of the compilation object. In short, we need to provide the same path for a tsconfig.json to the loader, that we are using for the plugin. Check out the modified webpack.config.js: Options for Angular's webpack plugin and loader: https://github.com/NativeScript/worker-loader/blob/165ba7038dd455bd0ddd466eeb6d1c3ed4789a42/demo-angular/webpack.config.js#L13

TypeScript loaders: https://github.com/NativeScript/worker-loader/blob/165ba7038dd455bd0ddd466eeb6d1c3ed4789a42/demo-angular/webpack.config.js#L135-L144

I've made a simple demo app in the repo, that you can refer to if you have any problems. Of course, don't hesitate to ask if something is unclear :)

romulowspp commented 7 years ago

I've tested in my project and works! Thank you!

But only work with next release of runtime. I will wait next release and watch this awesome project.

sis0k0 commented 7 years ago

Yup, it will be part of the 3.2 release of the Android runtime which is coming in a few days :)

markncsu commented 6 years ago

I just stumbled upon this thread in search of a solution for the same "Error: @ngtools/webpack is being used as a loader but no tsConfigPath option nor AotPlugin was detected." using the worker-loader plugin. Passing the tsConfigPath to the loader as you suggested worked!

Thanks so much!

andipahlevy commented 6 years ago

var MyWorker = require("worker-loader!./file.js")

@sis0k0 where the file.js should place? ini node_modul/nativescript-worker-loader ? or in src ?