-
-
Notifications
You must be signed in to change notification settings - Fork 520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(plugin-vite): package volume size to large #3336
fix(plugin-vite): package volume size to large #3336
Conversation
Is there any update on this? Saw a few people recently post about this issue in the Electron Discord server and pointed them here for reference. |
@caoxiemeihao Have you looked at the code I posted here (#3224 (comment))? I believe this is the type of solution you are looking for. Whatever node_modules vite has to externalize, we need to whitelist to include in the package. The tricky part is we then need to also include its nested dependencies. This is where I used the walker created by @MarshallOfSound called |
Why not use vite to build the dist directory and use the built html resources in the dist directory? Now the behavior of plugin-vite is to copy the entire source code (including the directory that node_modules depends on) to the out directory, and then perform the build operation again. My project built with Vite is only 108kb, but built with this plug-in it is nearly 700m. I don’t know what I did wrong. |
Hello! I hope you're doing well. Any chance of an update? We're eagerly waiting to release a new app with vite. Your contributions are greatly appreciated! Thank you! |
Ping @caoxiemeihao |
@caoxiemeihao Is there any update to this? |
I already raised this issue last year. #3071 (comment) I resolved by modifying the forge.config.ts file. const config: ForgeConfig = {
packagerConfig: {
ignore: (file) => {
if (!file) return false;
return (
!/^[/\\]package.json$/.test(file) &&
!/^[/\\]\.vite($|[/\\]).*$/.test(file)
);
},
},
} |
I have a similar solution but the problem is for a generic solution it is more complicated because if you have any native node libs (like Realm or SQLite etc) you have to add more logic to include them since they do not get bundled (external). And then you have to check their nested dependencies and also include them. I had to do quite a bit of extra logic to get it working for us and I think that is the complication here, that they need to come up with a way to generically handle this for all users. |
f7ba5d7
to
7560f15
Compare
I'm very sorry I've been busy with my work and I'm currently trying to fix it. |
7560f15
to
86c0745
Compare
86c0745
to
c67f0f6
Compare
e2f9bec
to
51f5aec
Compare
1f1a432
to
65eea3e
Compare
65eea3e
to
2f6602f
Compare
04253d4
to
0564e07
Compare
e48b7d3
to
c7da756
Compare
Awesome work! |
This is not yet released, right? I'm using |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@electron-forge/maker-base](https://togithub.com/electron/forge) | [`7.2.0` -> `7.3.0`](https://renovatebot.com/diffs/npm/@electron-forge%2fmaker-base/7.2.0/7.3.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@electron-forge%2fmaker-base/7.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@electron-forge%2fmaker-base/7.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@electron-forge%2fmaker-base/7.2.0/7.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@electron-forge%2fmaker-base/7.2.0/7.3.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@napi-rs/cli](https://togithub.com/napi-rs/napi-rs) | [`3.0.0-alpha.40` -> `3.0.0-alpha.41`](https://renovatebot.com/diffs/npm/@napi-rs%2fcli/3.0.0-alpha.40/3.0.0-alpha.41) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@napi-rs%2fcli/3.0.0-alpha.41?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@napi-rs%2fcli/3.0.0-alpha.41?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@napi-rs%2fcli/3.0.0-alpha.40/3.0.0-alpha.41?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@napi-rs%2fcli/3.0.0-alpha.40/3.0.0-alpha.41?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>electron/forge (@​electron-forge/maker-base)</summary> ### [`v7.3.0`](https://togithub.com/electron/forge/releases/tag/v7.3.0) [Compare Source](https://togithub.com/electron/forge/compare/v7.2.0...v7.3.0) ##### What's Changed ##### Features - feat(plugin-vite): upgrade to vite@5 by [@​caoxiemeihao](https://togithub.com/caoxiemeihao) in [https://github.com/electron/forge/pull/3468](https://togithub.com/electron/forge/pull/3468) - feat: allow a custom out dir from forge config by [@​lutzroeder](https://togithub.com/lutzroeder) in [https://github.com/electron/forge/pull/3458](https://togithub.com/electron/forge/pull/3458) - feat(template-vite): patch types by [@​caoxiemeihao](https://togithub.com/caoxiemeihao) in [https://github.com/electron/forge/pull/3494](https://togithub.com/electron/forge/pull/3494) - feat: adds default fuses to templates by [@​yangannyx](https://togithub.com/yangannyx) in [https://github.com/electron/forge/pull/3480](https://togithub.com/electron/forge/pull/3480) - feat(publisher-github): option to automatically generate release notes by [@​dsanders11](https://togithub.com/dsanders11) in [https://github.com/electron/forge/pull/3484](https://togithub.com/electron/forge/pull/3484) ##### Fixes - fix(electron-release-publisher): change api/version endpoint in PublisherERS to use versions/sorted by [@​kgallagher52](https://togithub.com/kgallagher52) in [https://github.com/electron/forge/pull/3431](https://togithub.com/electron/forge/pull/3431) - fix(core): packageJSON won't be found when programmatic usage instead of CLI by [@​ianho](https://togithub.com/ianho) in [https://github.com/electron/forge/pull/3455](https://togithub.com/electron/forge/pull/3455) - fix: actually depend on preceeding groups by [@​MarshallOfSound](https://togithub.com/MarshallOfSound) in [https://github.com/electron/forge/pull/3438](https://togithub.com/electron/forge/pull/3438) - fix: normalize windows version with build part correctly by [@​rickymohk](https://togithub.com/rickymohk) in [https://github.com/electron/forge/pull/3461](https://togithub.com/electron/forge/pull/3461) - fix: .vscode settings.json changes on open by [@​lutzroeder](https://togithub.com/lutzroeder) in [https://github.com/electron/forge/pull/3460](https://togithub.com/electron/forge/pull/3460) - fix(plugin-vite): package volume size to large by [@​caoxiemeihao](https://togithub.com/caoxiemeihao) in [https://github.com/electron/forge/pull/3336](https://togithub.com/electron/forge/pull/3336) ##### Performance - refactor: only run webpack once for multi-arch packages by [@​MarshallOfSound](https://togithub.com/MarshallOfSound) in [https://github.com/electron/forge/pull/3437](https://togithub.com/electron/forge/pull/3437) ##### Other Changes - chore: update Packager by [@​erikian](https://togithub.com/erikian) in [https://github.com/electron/forge/pull/3419](https://togithub.com/electron/forge/pull/3419) - chore: bump electronjs/node to 2.2.0 (main) by [@​electron-roller](https://togithub.com/electron-roller) in [https://github.com/electron/forge/pull/3469](https://togithub.com/electron/forge/pull/3469) - chore(plugins/electronegativity): correct some config types by [@​Dogdriip](https://togithub.com/Dogdriip) in [https://github.com/electron/forge/pull/3482](https://togithub.com/electron/forge/pull/3482) - chore: use Dependabot to update GitHub Actions deps by [@​dsanders11](https://togithub.com/dsanders11) in [https://github.com/electron/forge/pull/3487](https://togithub.com/electron/forge/pull/3487) - chore: bump electronjs/node to 2.2.1 (main) by [@​electron-roller](https://togithub.com/electron-roller) in [https://github.com/electron/forge/pull/3496](https://togithub.com/electron/forge/pull/3496) ##### New Contributors - [@​kgallagher52](https://togithub.com/kgallagher52) made their first contribution in [https://github.com/electron/forge/pull/3431](https://togithub.com/electron/forge/pull/3431) - [@​rickymohk](https://togithub.com/rickymohk) made their first contribution in [https://github.com/electron/forge/pull/3461](https://togithub.com/electron/forge/pull/3461) - [@​lutzroeder](https://togithub.com/lutzroeder) made their first contribution in [https://github.com/electron/forge/pull/3460](https://togithub.com/electron/forge/pull/3460) - [@​ianho](https://togithub.com/ianho) made their first contribution in [https://github.com/electron/forge/pull/3455](https://togithub.com/electron/forge/pull/3455) - [@​yangannyx](https://togithub.com/yangannyx) made their first contribution in [https://github.com/electron/forge/pull/3480](https://togithub.com/electron/forge/pull/3480) - [@​Dogdriip](https://togithub.com/Dogdriip) made their first contribution in [https://github.com/electron/forge/pull/3482](https://togithub.com/electron/forge/pull/3482) **Full Changelog**: electron/forge@v7.2.0...v7.3.0 </details> <details> <summary>napi-rs/napi-rs (@​napi-rs/cli)</summary> ### [`v3.0.0-alpha.41`](https://togithub.com/napi-rs/napi-rs/releases/tag/%40napi-rs/cli%403.0.0-alpha.41) [Compare Source](https://togithub.com/napi-rs/napi-rs/compare/@napi-rs/[email protected]...@napi-rs/[email protected]) ##### What's Changed - fix(cli): fallback to wasm32 if platform is not support by [@​Brooooooklyn](https://togithub.com/Brooooooklyn) in [https://github.com/napi-rs/napi-rs/pull/1967](https://togithub.com/napi-rs/napi-rs/pull/1967) - fix(cli): allow more platform & arch fallback to wasm by [@​Brooooooklyn](https://togithub.com/Brooooooklyn) in [https://github.com/napi-rs/napi-rs/pull/1969](https://togithub.com/napi-rs/napi-rs/pull/1969) **Full Changelog**: https://github.com/napi-rs/napi-rs/compare/[email protected]...[@​napi-rs/cli](https://togithub.com/napi-rs/cli)[@​3](https://togithub.com/3).0.0-alpha.41 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://togithub.com/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/toeverything/AFFiNE). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMDAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjIwMC4wIiwidGFyZ2V0QnJhbmNoIjoiY2FuYXJ5In0=-->
Looking back at this, isn't this just marking all dependencies from the package.json as "External" for vite and then copying all dependencies from package.json into the final package in the packageAfterCopy forge hook? So doesn't this still copy the entire node_modules folder and prevent doing any tree shaking with vite (since all packages are marked as external)? |
Only dependencies in |
@caoxiemeihao I am just here trying to understand and to help where I can (as seen by several of my comments above in this PR sharing some of my solutions I tried). If my last comment came off any differently I apologize... we are all on the same team 👍 As per your response. Dev dependencies don't build at all as they are part of the dev process. Vite doesn't bundle them unless you mark a true dependency as dev accidentally. It should only be stuff like forge, eslint, prettier, vite etc etc. Vite has the ability to mark packages as external primarily for things like native node addons which need to be packaged with the app (realm and SQLite in my case). But a majority of dependencies are supposed to be bundled with the rest of the code and tree shaken (if desired) leaving only those external packages needing to be packaged from node modules with the electron app. As an example, my build code goes from 250mb (with the current vite plugin from this PR which copies all node modules marked as dependency)... down to 29mb when I only include the external native node addons and allow all my other dependencies to be tree shaken and bundled by vite (which they already do anyways). @MarshallOfSound given you wrote flora-collosus maybe you could chime in here with your 2 cents and correct me if I am mistaken? Or can provide any guidance? Thanks! |
Here is my "hack" I applied to the forge-plugin-vite util/package.js file with a pnpm patch to override the current functionality so that it only packages my 4 external libs (along with all their nested dependencies). This ties to when I referenced in my last message going from 250mb down to 29mb... and to potentially use as a MVP / idea for implementing. My initial thought would be allow this to read from your vite config the libs you mark as External and only package those ones (as the rest are built/bundled by vite with your main.js etc.). In my vite config I also set these same 4 packages as External, as opposed to all Dependencies from package.json as the forge vite typescript template defaults to. Hope this helps. Patch name:
|
I'm finding the same thing by the way (on the latest forge version, 7.4.0). Vite bundles ~200mb of my node modules, which not only increases the total bundle size, but also increases packing time and extraction time. Using the webpack plugin, exactly 0 node modules are bundled. That's right, nothing. Why is vite so different here? Shouldn't it be tree shaking all these? |
Using the patch mentioned above, Vite is now properly bundling everything, including proper tree shaking. |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, I didn't discover |
That has nothing to do with this issue or the patch. My project is new and created with the newer template. Do you actually understand what the issue is here? |
This issue started happening randomly in my project. Setting |
"No modules" are bundled? or you mean just the external ones are not bundled? Because the external ones are not meant to be bundled in the .js file. |
The issue is happening due to my project using |
Summarize your changes:
Fixes:
#3224
#3298
#3293
#3377
#3420
At present, I still need to exactly include the third-party packages in
dependencies
in tonode_modules
, because they cannot be built normally be Vite, such asC/C++
addons.I haven't found the right logic to handle
node_modules
, I'm trying...