Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron - Cannot bundle worker code #20

Closed
alfredalfie123 opened this issue Apr 29, 2020 · 7 comments
Closed

Electron - Cannot bundle worker code #20

alfredalfie123 opened this issue Apr 29, 2020 · 7 comments
Labels
question Further information is requested

Comments

@alfredalfie123
Copy link

Hi!

I am building an electron application using electron-react-boilerplate.
In this boilerplate, I am trying to use threadsjs.
When testing in dev mode everything works great, but unfortunately when trying to pack the app, I am getting the following warning:

[0] WARNING in ./node_modules/electron-debug/index.js 96:45-58
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./app/main.dev.js
[0]
[0] WARNING in ./node_modules/electron-debug/index.js 97:61-74
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./app/main.dev.js
[0]
[0] WARNING in ./node_modules/threads/dist-esm/master/implementation.node.js 57:10-99
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./node_modules/threads/dist-esm/master/implementation.js
[0]  @ ./node_modules/threads/dist-esm/master/index.js
[0]  @ ./node_modules/threads/dist-esm/index.js
[0]  @ ./app/main_handler/main_handler.js
[0]  @ ./app/main.dev.js
[0]
[0] WARNING in ./node_modules/threads/dist-esm/master/implementation.node.js
[0] Module not found: Error: Can't resolve 'ts-node' in 'C:\Users\NC PC\Workspace\skuns\node_modules\threads\dist-esm\master'
[0]  @ ./node_modules/threads/dist-esm/master/implementation.node.js
[0]  @ ./node_modules/threads/dist-esm/master/implementation.js
[0]  @ ./node_modules/threads/dist-esm/master/index.js
[0]  @ ./node_modules/threads/dist-esm/index.js
[0]  @ ./app/main_handler/main_handler.js
[0]  @ ./app/main.dev.js
[0]
[0] WARNING in No instantiations of threads.js workers found.
[0] Please check that:
[0]   1. You have configured Babel / TypeScript to not transpile ES modules
[0]   2. You import `Worker` from `threads` where you use it
[0]
[0] For more details see: https://github.com/andywer/threads-plugin

In webpack.config.main.prod.babel.js, I follow your guide to use the plugins:

const ThreadsPlugin = require('threads-plugin');
...
plugins: [
    new ThreadsPlugin({
      target: 'electron-node-worker'
    }),
   ...
]

Could you please help me to solve this issue?

Best regards.

@andywer andywer added the question Further information is requested label Apr 29, 2020
@andywer
Copy link
Owner

andywer commented Apr 29, 2020

Hey @alfredalfie123!
I think there is a similar issue on the threads.js repo right now: andywer/threads.js#238

Can you share the code where you spawn the worker?

@alfredalfie123
Copy link
Author

Hi @andywer, after trying with latest boilerplate code, It show another warning when building:

0] WARNING in ./node_modules/electron-debug/index.js 96:45-58
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./app/main.dev.ts
[0] 
[0] WARNING in ./node_modules/electron-debug/index.js 97:61-74
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./app/main.dev.ts
[0] 
[0] WARNING in ./node_modules/threads/dist-esm/master/implementation.node.js 57:10-99
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./node_modules/threads/dist-esm/master/implementation.js
[0]  @ ./node_modules/threads/dist-esm/master/index.js
[0]  @ ./node_modules/threads/dist-esm/index.js
[0]  @ ./app/main.dev.ts
[0] 
[0] WARNING in ./node_modules/threads/dist-esm/master/implementation.node.js
[0] Module not found: Error: Can't resolve 'ts-node' in '/home/alfred/workspace/test_worker/node_modules/threads/dist-esm/master'
[0]  @ ./node_modules/threads/dist-esm/master/implementation.node.js
[0]  @ ./node_modules/threads/dist-esm/master/implementation.js
[0]  @ ./node_modules/threads/dist-esm/master/index.js
[0]  @ ./node_modules/threads/dist-esm/index.js
[0]  @ ./app/main.dev.ts

When building finish, I try to open the release file, and then I found out this error:

