andywer / threads-plugin

📦 Makes your threads.js code build with webpack out of the box.
Apache License 2.0
28 stars 6 forks source link

Vue.js + Electron + Typescript unable to import native modules #39

Open benoitlahoz opened 3 years ago

benoitlahoz commented 3 years ago

Hello and thank you for the great work!

I'm building an app in Typescript with Electron (electron-builder) and Vue.js

The plugin seems to be ok, as it bundles my worker correctly.

Then, in the worker, I try to import 'is-online' module, that works great in the main thread but gives a Can't resolve 'dgram' in... in the worker (so... error happens when the module imports a submodule), and tried 'is-reachable' module giving this time Can't resolve 'dns'.

When importing directly the 'dns' module I have the same error.

Module build failed (from ./node_modules/threads-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve 'dns' in '/Users/benoitlahoz/Documents/oz/mmmap/src/core/connectivity'
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/webpack/lib/Compilation.js:925:10
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/webpack/lib/NormalModuleFactory.js:401:22
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/webpack/lib/NormalModuleFactory.js:130:21
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/webpack/lib/NormalModuleFactory.js:224:22
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/neo-async/async.js:2830:7
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/neo-async/async.js:6877:13
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/webpack/lib/NormalModuleFactory.js:214:25
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/Resolver.js:213:14
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/Users/benoitlahoz/Documents/oz/mmmap/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:44:7
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/Users/benoitlahoz/Documents/oz/mmmap/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/Users/benoitlahoz/Documents/oz/mmmap/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
    at /Users/benoitlahoz/Documents/oz/mmmap/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:67:43

FYI, nodeIntegration is set to false in Electron, sandbox to true, contextIsolation to true. But, I thought that, as I'm running the worker from the main process, it wouldn't be an issue.

I've read about babel '@preset/env' configuration, but for the time being it doesn't change anything. Am I missing something?

Thank you!

rathboma commented 3 years ago

I'm having a similar issue and have made a sample project to replicate it using better-sqlite3:

https://github.com/rathboma/bettersqlite3-electron-vue

I believe I need to add better-sqlite3 to the webpack externals for the threads build. Is there a way to tell if it's doing that, or a way to force it?