Inconsistency in externalizing dependencies between v0.10.2 and v0.10.3
Vages opened this issue · 3 comments
Hi! Thank you for making the plugin. Would not be able to use Vite for my Electron project without it.
Problem
Upgrading from v0.10.2 to v0.15.5, the contents of dist-electron
changed unexpectedly. I pinpointed the change to v0.10.2, and have created a minimal example to demonstrate it.
- With 0.10.2,
chalk
is externalized because it is in dependencies: Vages/vite-plugin-electron-dependencies@46e5749#diff-3c248a1f76d3f4ef3b5a2a57cd5aa6da56a705a28bf5bf7190c93ddc40c3f2f2 - With 0.10.3,
chalk
dependency is inlined, making the app crash: Vages/vite-plugin-electron-dependencies@2c4987f#diff-3c248a1f76d3f4ef3b5a2a57cd5aa6da56a705a28bf5bf7190c93ddc40c3f2f2 - I have been able to fix the problem by adding my package.json
dependencies
tovite.build.rollupOptions.external
: Vages/vite-plugin-electron-dependencies@da3a5f4
I have found that this is due to the following code change:
- In 0.10.2, the
dependencies
listed inpackage.json
are automatically included inbuild.rollupOptions.external
in the functioncreateWithExternal
: https://github.com/electron-vite/vite-plugin-electron/blob/v0.10.2/packages/electron/src/config.ts#L41-L42 - In 0.10.3,
createWithExternal
is superseded bywithExternalBuiltins
, which does not includedependencies
build.rollupOptions.external
: https://github.com/electron-vite/vite-plugin-electron/blob/v0.10.3/src/config.ts#L43-L70
The fact that the README changed between 0.10.2 and 0.10.3 makes me suspect that the change was intentional:
- https://github.com/electron-vite/vite-plugin-electron/tree/v0.10.2/packages/electron#put-nodejs-packages-in-dependencies
- https://github.com/electron-vite/vite-plugin-electron/tree/v0.10.3#be-aware
Suggested fixes
I’d be willing to help by clarifying the README – as I suspect the change was intentional. I would like the sentence that currently says “So, put your Node.js package in dependencies.” to say something like this:
In general, Vite may not correctly build Node.js packages, especially C/C++ native modules, but Vite can load them as external packages.
1. Add the names of any dependencies that should be externalized to the plugin configuration’s `vite.build.rollupOptions.external` field
2. Make sure that they are available to your Electron application at runtime. Electron build tools such as `electron-builder` and `electron-forge` will by default only make a dependency available by default if it is listed under `dependencies` in `package.json`[^1]
[^1]: This behavior is documented by the following sentence in the @electron/packager README: “Be careful not to include `node_modules` you don't want into your final app. If you put them in the `devDependencies` section of `package.json`, by default none of the modules related to those dependencies will be copied in the app bundles.” See https://github.com/electron/packager/tree/d829dbef09b4c106f59ea545254fef498ecc6731#from-the-command-line
If the change was unintentional, I would like to contribute code which makes the plugin behave as in v0.10.2.
Why I am confused by the README
In its current state, the README misleads me to think that it’s enough to put the dependencies you want to externalize in dependencies
:
- The instruction to “put your Node.js package in
dependencies
” makes me believe that the plugin will externalize the dependencies listed independencies
. - The code example makes me believe the opposite by showing the packages listed in
vite.build.rollupOptions.external
.
In general, Vite may not correctly build Node.js packages, especially C/C++ native modules, but Vite can load them as external packages
So, put your Node.js package in
dependencies
. Unless you know how to properly build them with Viteexport default { plugins: [ electron({ entry: "electron/main.ts", vite: { build: { rollupOptions: { // Here are some C/C++ modules them can't be built properly external: ["serialport", "sqlite3"], }, }, }, }), ], };
@Vages Thank you! Your workaround helped me to fix this issue.
Also, I wonder if this project is dead :/
@Andrew-web-coder If you think that way, you should use another project instead.
Thanks for fixing, @caoxiemeihao!
I do not think @Andrew-web-coder intended to make a judgment on the quality of your work, so don’t lose heart. I like it a lot 😄