Closed larssvensby closed 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.
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.
The snowpack-plugin-sharp
looks more like a loader, as I understand you want to use sharp
at runtime right?
Yes, I want to use it at runtime. Thanks for looking into this!
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:
require
syntax (there's an issue with Snowpack if you want to use import
https://github.com/snowpackjs/snowpack/discussions/2469)preload
script (https://github.com/karolis-sh/electron-snowpack/blob/main/examples/electron-natives/src/main/preload.js, https://github.com/karolis-sh/electron-snowpack/blob/main/examples/electron-natives/src/main/index.js#L12)
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!