Require stack:
- /tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:717:15)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (electron/js2c/browser_init.js:7648:16)
    at Function.resolve (internal/modules/cjs/helpers.js:74:19)
    at resolveScriptPath (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:544068)
    at new Worker (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:544514)
    at IpcMain.<anonymous> (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:1889)
    at IpcMain.emit (events.js:203:13)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:3850:15)
    at WebContents.emit (events.js:203:13)
(node:25978) UnhandledPromiseRejectionWarning: Error: Cannot find module '/tmp/.mount_ElectrctMdOm/resources/app.asar/app/0.main.prod.worker.js'
Require stack:
- /tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:717:15)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (electron/js2c/browser_init.js:7648:16)
    at Function.resolve (internal/modules/cjs/helpers.js:74:19)
    at resolveScriptPath (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:544068)
    at new Worker (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:544514)
    at IpcMain.<anonymous> (/tmp/.mount_ElectrctMdOm/resources/app.asar/main.prod.js:2:1889)
    at IpcMain.emit (events.js:203:13)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:3850:15)
    at WebContents.emit (events.js:203:13)

Here is the repo which reproduce above one: https://github.com/alfredalfie123/test_worker

@andywer
Copy link
Owner

andywer commented Apr 29, 2020

I think I know what's going on: threads.js checks if ts-node is available at one point, using a try { require.resolve("ts-node") } catch { /*...*/ }.

Now webpack chokes on the require.resolve("ts-node"), I think.

Can you try to monkey-patch the implementation.node.js file and change require.resolve("ts-node") to eval('require.resolve("ts-node")')? Alternatively you could try adding ts-node to the externals setting of your webpack config.

If that works we can try to fix it permanently in threads.js.

@alfredalfie123
Copy link
Author

alfredalfie123 commented Apr 29, 2020

@andywer After patching implementation.node.js, It still show warning like this when building:

[0] WARNING in ./node_modules/threads/dist-esm/master/implementation.node.js 58:10-99
[0] Critical dependency: the request of a dependency is an expression
[0]  @ ./node_modules/threads/dist-esm/master/implementation.js
[0]  @ ./node_modules/threads/dist-esm/master/index.js
[0]  @ ./node_modules/threads/dist-esm/index.js
[0]  @ ./app/main.dev.ts

And when running release, it show error like this:

(node:30615) UnhandledPromiseRejectionWarning: Error: Cannot find module '/tmp/.mount_ElectrU1rTT5/resources/app.asar/app/0.main.prod.worker.js'
Require stack:
- /tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:717:15)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (electron/js2c/browser_init.js:7648:16)
    at Function.resolve (internal/modules/cjs/helpers.js:74:19)
    at resolveScriptPath (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:544053)
    at new Worker (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:544499)
    at IpcMain.<anonymous> (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:1889)
    at IpcMain.emit (events.js:203:13)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:3850:15)
    at WebContents.emit (events.js:203:13)
(node:30615) UnhandledPromiseRejectionWarning: Error: Cannot find module '/tmp/.mount_ElectrU1rTT5/resources/app.asar/app/0.main.prod.worker.js'
Require stack:
- /tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:717:15)
    at Function../lib/common/reset-search-paths.ts.Module._resolveFilename (electron/js2c/browser_init.js:7648:16)
    at Function.resolve (internal/modules/cjs/helpers.js:74:19)
    at resolveScriptPath (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:544053)
    at new Worker (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:544499)
    at IpcMain.<anonymous> (/tmp/.mount_ElectrU1rTT5/resources/app.asar/main.prod.js:2:1889)
    at IpcMain.emit (events.js:203:13)
    at WebContents.<anonymous> (electron/js2c/browser_init.js:3850:15)
    at WebContents.emit (events.js:203:13)

@andywer
Copy link
Owner

andywer commented May 18, 2020

Sorry for the late response! I hope you haven’t given up yet.

Did you configure your Electron build pipeline as documented here: https://threads.js.org/getting-started#electron?

@andywer
Copy link
Owner

andywer commented Aug 17, 2020

Any updates on this, @alfredalfie123?

@alfredalfie123
Copy link
Author

@andywer sorry for late reply, after bundlinlg threadjs to asar it works perfectly. Thank you for great library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants