karolis-sh / electron-snowpack

Use Snowpack and esbuild for Electron app development
MIT License
50 stars 8 forks source link

Native Node Modules #44

Closed larssvensby closed 3 years ago

larssvensby commented 3 years ago

I'm using https://github.com/lovell/sharp-libvips in my project. When running electron-snowpack dev --verbose I get below error. How do I configure a loader for Native Node Modules?

Thanks!

[16:22:37] [@snowpack/plugin-react-refresh] transform() starting… [node_modules/sharp/build/Release/sharp.node] [16:22:37] [@snowpack/plugin-react-refresh] ✔ transform() success [node_modules/sharp/build/Release/sharp.node] [16:22:37] [esinstall:sharp] FAILURE: Error: Unexpected character '�' (Note that you need plugins to import files that are not JavaScript) [16:22:37] [esinstall:sharp] Failed to load ../build/Release/sharp.node Unexpected character '�' (Note that you need plugins to import files that are not JavaScript) [electron-snowpack] Error: Install failed. [electron-snowpack] 🚨 An unexpected error has occurred!

karolis-sh commented 3 years ago

As I don't have experience working with the native modules, could you provide a barebones example of how it should work? Like an https://github.com/electron-userland/electron-webpack/blob/master/docs/en/dependency-management.md#installing-native-node-modules example would help me to understand what needs to be achieved with Snowpakc.

larssvensby commented 3 years ago

I modified one of your examples https://github.com/larssvensby/electron-snowpack/tree/main/examples/hello-world-typescript Normally (unbundled/webpack) I use global.process.dlopen(module, sharpPath); to load sharp. I can't figure out how to do that in snowpack, I looked at this plugin https://github.com/Krutsch/snowpack-plugin-sharp but that only expose a small part of the sharp API.

As for the rebuilding part (https://github.com/electron-userland/electron-webpack/blob/master/docs/en/dependency-management.md#installing-native-node-modules) that isn't really an issue if I can get the module to load.

karolis-sh commented 3 years ago

The snowpack-plugin-sharp looks more like a loader, as I understand you want to use sharp at runtime right?

larssvensby commented 3 years ago

Yes, I want to use it at runtime. Thanks for looking into this!

karolis-sh commented 3 years ago

Here's an example https://github.com/karolis-sh/electron-snowpack/tree/main/examples/native-node-modules

Native Node modules are actually nothing special, it's just like using any other Node module. You have 2 options: