threads-plugin icon indicating copy to clipboard operation
threads-plugin copied to clipboard

Externalizing native dependencies doesn't seem to work with Electron

Open rathboma opened this issue 3 years ago • 5 comments

Firstly, :wave: @andywer! Thank you so much for making Threads and threads-plugin. It's great.

I maintain https://github.com/beekeeper-studio/beekeeper-studio which is an electron app and I'm trying to add a web worker that uses better-sqlite3.

I've enabled native module support in Electron, so it should work, but I'm getting this error, which seems to indicate that the better-sqlite3 module is not in the webpack externals list.

I'm providing it as an external to both the plugin, and to the main webpack config, so not sure if I'm doing something wrong (I probably am).

Here's a self contained project where I replicate the issue (see the console for the error): https://github.com/rathboma/bettersqlite3-electron-vue

Run with:

yarn install
yarn electron:start

rathboma avatar May 16 '21 02:05 rathboma

I can confirm that external config is not working:

  externals: {
    '@tiddlygit/tiddlywiki': '@tiddlygit/tiddlywiki',
  },
  externalsType: 'commonjs',
  externalsPresets: { electronMain: true },

Seems this plugin is not reading the external config.

linonetwo avatar May 30 '21 13:05 linonetwo

Webpack external plugin only works in webpack4 https://github.com/tiddly-gittly/TiddlyGit-Desktop/blob/8759bdf433b9e87972d3e12c991743a4185a3b8d/webpack.plugins.js#L34-L41

For webpack5, there is no way to external things from worker.

linonetwo avatar May 30 '21 13:05 linonetwo

I've created https://github.com/linonetwo/webpack5-externals-plugin

You can do

  new ExternalsPlugin({
    type: 'commonjs',
    include: path.join(__dirname, 'node_modules', '@tiddlygit', 'tiddlywiki'),
  }),
  new ThreadsPlugin({
    target: 'electron-node-worker',
    plugins: ['ExternalsPlugin'],
  }),

To external things.

This issue canbe closed now.

linonetwo avatar May 31 '21 17:05 linonetwo

I had this work with a node worker, but it does not work for a we worker with node integration enabled unfortunately

rathboma avatar Jun 07 '21 01:06 rathboma

My worker has node integration too, you can just external all node-related things, and import them as usual in the code.

linonetwo avatar Jun 07 '21 03:06 linonetwo