fix(plugin-vite): Don't copy node_modules on build #3579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summarize your changes:
This resolves the ongoing issues with the Vite plugin (as well as fixes #3570). The plugin right now copies all dependencies, all the time, regardless of config. This is absolutely insane as it completely bypasses Vite's normal bundling and tree-shaking process in favor of copying all node_modules and requiring them (via registering them as external), instead of inlining the tree-shaken code. This means you can get something like my case, where ~200mb of uncompressed, uncompiled node modules were being copied, slowing down my build time, .zip extract time, and dramatically increasing my bundle size.
This PR fixes that issue by simply removing the offending code. It is not needed. Anyone who needs to copy a specific node module for whatever reason can use something like this Vite plugin to simply copy their node module manually. However, for 99.9% of use cases, this will be unnecessary.
This also brings the Vite plugin more in line to how the webpack one works (which also does not copy any node modules by default)
There is one issue with this though: any import or require that is not top-level does not get inlined, and must either be moved to the top-level or have its node_modules copied. This appears to just be standard Vite behavior and is different from webpack. It is worth noting that because this, this PR actually does break electron-squirrel-startup imports, assuming a user is following the recommended guide for its usage, since that does a require inside the condition of an if statement. This can simply just be moved to the top to avoid this problem (I've tested it, it works fine), but it is important to note that it would technically be a breaking change until a user fixes that themselves. However, I do believe the benefits outweigh the negatives here.
Users will also have to remove the offending dependency externalizations from their configs as well, so that could be considered another breaking change.